Skip to content
Open
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
3 changes: 2 additions & 1 deletion build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { readFileSync, writeFileSync } from 'fs';
const pkg = JSON.parse(readFileSync('package.json', 'utf-8'));
const VERSION = process.env.VERSION ?? pkg.version;
const OUT = 'dist/mmx.mjs';
const DEV_BUILD = process.argv.includes('--dev');

await Bun.build({
entrypoints: ['src/main.ts'],
outdir: 'dist',
naming: 'mmx.mjs',
target: 'node',
minify: true,
minify: !DEV_BUILD,
define: { 'process.env.CLI_VERSION': JSON.stringify(VERSION) },
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"dev": "bun run src/main.ts",
"build": "bun run build.ts",
"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",
"build:dev": "bun run build.ts --dev",
"prepublishOnly": "bun run build",
"lint": "eslint src/ test/",
"typecheck": "tsc --noEmit",
Expand Down
4 changes: 2 additions & 2 deletions src/client/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ExitCode } from '../errors/codes';
import { resolveCredential } from '../auth/resolver';
import { mapApiError } from '../errors/api';
import { maybeShowStatusBar } from '../output/status-bar';
import { CLI_VERSION } from '../version';

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

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

Expand Down
3 changes: 1 addition & 2 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defineCommand } from '../command';

const CLI_VERSION = process.env.CLI_VERSION ?? '0.0.0';
import { CLI_VERSION } from '../version';

export default defineCommand({
name: 'update',
Expand Down
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { REGIONS, type Region } from './config/schema';
import { checkForUpdate, getPendingUpdateNotification } from './update/checker';
import { loadCredentials } from './auth/credentials';
import { ensureApiKey } from './auth/setup';

const CLI_VERSION = process.env.CLI_VERSION ?? '1.0.4';
import { CLI_VERSION } from './version';

// Handle Ctrl+C gracefully
process.on('SIGINT', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pkg from '../package.json' with { type: 'json' };

export const CLI_VERSION = process.env.CLI_VERSION ?? pkg.version;
8 changes: 7 additions & 1 deletion test/client/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, afterEach } from 'bun:test';
import { requestJson } from '../../src/client/http';
import { CLI_VERSION } from '../../src/version';
import { createMockServer, jsonResponse, type MockServer } from '../helpers/mock-server';
import type { Config } from '../../src/config/schema';

Expand Down Expand Up @@ -28,9 +29,13 @@ describe('HTTP client', () => {
});

it('makes authenticated GET request', async () => {
let userAgent: string | null = null;
server = createMockServer({
routes: {
'/v1/test': () => jsonResponse({ result: 'ok' }),
'/v1/test': (req) => {
userAgent = req.headers.get('user-agent');
return jsonResponse({ result: 'ok' });
},
},
});

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

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

it('makes POST request with body', async () => {
Expand Down
22 changes: 22 additions & 0 deletions test/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it, expect } from 'bun:test';
import { readFileSync } from 'fs';
import { spawnSync } from 'child_process';
import { CLI_VERSION } from '../src/version';

const packageVersion = (JSON.parse(readFileSync('package.json', 'utf-8')) as { version: string }).version;

describe('CLI version', () => {
it('uses package.json as the source version when no build override is present', () => {
expect(CLI_VERSION).toBe(packageVersion);
});

it('prints the package version in dev mode', () => {
const result = spawnSync(process.execPath, ['src/main.ts', '--version'], {
cwd: process.cwd(),
encoding: 'utf-8',
});

expect(result.status).toBe(0);
expect(result.stdout.trim()).toBe(`mmx ${packageVersion}`);
});
});
Loading