Skip to content

Commit

Permalink
fix: move back to @jsdevtools/ez-spawn, fix #224
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 14, 2024
1 parent bbe28d0 commit af1b1be
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 52 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@
"lint": "eslint .",
"test": "vitest"
},
"dependencies": {
"package-manager-detector": "^0.1.1",
"tinyexec": "^0.1.4"
},
"devDependencies": {
"@antfu/eslint-config": "^2.25.1",
"@jsdevtools/ez-spawn": "^3.0.4",
"@posva/prompts": "^2.4.4",
"@types/fs-extra": "^11.0.4",
"@types/ini": "^4.1.1",
Expand All @@ -63,6 +60,7 @@
"fs-extra": "^11.2.0",
"fzf": "^0.5.2",
"ini": "^4.1.3",
"package-manager-detector": "^0.1.1",
"picocolors": "^1.0.1",
"taze": "^0.16.5",
"terminal-link": "^3.0.0",
Expand Down
35 changes: 15 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions src/detect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'node:process'
import { x } from 'tinyexec'
import { async as ezspawn } from '@jsdevtools/ez-spawn'
import terminalLink from 'terminal-link'
import prompts from '@posva/prompts'
import { detect as detectPM } from 'package-manager-detector'
Expand Down Expand Up @@ -43,15 +43,10 @@ export async function detect({ autoInstall, programmatic, cwd }: DetectOptions =
process.exit(1)
}

await x(
await ezspawn(
'npm',
['i', '-g', `${agent.split('@')[0]}${version ? `@${version}` : ''}`],
{
nodeOptions: {
stdio: 'inherit',
cwd,
},
},
{ stdio: 'inherit', cwd },
)
}

Expand Down
36 changes: 16 additions & 20 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import { resolve } from 'node:path'
import process from 'node:process'
import prompts from '@posva/prompts'
import type { Options as TinyExecOptions } from 'tinyexec'
import { x } from 'tinyexec'
import { async as ezspawn } from '@jsdevtools/ez-spawn'
import c from 'picocolors'
import type { Agent } from 'package-manager-detector/agents'
import { AGENTS } from 'package-manager-detector/agents'
Expand Down Expand Up @@ -35,8 +34,10 @@ export async function runCli(fn: Runner, options: DetectOptions & { args?: strin
if (error instanceof UnsupportedCommand && !options.programmatic)
console.log(c.red(`\u2717 ${error.message}`))

if (!options.programmatic)
if (!options.programmatic) {
console.error(error)
process.exit(1)
}

throw error
}
Expand Down Expand Up @@ -85,19 +86,19 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
}

if (args.length === 1 && (args[0]?.toLowerCase() === '-v' || args[0] === '--version')) {
const getCmd = (a: Agent) => AGENTS.includes(a) ? getCommand(a, 'agent', ['-v']) : `${a} -v`
const getV = (a: string, o?: Partial<TinyExecOptions>) => x(getCmd(a as Agent), undefined, o).then(e => e.stdout).then(e => e.startsWith('v') ? e : `v${e}`)
const getVersionOf = (a: string) => {
const command = AGENTS.includes(a as Agent)
? getCommand(a as Agent, 'agent', ['-v'])
: `${a} -v`
return ezspawn(command, { cwd })
.then(e => e.stdout)
.then(e => e.startsWith('v') ? e : `v${e}`)
}
const globalAgentPromise = getGlobalAgent()
const globalAgentVersionPromise = globalAgentPromise.then(getV)
const globalAgentVersionPromise = globalAgentPromise.then(getVersionOf)
const agentPromise = detect({ ...options, cwd }).then(a => a || '')
const agentVersionPromise = agentPromise.then(a => a && getV(a, {
nodeOptions: {
cwd,
},
}))
const nodeVersionPromise = getV('node', {
nodeOptions: { cwd },
})
const agentVersionPromise = agentPromise.then(a => a && getVersionOf(a))
const nodeVersionPromise = getVersionOf('node')

console.log(`@antfu/ni ${c.cyan(`v${version}`)}`)
console.log(`node ${c.green(await nodeVersionPromise)}`)
Expand Down Expand Up @@ -146,10 +147,5 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
return
}

await x(command, undefined, {
nodeOptions: {
stdio: 'inherit',
cwd,
},
})
await ezspawn(command, { stdio: 'inherit', cwd })
}

0 comments on commit af1b1be

Please sign in to comment.