From f34c63bf22a4daf183015c29a08e7abcf3b02d58 Mon Sep 17 00:00:00 2001 From: Zhongjin Lu Date: Wed, 11 Feb 2026 20:41:43 +0800 Subject: [PATCH] feat: add version flags and unify version lookup --- README.md | 1 + src/cli.ts | 6 ++++++ src/daemon-client.ts | 25 +------------------------ src/daemon.ts | 25 +------------------------ src/utils/args.ts | 8 +++++++- src/utils/version.ts | 26 ++++++++++++++++++++++++++ test/integration/smoke-cli.test.ts | 12 ++++++++++++ 7 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 src/utils/version.ts diff --git a/README.md b/README.md index 21d6e823c..ebc98c9a4 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ Notes: - If XCTest returns 0 nodes (e.g., foreground app changed), agent-device falls back to AX when available. Flags: +- `--version, -V` print version and exit - `--platform ios|android` - `--device ` - `--udid ` (iOS) diff --git a/src/cli.ts b/src/cli.ts index c4ecd38fe..c39f3e351 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,7 @@ import { parseArgs, usage } from './utils/args.ts'; import { asAppError, AppError } from './utils/errors.ts'; import { formatSnapshotText, printHumanError, printJson } from './utils/output.ts'; +import { readVersion } from './utils/version.ts'; import { pathToFileURL } from 'node:url'; import { sendToDaemon } from './daemon-client.ts'; import fs from 'node:fs'; @@ -10,6 +11,11 @@ import path from 'node:path'; export async function runCli(argv: string[]): Promise { const parsed = parseArgs(argv); + if (parsed.flags.version) { + process.stdout.write(`${readVersion()}\n`); + process.exit(0); + } + if (parsed.flags.help || !parsed.command) { process.stdout.write(`${usage()}\n`); process.exit(parsed.flags.help ? 0 : 1); diff --git a/src/daemon-client.ts b/src/daemon-client.ts index 85febd742..713dd1501 100644 --- a/src/daemon-client.ts +++ b/src/daemon-client.ts @@ -2,10 +2,10 @@ import net from 'node:net'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; -import { fileURLToPath } from 'node:url'; import { AppError } from './utils/errors.ts'; import type { CommandFlags } from './core/dispatch.ts'; import { runCmdDetached } from './utils/exec.ts'; +import { findProjectRoot, readVersion } from './utils/version.ts'; export type DaemonRequest = { token: string; @@ -134,18 +134,6 @@ async function sendRequest(info: DaemonInfo, req: DaemonRequest): Promise { assert.match(result.stdout, /agent-device/i); }); +test('cli --version prints semver and exits 0', () => { + const result = runCli(['--version']); + assert.equal(result.status, 0, result.stderr); + assert.match(result.stdout, /^\d+\.\d+\.\d+/i); +}); + +test('cli -V prints semver and exits 0', () => { + const result = runCli(['-V']); + assert.equal(result.status, 0, result.stderr); + assert.match(result.stdout, /^\d+\.\d+\.\d+/i); +}); + test('cli without command prints usage and exits 1', () => { const result = runCli([]); assert.equal(result.status, 1, result.stderr);