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
3 changes: 3 additions & 0 deletions src/exit-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function settledExitCode(): number {
return typeof process.exitCode === 'number' ? process.exitCode : 0;
}
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { loadConfig } from './config/loader';
import { handleError } from './errors/handler';
import { CLIError, isCLIError } from './errors/base';
import { ExitCode } from './errors/codes';
import { settledExitCode } from './exit-code';
import { registerAllCommands } from './commands';
import {
registry,
Expand Down Expand Up @@ -160,6 +161,10 @@ async function run(): Promise<void> {
}
}
await command.execute(config, globalFlags, args);
// Commands that finish without throwing but still failed (a browser wait
// that timed out) flag it on process.exitCode; a hard exit(0) here used to
// discard it.
const exitCode = settledExitCode();
if (!isTelemetryCommand) {
await dispatch(
config,
Expand All @@ -168,14 +173,14 @@ async function run(): Promise<void> {
command: command.name,
flags: flagNames,
positionalArgCount: positional.length,
exitCode: ExitCode.SUCCESS,
exitCode,
durationMs: Date.now() - started,
credential,
error: null,
})
);
}
process.exit(0);
process.exit(exitCode);
} catch (err) {
const { exitCode, category, rawMessage } = classifyError(err);
if (!isTelemetryCommand) {
Expand Down
53 changes: 53 additions & 0 deletions test/exit-code.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, it, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

import { settledExitCode } from '../src/exit-code';

describe('settledExitCode', () => {
afterEach(() => {
process.exitCode = undefined;
});

it('is 0 when no command flagged a soft failure', () => {
assert.equal(settledExitCode(), 0);
});

it('carries the code a command set on process.exitCode', () => {
process.exitCode = 1;
assert.equal(settledExitCode(), 1);
});
});

describe('main honors process.exitCode after a command completes', () => {
// Preload sets process.exitCode the way the connect commands do on a
// browser-wait timeout; `config show` completes without throwing.
function runConfigShow(preset: string): number | null {
const home = mkdtempSync(join(tmpdir(), 'polylane-exit-code-test-'));
try {
const result = spawnSync(
process.execPath,
['--import', 'tsx', '--import', './test/helpers/preset-exit-code.ts', 'src/main.ts', 'config', 'show'],
{
cwd: join(import.meta.dirname, '..'),
env: { ...process.env, HOME: home, PRESET_EXIT_CODE: preset, POLYLANE_TELEMETRY: '0' },
encoding: 'utf8',
}
);
return result.status;
} finally {
rmSync(home, { recursive: true, force: true });
}
}

it('exits 0 when nothing was flagged', () => {
assert.equal(runConfigShow('0'), 0);
});

it('exits with the flagged code instead of 0', () => {
assert.equal(runConfigShow('1'), 1);
});
});
1 change: 1 addition & 0 deletions test/helpers/preset-exit-code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.exitCode = Number(process.env.PRESET_EXIT_CODE);
Loading