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
12 changes: 11 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* to the requested command, and guarantees the socket is closed afterwards.
*/

import { createRequire } from 'node:module';
import * as path from 'node:path';
import { Command } from 'commander';

Expand Down Expand Up @@ -33,6 +34,15 @@ import { remove } from './commands/remove';
import { login, logout } from './commands/login';
import { setupTypes } from './commands/types';

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

const program = new Command();

let verbose = false;
Expand Down Expand Up @@ -171,7 +181,7 @@ function action(fn: () => Promise<void>): () => Promise<void> {
program
.name('iob-sync')
.description('Sync ioBroker scripts with a local folder')
.version('0.1.0')
.version(version)
.option('-n, --dry-run', 'show what would happen without changing anything')
.option('-v, --verbose', 'verbose output')
.option('-C, --cwd <dir>', 'run as if started in <dir>')
Expand Down
11 changes: 9 additions & 2 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,17 @@ describe('cli argv handling', () => {
}
});

it('prints the version', async () => {
it('prints the version from package.json, not a hardcoded literal', async () => {
// It used to be a string in cli.ts. release-please bumps package.json and
// cannot reach that string, so the published 1.0.0 reported 0.1.0 to every
// user who ran --version.
const pkg = JSON.parse(
await fs.readFile(path.resolve(process.cwd(), 'package.json'), 'utf8'),
) as { version: string };

const { stdout, code } = await runCli(['--version']);

assert.equal(code, 0);
assert.match(stdout, /\d+\.\d+\.\d+/);
assert.equal(stdout.trim(), pkg.version);
});
});