Skip to content

Commit 798ebc4

Browse files
nzakasCopilot
andauthored
feat: Add -v/--version flags to CLI (#137)
* Initial plan * Add --version and -v flags to CLI Co-authored-by: nzakas <38546+nzakas@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nzakas <38546+nzakas@users.noreply.github.com>
1 parent dda375d commit 798ebc4

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/bin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@ const options = {
7070
image: { type: stringType },
7171
"image-alt": { type: stringType },
7272
help: { type: booleanType, short: "h" },
73+
version: { type: booleanType, short: "v" },
7374
};
7475

7576
const { values: flags, positionals } = parseArgs({
7677
options,
7778
allowPositionals: true,
7879
});
7980

81+
if (flags.version) {
82+
const packagePath = new URL("../package.json", import.meta.url);
83+
const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
84+
console.log(packageJson.version);
85+
process.exit(0);
86+
}
87+
8088
if (flags.mcp && flags.file) {
8189
console.error("Error: --file cannot be used with --mcp");
8290
process.exit(1);
@@ -111,6 +119,7 @@ if (
111119
console.log("--image The image file to upload with the message.");
112120
console.log("--image-alt Alt text for the image (default: filename).");
113121
console.log("--help, -h Show this message.");
122+
console.log("--version, -v Show version number.");
114123
process.exit(1);
115124
}
116125

tests/bin.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
import { strict as assert } from "node:assert";
1313
import { fork } from "node:child_process";
14+
import fs from "node:fs";
1415
import path from "node:path";
1516

1617
//-----------------------------------------------------------------------------
1718
// Data
1819
//-----------------------------------------------------------------------------
1920

2021
const executablePath = path.resolve("src/bin.js");
22+
const builtExecutablePath = path.resolve("dist/bin.js");
2123

2224
//-----------------------------------------------------------------------------
2325
// Tests
@@ -55,4 +57,65 @@ describe("bin", function () {
5557
done();
5658
});
5759
});
60+
61+
describe("version flag", function () {
62+
it("should display version with --version flag", done => {
63+
const child = fork(builtExecutablePath, ["--version"], {
64+
stdio: "pipe",
65+
});
66+
67+
let output = "";
68+
69+
child.stdout.on("data", data => {
70+
output += data.toString();
71+
});
72+
73+
child.on("exit", code => {
74+
assert.strictEqual(code, 0);
75+
assert.match(output.trim(), /^\d+\.\d+\.\d+$/);
76+
done();
77+
});
78+
});
79+
80+
it("should display version with -v flag", done => {
81+
const child = fork(builtExecutablePath, ["-v"], {
82+
stdio: "pipe",
83+
});
84+
85+
let output = "";
86+
87+
child.stdout.on("data", data => {
88+
output += data.toString();
89+
});
90+
91+
child.on("exit", code => {
92+
assert.strictEqual(code, 0);
93+
assert.match(output.trim(), /^\d+\.\d+\.\d+$/);
94+
done();
95+
});
96+
});
97+
98+
it("should display correct version from package.json", done => {
99+
// Read the actual version from package.json
100+
const packagePath = path.resolve("package.json");
101+
const packageJson = JSON.parse(fs.readFileSync(packagePath, "utf8"));
102+
const expectedVersion = packageJson.version;
103+
104+
const child = fork(builtExecutablePath, ["--version"], {
105+
stdio: "pipe",
106+
});
107+
108+
let output = "";
109+
110+
child.stdout.on("data", data => {
111+
output += data.toString();
112+
});
113+
114+
child.on("exit", code => {
115+
assert.strictEqual(code, 0);
116+
assert.strictEqual(output.trim(), expectedVersion);
117+
done();
118+
});
119+
});
120+
});
58121
});

0 commit comments

Comments
 (0)