From 53bea18617e68e54e60401e2a1414f0d46f0da68 Mon Sep 17 00:00:00 2001 From: stevelliu Date: Sun, 31 May 2026 09:36:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(cli):=20=E6=94=AF=E6=8C=81=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 -v 和 --version 全局参数,直接读取 package.json 输出当前 CLI 版本,并补充回归测试和 patch changeset。 Co-authored-by: Cursor --- .changeset/quick-version-flag.md | 5 +++++ src/cli.ts | 16 ++++++++++++++++ test/cli.test.ts | 17 +++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 .changeset/quick-version-flag.md diff --git a/.changeset/quick-version-flag.md b/.changeset/quick-version-flag.md new file mode 100644 index 0000000..42a12b8 --- /dev/null +++ b/.changeset/quick-version-flag.md @@ -0,0 +1,5 @@ +--- +"@codehourra/llm-iwiki": patch +--- + +新增 `-v` / `--version` 参数,用于直接打印当前 CLI 版本号。 diff --git a/src/cli.ts b/src/cli.ts index 547c213..f6c5447 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -36,6 +36,8 @@ import { import { applySummaries, prepareSummariesTask } from './summarize' import { runSync } from './sync' +const PACKAGE_JSON_PATH = new URL('../package.json', import.meta.url) + export interface CliRuntime { cwd: string homeDir?: string @@ -76,6 +78,7 @@ Usage: llm-iwiki skills init [--target codex|claude-code|cursor] [--force] [--dry-run] Global flags: + -v, --version 打印版本号 --debug 出错时打印详细堆栈 ` @@ -117,6 +120,14 @@ function reportError(error: unknown, runtime: CliRuntime, debug: boolean): void if (debug && error instanceof Error && error.stack) runtime.stderr(error.stack) } +function readPackageVersion(): string { + const packageJson = JSON.parse(readFileSync(PACKAGE_JSON_PATH, 'utf8')) as { version?: unknown } + if (typeof packageJson.version !== 'string' || packageJson.version.length === 0) { + throw new Error('Unable to read package version.') + } + return packageJson.version +} + /** * 解析 --project(默认当前目录),接受 path / proj_id / name,不创建空项目。 */ @@ -141,6 +152,11 @@ export async function runCli(args: string[], runtime: CliRuntime): Promise { expect(stdout.join('\n')).toContain('Usage:') }) +test('prints package version for -v and --version', async () => { + const packageJson = JSON.parse(readFileSync(join(import.meta.dir, '..', 'package.json'), 'utf8')) as { + version: string + } + + for (const flag of ['-v', '--version']) { + const homeDir = join(tmpRoot, `version-${flag.replace(/^-+/, '')}`) + const { runtime, stdout, stderr } = createRuntime(homeDir) + + const exitCode = await runCli([flag], runtime) + + expect(exitCode).toBe(0) + expect(stderr).toEqual([]) + expect(stdout).toEqual([packageJson.version]) + } +}) + test('sync reports scanning progress when no collectors are detected', async () => { const homeDir = join(tmpRoot, 'sync-empty-home') const { runtime, stdout, stderr } = createRuntime(homeDir)