Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: silent programmatic usage #151

Merged
merged 6 commits into from Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/commands/nr.ts
Expand Up @@ -12,15 +12,16 @@ runCli(async (agent, args, ctx) => {

if (args[0] === '-') {
if (!storage.lastRunCommand) {
console.error('No last command found')
process.exit(1)
!ctx.programmatic ? console.error('No last command found') : 0
!ctx.programmatic ? process.exit(1) : 0
millsp marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('No last command found')
}
args[0] = storage.lastRunCommand
}

if (args.length === 0) {
if (args.length === 0 && !ctx.programmatic) {
// support https://www.npmjs.com/package/npm-scripts-info conventions
const pkg = getPackageJSON(ctx?.cwd)
const pkg = getPackageJSON(ctx)
const scripts = pkg.scripts || {}
const scriptsInfo = pkg['scripts-info'] || {}

Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Expand Up @@ -44,9 +44,9 @@ export async function getConfig(): Promise<Config> {
return config
}

export async function getDefaultAgent() {
export async function getDefaultAgent(programmatic?: boolean) {
const { defaultAgent } = await getConfig()
if (defaultAgent === 'prompt' && process.env.CI)
if (defaultAgent === 'prompt' && (programmatic || process.env.CI))
return 'npm'
return defaultAgent
}
Expand Down
7 changes: 4 additions & 3 deletions src/detect.ts
Expand Up @@ -10,10 +10,11 @@ import { cmdExists } from './utils'

export interface DetectOptions {
autoInstall?: boolean
programmatic?: boolean
cwd?: string
}

export async function detect({ autoInstall, cwd }: DetectOptions = {}) {
export async function detect({ autoInstall, programmatic, cwd }: DetectOptions = {}) {
let agent: Agent | null = null
let version: string | null = null

Expand All @@ -39,7 +40,7 @@ export async function detect({ autoInstall, cwd }: DetectOptions = {}) {
else if (name in AGENTS)
agent = name
else
console.warn('[ni] Unknown packageManager:', pkg.packageManager)
!programmatic ? console.warn('[ni] Unknown packageManager:', pkg.packageManager) : 0
}
}
catch {}
Expand All @@ -50,7 +51,7 @@ export async function detect({ autoInstall, cwd }: DetectOptions = {}) {
agent = LOCKS[path.basename(lockPath)]

// auto install
if (agent && !cmdExists(agent.split('@')[0])) {
if (agent && !cmdExists(agent.split('@')[0]) && !options.programmatic) {
if (!autoInstall) {
console.warn(`[ni] Detected ${agent} but it doesn't seem to be installed.\n`)

Expand Down
8 changes: 5 additions & 3 deletions src/fs.ts
@@ -1,7 +1,8 @@
import { resolve } from 'node:path'
import type { RunnerContext } from './runner'
import fs from 'node:fs'

export function getPackageJSON(cwd = process.cwd()): any {
export function getPackageJSON({ cwd = process.cwd(), programmatic }: RunnerContext): any {
const path = resolve(cwd, 'package.json')

if (fs.existsSync(path)) {
Expand All @@ -11,8 +12,9 @@ export function getPackageJSON(cwd = process.cwd()): any {
return data
}
catch (e) {
console.warn('Failed to parse package.json')
process.exit(0)
!programmatic ? console.warn('Failed to parse package.json') : 0
!programmatic ? process.exit(0) : 0
millsp marked this conversation as resolved.
Show resolved Hide resolved
throw e
}
}
}
9 changes: 6 additions & 3 deletions src/runner.ts
Expand Up @@ -15,6 +15,7 @@ import { UnsupportedCommand } from './parse'
const DEBUG_SIGN = '?'

export interface RunnerContext {
programmatic?: boolean
hasLock?: boolean
cwd?: string
}
Expand All @@ -28,9 +29,10 @@ export async function runCli(fn: Runner, options: DetectOptions = {}) {
}
catch (error) {
if (error instanceof UnsupportedCommand)
console.log(c.red(`\u2717 ${error.message}`))
!options.programmatic ? console.log(c.red(`\u2717 ${error.message}`)) : 0

process.exit(1)
!options.programmatic ? process.exit(1) : 0
throw e
}
}

Expand Down Expand Up @@ -71,7 +73,7 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
command = await fn(await getGlobalAgent(), args)
}
else {
let agent = await detect({ ...options, cwd }) || await getDefaultAgent()
let agent = await detect({ ...options, cwd }) || await getDefaultAgent(options.programmatic)
if (agent === 'prompt') {
agent = (await prompts({
name: 'agent',
Expand All @@ -83,6 +85,7 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
return
}
command = await fn(agent as Agent, args, {
programmatic: options.programmatic,
hasLock: Boolean(agent),
cwd,
})
Expand Down