Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-version-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codehourra/llm-iwiki": patch
---

新增 `-v` / `--version` 参数,用于直接打印当前 CLI 版本号。
16 changes: 16 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,6 +78,7 @@ Usage:
llm-iwiki skills init [--target codex|claude-code|cursor] [--force] [--dry-run]

Global flags:
-v, --version 打印版本号
--debug 出错时打印详细堆栈
`

Expand Down Expand Up @@ -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,不创建空项目。
*/
Expand All @@ -141,6 +152,11 @@ export async function runCli(args: string[], runtime: CliRuntime): Promise<numbe
return 0
}

if (args[0] === '--version' || args[0] === '-v') {
runtime.stdout(readPackageVersion())
return 0
}

if (args[0] === 'init') {
const paths = getAppPaths(runtime.homeDir)
mkdirSync(paths.configDir, { recursive: true })
Expand Down
17 changes: 17 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ test('prints help for --help', async () => {
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)
Expand Down