Skip to content

Commit 9670547

Browse files
author
Zhongjin Lu
committed
feat: add --version and -V flags to CLI
Print the installed version and exit with code 0, consistent with common CLI conventions. Closes #43
1 parent 6a064f2 commit 9670547

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/cli.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parseArgs, usage } from './utils/args.ts';
22
import { asAppError, AppError } from './utils/errors.ts';
33
import { formatSnapshotText, printHumanError, printJson } from './utils/output.ts';
4-
import { pathToFileURL } from 'node:url';
4+
import { pathToFileURL, fileURLToPath } from 'node:url';
55
import { sendToDaemon } from './daemon-client.ts';
66
import fs from 'node:fs';
77
import os from 'node:os';
@@ -10,6 +10,14 @@ import path from 'node:path';
1010
export async function runCli(argv: string[]): Promise<void> {
1111
const parsed = parseArgs(argv);
1212

13+
if (parsed.flags.version) {
14+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
15+
const pkgPath = path.join(__dirname, '..', '..', 'package.json');
16+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
17+
process.stdout.write(`${pkg.version}\n`);
18+
process.exit(0);
19+
}
20+
1321
if (parsed.flags.help || !parsed.command) {
1422
process.stdout.write(`${usage()}\n`);
1523
process.exit(parsed.flags.help ? 0 : 1);

src/utils/args.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ export type ParsedArgs = {
2525
noRecord?: boolean;
2626
replayUpdate?: boolean;
2727
help: boolean;
28+
version: boolean;
2829
};
2930
};
3031

3132
export function parseArgs(argv: string[]): ParsedArgs {
32-
const flags: ParsedArgs['flags'] = { json: false, help: false };
33+
const flags: ParsedArgs['flags'] = { json: false, help: false, version: false };
3334
const positionals: string[] = [];
3435

3536
for (let i = 0; i < argv.length; i += 1) {
@@ -42,6 +43,10 @@ export function parseArgs(argv: string[]): ParsedArgs {
4243
flags.help = true;
4344
continue;
4445
}
46+
if (arg === '--version' || arg === '-V') {
47+
flags.version = true;
48+
continue;
49+
}
4550
if (arg === '--verbose' || arg === '-v') {
4651
flags.verbose = true;
4752
continue;

0 commit comments

Comments
 (0)