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
72 changes: 72 additions & 0 deletions packages/cli/src/check-version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { checkNodeVersion } from './index.js';

describe('checkNodeVersion', () => {
let originalExit: typeof process.exit;
let originalNodeVersion: string;
let consoleErrorSpy: ReturnType<typeof vi.spyOn>>;

beforeEach(() => {
originalExit = process.exit as unknown as typeof vi.fn;
originalNodeVersion = process.versions.node;
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
});

afterEach(() => {
vi.restoreAllMocks();
// Restore original values
(process.exit as unknown as typeof vi.fn).mockRestore?.();
Object.defineProperty(process.versions, 'node', {
value: originalNodeVersion,
writable: true,
});
});

it('passes when Node major version meets requirement', () => {
Object.defineProperty(process.versions, 'node', { value: '20.10.0', writable: true });
vi.spyOn(process, 'exit').mockImplementation(() => {} as never);

// Should not throw or exit
expect(() => checkNodeVersion()).not.toThrow();
expect(process.exit).not.toHaveBeenCalled();
});

it('passes when Node major version exceeds requirement', () => {
Object.defineProperty(process.versions, 'node', { value: '22.5.1', writable: true });
vi.spyOn(process, 'exit').mockImplementation(() => {} as never);

expect(() => checkNodeVersion()).not.toThrow();
expect(process.exit).not.toHaveBeenCalled();
});

it('exits with code 1 when Node major version is below requirement', () => {
Object.defineProperty(process.versions, 'node', { value: '18.19.0', writable: true });
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {} as never);

expect(() => checkNodeVersion()).toThrow();
expect(exitSpy).toHaveBeenCalledWith(1);
});

it('includes the detected version in the error message', () => {
Object.defineProperty(process.versions, 'node', { value: '16.20.2', writable: true });
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {} as never);

try {
checkNodeVersion();
} catch {
// expected
}

expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('v16.20.2'),
);
});

it('exits with code 1 when Node major version equals 19 (below 20)', () => {
Object.defineProperty(process.versions, 'node', { value: '19.9.0', writable: true });
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {} as never);

expect(() => checkNodeVersion()).toThrow();
expect(exitSpy).toHaveBeenCalledWith(1);
});
});
4 changes: 2 additions & 2 deletions packages/cli/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { parseArgs } from './index.js';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { parseArgs, checkNodeVersion } from './index.js';

describe('parseArgs', () => {
it('defaults to `start` with no args', () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ import { initCliTelemetry, captureCliEvent, getInstallId } from './telemetry.js'
const pkg = require('../package.json') as { name: string; version: string };

const DEFAULT_PORT = 8787;
const REQUIRED_NODE_MAJOR = 20;

/**
* Check that the running Node.js version meets the minimum requirement.
* Exits with code 1 and a friendly message if not.
*/
export function checkNodeVersion(): void {
const required = REQUIRED_NODE_MAJOR;
const current = process.versions.node;
const major = Number(current.split('.')[0]);

if (major < required) {
console.error(
`error: Node.js ${required}+ is required (found v${current}).\n\n` +
`GranClaw requires Node.js ${required} or newer. Please upgrade:\n` +
` https://nodejs.org\n\n` +
`You can check your version with: node --version`,
);
process.exit(1);
}
}

// ANSI colour helpers — no external deps
const c = {
Expand Down Expand Up @@ -299,6 +320,8 @@ function startServer(parsed: ParsedArgs): void {
}

export function main(argv: string[]): void {
checkNodeVersion();

let parsed: ParsedArgs;
try {
parsed = parseArgs(argv);
Expand Down