Skip to content

Commit 9f2eab0

Browse files
authored
Merge pull request #121 from NianJiuZst/codex/fix-version-source
fix derive CLI version from package metadata
2 parents 3402565 + dea0e53 commit 9f2eab0

8 files changed

Lines changed: 39 additions & 9 deletions

File tree

build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { readFileSync, writeFileSync } from 'fs';
33
const pkg = JSON.parse(readFileSync('package.json', 'utf-8'));
44
const VERSION = process.env.VERSION ?? pkg.version;
55
const OUT = 'dist/mmx.mjs';
6+
const DEV_BUILD = process.argv.includes('--dev');
67

78
await Bun.build({
89
entrypoints: ['src/main.ts'],
910
outdir: 'dist',
1011
naming: 'mmx.mjs',
1112
target: 'node',
12-
minify: true,
13+
minify: !DEV_BUILD,
1314
define: { 'process.env.CLI_VERSION': JSON.stringify(VERSION) },
1415
});
1516

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"scripts": {
1616
"dev": "bun run src/main.ts",
1717
"build": "bun run build.ts",
18-
"build:dev": "bun build src/main.ts --outfile dist/mmx.mjs --target node --minify --define \"process.env.CLI_VERSION='1.0.4'\" && printf '#!/usr/bin/env node\\n' | cat - dist/mmx.mjs > dist/tmp && mv dist/tmp dist/mmx.mjs && chmod +x dist/mmx.mjs",
18+
"build:dev": "bun run build.ts --dev",
1919
"prepublishOnly": "bun run build",
2020
"lint": "eslint src/ test/",
2121
"typecheck": "tsc --noEmit",

src/client/http.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ExitCode } from '../errors/codes';
55
import { resolveCredential } from '../auth/resolver';
66
import { mapApiError } from '../errors/api';
77
import { maybeShowStatusBar } from '../output/status-bar';
8+
import { CLI_VERSION } from '../version';
89

910
export interface RequestOpts {
1011
url: string;
@@ -20,9 +21,8 @@ export interface RequestOpts {
2021
export async function request(config: Config, opts: RequestOpts): Promise<Response> {
2122
const isFormData = typeof FormData !== 'undefined' && opts.body instanceof FormData;
2223

23-
const version = process.env.CLI_VERSION ?? '1.0.4';
2424
const headers: Record<string, string> = {
25-
'User-Agent': `mmx-cli/${version}`,
25+
'User-Agent': `mmx-cli/${CLI_VERSION}`,
2626
...opts.headers,
2727
};
2828

src/commands/update.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { defineCommand } from '../command';
2-
3-
const CLI_VERSION = process.env.CLI_VERSION ?? '0.0.0';
2+
import { CLI_VERSION } from '../version';
43

54
export default defineCommand({
65
name: 'update',

src/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import { REGIONS, type Region } from './config/schema';
88
import { checkForUpdate, getPendingUpdateNotification } from './update/checker';
99
import { loadCredentials } from './auth/credentials';
1010
import { ensureApiKey } from './auth/setup';
11-
12-
const CLI_VERSION = process.env.CLI_VERSION ?? '1.0.4';
11+
import { CLI_VERSION } from './version';
1312

1413
// Handle Ctrl+C gracefully
1514
process.on('SIGINT', () => {

src/version.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pkg from '../package.json' with { type: 'json' };
2+
3+
export const CLI_VERSION = process.env.CLI_VERSION ?? pkg.version;

test/client/http.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect, afterEach } from 'bun:test';
22
import { requestJson } from '../../src/client/http';
3+
import { CLI_VERSION } from '../../src/version';
34
import { createMockServer, jsonResponse, type MockServer } from '../helpers/mock-server';
45
import type { Config } from '../../src/config/schema';
56

@@ -28,9 +29,13 @@ describe('HTTP client', () => {
2829
});
2930

3031
it('makes authenticated GET request', async () => {
32+
let userAgent: string | null = null;
3133
server = createMockServer({
3234
routes: {
33-
'/v1/test': () => jsonResponse({ result: 'ok' }),
35+
'/v1/test': (req) => {
36+
userAgent = req.headers.get('user-agent');
37+
return jsonResponse({ result: 'ok' });
38+
},
3439
},
3540
});
3641

@@ -40,6 +45,7 @@ describe('HTTP client', () => {
4045
});
4146

4247
expect(result.result).toBe('ok');
48+
expect(userAgent ?? '').toBe(`mmx-cli/${CLI_VERSION}`);
4349
});
4450

4551
it('makes POST request with body', async () => {

test/version.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, it, expect } from 'bun:test';
2+
import { readFileSync } from 'fs';
3+
import { spawnSync } from 'child_process';
4+
import { CLI_VERSION } from '../src/version';
5+
6+
const packageVersion = (JSON.parse(readFileSync('package.json', 'utf-8')) as { version: string }).version;
7+
8+
describe('CLI version', () => {
9+
it('uses package.json as the source version when no build override is present', () => {
10+
expect(CLI_VERSION).toBe(packageVersion);
11+
});
12+
13+
it('prints the package version in dev mode', () => {
14+
const result = spawnSync(process.execPath, ['src/main.ts', '--version'], {
15+
cwd: process.cwd(),
16+
encoding: 'utf-8',
17+
});
18+
19+
expect(result.status).toBe(0);
20+
expect(result.stdout.trim()).toBe(`mmx ${packageVersion}`);
21+
});
22+
});

0 commit comments

Comments
 (0)