Skip to content

Commit a051e97

Browse files
mschmickingclaude
andcommitted
fix(cli): report the real version instead of a hardcoded 0.1.0
Found by installing the published package and running it: iobroker-sync@1.0.0 printed 0.1.0 for --version. The version was a string literal in cli.ts, and release-please bumps package.json — it has no way to reach a literal in the source, so every release since 0.1.0 would have lied. It is now read from package.json at runtime via createRequire. dist/cli.js sits one level below package.json both in the repository and in the installed package, so the relative path holds in both; verified by dropping the built file into the package installed from npm. The cli test now compares --version against package.json rather than a regex, so the two cannot drift again. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent ba8f2da commit a051e97

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/cli.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* to the requested command, and guarantees the socket is closed afterwards.
77
*/
88

9+
import { createRequire } from 'node:module';
910
import * as path from 'node:path';
1011
import { Command } from 'commander';
1112

@@ -33,6 +34,15 @@ import { remove } from './commands/remove';
3334
import { login, logout } from './commands/login';
3435
import { setupTypes } from './commands/types';
3536

37+
/**
38+
* Read at runtime rather than hardcoded. The literal that used to live here said
39+
* 0.1.0 while the published package was 1.0.0 — release-please bumps package.json
40+
* and had no way to reach a string in the source, so `--version` lied to every
41+
* user. dist/cli.js sits one level below package.json in both the repository and
42+
* the installed package, so the relative path holds in both.
43+
*/
44+
const { version } = createRequire(__filename)('../package.json') as { version: string };
45+
3646
const program = new Command();
3747

3848
let verbose = false;
@@ -171,7 +181,7 @@ function action(fn: () => Promise<void>): () => Promise<void> {
171181
program
172182
.name('iob-sync')
173183
.description('Sync ioBroker scripts with a local folder')
174-
.version('0.1.0')
184+
.version(version)
175185
.option('-n, --dry-run', 'show what would happen without changing anything')
176186
.option('-v, --verbose', 'verbose output')
177187
.option('-C, --cwd <dir>', 'run as if started in <dir>')

test/cli.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,17 @@ describe('cli argv handling', () => {
204204
}
205205
});
206206

207-
it('prints the version', async () => {
207+
it('prints the version from package.json, not a hardcoded literal', async () => {
208+
// It used to be a string in cli.ts. release-please bumps package.json and
209+
// cannot reach that string, so the published 1.0.0 reported 0.1.0 to every
210+
// user who ran --version.
211+
const pkg = JSON.parse(
212+
await fs.readFile(path.resolve(process.cwd(), 'package.json'), 'utf8'),
213+
) as { version: string };
214+
208215
const { stdout, code } = await runCli(['--version']);
209216

210217
assert.equal(code, 0);
211-
assert.match(stdout, /\d+\.\d+\.\d+/);
218+
assert.equal(stdout.trim(), pkg.version);
212219
});
213220
});

0 commit comments

Comments
 (0)