diff --git a/README.md b/README.md index d7c20a9..51b7eba 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,11 @@ Zero dependencies. JSON output by default. Pipeable. Works with CapCut and JianY ```bash npm install -g capcut-cli ``` +Verify the installation: +```bash +capcut --version # prints the installed CLI version +``` Or run directly: ```bash npx capcut-cli info ./my-project/ diff --git a/src/index.ts b/src/index.ts index 2248d00..fb69075 100644 --- a/src/index.ts +++ b/src/index.ts @@ -85,6 +85,7 @@ Usage: capcut [options] Global flags: -H, --human Human-readable table output (default: JSON) + -v, --version Print the installed CLI version -q, --quiet No output on success, exit code only (write commands) --jianying Use JianYing enum namespace (default: CapCut) for transition, mask, text-anim, image-anim, add-effect, enums @@ -452,6 +453,7 @@ interface Flags { // serve queue?: string; failFast?: boolean; + version?: boolean; } // Map CLI enum flags -> enums.json category key. Order matters for HELP text. @@ -477,6 +479,7 @@ function parseFlags(args: string[]): { positional: string[]; flags: Flags } { for (let i = 0; i < args.length; i++) { const a = args[i]; if (a === "-H" || a === "--human") flags.human = true; + else if (a === "-v" || a === "--version") flags.version = true; else if (a === "-q" || a === "--quiet") flags.quiet = true; else if (a === "--batch") flags.batch = true; else if ((a === "--track" || a === "--type") && i + 1 < args.length) { @@ -1924,6 +1927,12 @@ function cmdDoctor(flags: Flags): boolean { return report.ok; } +function getCliVersion(): string { + const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); + + return pkg.version; +} + // --- Main --- async function main(): Promise { @@ -1934,6 +1943,11 @@ async function main(): Promise { } const { positional, flags } = parseFlags(raw); + + if (flags.version) { + console.log(getCliVersion()); + process.exit(0); + } const cmd = positional[0]; const projectPath = positional[1]; diff --git a/test/cli-version.test.mjs b/test/cli-version.test.mjs new file mode 100644 index 0000000..dc64c21 --- /dev/null +++ b/test/cli-version.test.mjs @@ -0,0 +1,25 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { describe, it } from "node:test"; +import { fileURLToPath } from "node:url"; +import { spawnCli } from "./helpers/spawn-cli.mjs"; + +const repoRoot = join(dirname(fileURLToPath(import.meta.url)), ".."); +const pkg = JSON.parse(readFileSync(join(repoRoot, "package.json"), "utf8")); + +describe("capcut --version", () => { + it("prints package version with --version", () => { + const r = spawnCli(["--version"]); + + assert.equal(r.status, 0); + assert.equal(r.stdout.trim(), pkg.version); + }); + + it("prints package version with -v", () => { + const r = spawnCli(["-v"]); + + assert.equal(r.status, 0); + assert.equal(r.stdout.trim(), pkg.version); + }); +});