diff --git a/src/main/ts/cli.ts b/src/main/ts/cli.ts index 1e21016..4a20ab6 100644 --- a/src/main/ts/cli.ts +++ b/src/main/ts/cli.ts @@ -73,6 +73,10 @@ const flags = new Command() 'Switch log level to verbose/debug', env.YAF_VERBOSE, ) + .option( + '--version, -v', + 'Print current yarn-audit-fix version' + ) .allowUnknownOption() .parse(process.argv) .opts() diff --git a/src/main/ts/runner.ts b/src/main/ts/runner.ts index b7ddbd0..f609894 100644 --- a/src/main/ts/runner.ts +++ b/src/main/ts/runner.ts @@ -3,7 +3,7 @@ import { join } from 'node:path' import { getFlow } from './flows' import { TContext, TFlags, TFlow, TStage } from './ifaces' -import { getTemp, normalizeFlags, readJson } from './util' +import { getSelfManifest, getTemp, normalizeFlags, readJson } from './util' /** * Build running context. @@ -44,6 +44,11 @@ export const exec = (stages: TStage, ctx: TContext): void => { * Public static void main. */ export const runSync = (_flags: TFlags = {}, _flow?: TFlow): void => { + if (_flags.V) { + console.log(getSelfManifest().version) + return + } + const flags = normalizeFlags(_flags) const ctx = getContext(flags) const flow = _flow || getFlow(flags.flow) diff --git a/src/main/ts/stages.ts b/src/main/ts/stages.ts index 82aa5ed..aa88ee3 100644 --- a/src/main/ts/stages.ts +++ b/src/main/ts/stages.ts @@ -1,6 +1,5 @@ import fs from 'fs-extra' import { dirname, join, relative } from 'node:path' -import { fileURLToPath } from 'node:url' import semver from 'semver' import synp from 'synp' @@ -11,23 +10,19 @@ import { formatFlags, getBinVersion, getNpm, + getSelfManifest, getSymlinkType, getWorkspaces, getYarn, invoke, - pkgDir, - readJson, } from './util' -const __dirname = dirname(fileURLToPath(import.meta.url)) /** * Resolve bins. */ export const resolveBins: TCallback = ({ ctx, temp, flags }) => { - const yafManifest = readJson( - join(pkgDir(__dirname) + '', 'package.json'), // eslint-disable-line - ) + const yafManifest = getSelfManifest() ctx.bins = { yarn: getYarn(), npm: getNpm(flags['npm-path']), diff --git a/src/main/ts/util.ts b/src/main/ts/util.ts index 26e2769..15393e9 100644 --- a/src/main/ts/util.ts +++ b/src/main/ts/util.ts @@ -211,3 +211,7 @@ export const formatYaml = yaml.dump export const getBinVersion = (bin: string, cwd = process.cwd()): string => invoke(bin, ['--version'], cwd, true, false) + +export const getSelfManifest = () => readJson( + join(pkgDir(__dirname) + '', 'package.json'), // eslint-disable-line +)