Skip to content

Commit 3566e43

Browse files
Dimavaantfu
andauthoredSep 1, 2023
feat: add flag to show agent versions (#169)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent b351d57 commit 3566e43

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed
 

‎src/runner.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { resolve } from 'node:path'
33
import process from 'node:process'
44
import prompts from '@posva/prompts'
5+
import type { Options as ExecaOptions } from 'execa'
56
import { execaCommand } from 'execa'
67
import c from 'kleur'
78
import { version } from '../package.json'
@@ -75,6 +76,32 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
7576
if (debug)
7677
remove(args, DEBUG_SIGN)
7778

79+
let cwd = options.cwd ?? process.cwd()
80+
if (args[0] === '-C') {
81+
cwd = resolve(cwd, args[1])
82+
args.splice(0, 2)
83+
}
84+
85+
if (args.length === 1 && (args[0]?.toLowerCase() === '-v')) {
86+
const getV = (a: string, o?: ExecaOptions) => execaCommand(`${a} -v`, o).then(e => e.stdout).then(e => e.startsWith('v') ? e : `v${e}`)
87+
const globalAgentPromise = getGlobalAgent()
88+
const globalAgentVersionPromise = globalAgentPromise.then(getV)
89+
const agentPromise = detect({ ...options, cwd }).then(a => a || '')
90+
const agentVersionPromise = agentPromise.then(a => a && getV(a, { cwd }))
91+
const nodeVersionPromise = getV('node', { cwd })
92+
93+
console.log(`@antfu/ni ${c.cyan(`v${version}`)}`)
94+
console.log(`node ${c.green(await nodeVersionPromise)}`)
95+
const [agent, agentVersion] = await Promise.all([agentPromise, agentVersionPromise])
96+
if (agent)
97+
console.log(`${agent.padEnd(10)} ${c.blue(agentVersion)}`)
98+
else
99+
console.log('agent no lock file')
100+
const [globalAgent, globalAgentVersion] = await Promise.all([globalAgentPromise, globalAgentVersionPromise])
101+
console.log(`${(`${globalAgent} -g`).padEnd(10)} ${c.blue(globalAgentVersion)}`)
102+
return
103+
}
104+
78105
if (args.length === 1 && (args[0] === '--version' || args[0] === '-v')) {
79106
console.log(`@antfu/ni v${version}`)
80107
return
@@ -94,13 +121,6 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
94121
return
95122
}
96123

97-
let cwd = options.cwd ?? process.cwd()
98-
99-
if (args[0] === '-C') {
100-
cwd = resolve(cwd, args[1])
101-
args.splice(0, 2)
102-
}
103-
104124
let command = await getCliCommand(fn, args, options, cwd)
105125

106126
if (!command)

0 commit comments

Comments
 (0)
Please sign in to comment.