Skip to content

Commit

Permalink
feat: add flag to show agent versions (#169)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
Dimava and antfu committed Sep 1, 2023
1 parent b351d57 commit 3566e43
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { resolve } from 'node:path'
import process from 'node:process'
import prompts from '@posva/prompts'
import type { Options as ExecaOptions } from 'execa'
import { execaCommand } from 'execa'
import c from 'kleur'
import { version } from '../package.json'
Expand Down Expand Up @@ -75,6 +76,32 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
if (debug)
remove(args, DEBUG_SIGN)

let cwd = options.cwd ?? process.cwd()
if (args[0] === '-C') {
cwd = resolve(cwd, args[1])
args.splice(0, 2)
}

if (args.length === 1 && (args[0]?.toLowerCase() === '-v')) {
const getV = (a: string, o?: ExecaOptions) => execaCommand(`${a} -v`, o).then(e => e.stdout).then(e => e.startsWith('v') ? e : `v${e}`)
const globalAgentPromise = getGlobalAgent()
const globalAgentVersionPromise = globalAgentPromise.then(getV)
const agentPromise = detect({ ...options, cwd }).then(a => a || '')
const agentVersionPromise = agentPromise.then(a => a && getV(a, { cwd }))
const nodeVersionPromise = getV('node', { cwd })

console.log(`@antfu/ni ${c.cyan(`v${version}`)}`)
console.log(`node ${c.green(await nodeVersionPromise)}`)
const [agent, agentVersion] = await Promise.all([agentPromise, agentVersionPromise])
if (agent)
console.log(`${agent.padEnd(10)} ${c.blue(agentVersion)}`)
else
console.log('agent no lock file')
const [globalAgent, globalAgentVersion] = await Promise.all([globalAgentPromise, globalAgentVersionPromise])
console.log(`${(`${globalAgent} -g`).padEnd(10)} ${c.blue(globalAgentVersion)}`)
return
}

if (args.length === 1 && (args[0] === '--version' || args[0] === '-v')) {
console.log(`@antfu/ni v${version}`)
return
Expand All @@ -94,13 +121,6 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
return
}

let cwd = options.cwd ?? process.cwd()

if (args[0] === '-C') {
cwd = resolve(cwd, args[1])
args.splice(0, 2)
}

let command = await getCliCommand(fn, args, options, cwd)

if (!command)
Expand Down

0 comments on commit 3566e43

Please sign in to comment.