Skip to content

Commit 05a76ca

Browse files
committed
chore(gates): flush diagnostics before forced exit
1 parent 7008b7c commit 05a76ca

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

‎scripts/check-affected/command.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,5 @@ runEntrypoint('validation command', async () => {
1818
process.stderr.write(chunk);
1919
},
2020
});
21-
await Promise.all(
22-
[process.stdout, process.stderr].map(
23-
(stream) =>
24-
new Promise<void>((resolve, reject) =>
25-
stream.write('', (error) => (error ? reject(error) : resolve())),
26-
),
27-
),
28-
);
2921
return result.exitCode;
3022
});

‎scripts/lib/cli-entrypoint.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import assert from 'node:assert/strict';
2+
import { spawn } from 'node:child_process';
3+
import { once } from 'node:events';
4+
import { test } from 'vitest';
5+
6+
test.each([false, true])('script exit flushes piped diagnostics (throws=%s)', async (throws) => {
7+
const bytes = 4 * 1024 * 1024;
8+
const stdoutTail = 'stdout diagnostic tail\n';
9+
const stderrTail = 'stderr diagnostic tail\n';
10+
const failure = 'fixture: expected failure\n';
11+
const source = [
12+
`import { runEntrypoint } from ${JSON.stringify(new URL('./cli-entrypoint.ts', import.meta.url).href)};`,
13+
'runEntrypoint("fixture", async () => {',
14+
`process.stdout.write('o'.repeat(${bytes}) + ${JSON.stringify(stdoutTail)});`,
15+
`process.stderr.write('e'.repeat(${bytes}) + ${JSON.stringify(stderrTail)});`,
16+
throws ? 'throw new Error("expected failure");' : 'return 7;',
17+
'});',
18+
].join('\n');
19+
const child = spawn(
20+
process.execPath,
21+
['--experimental-strip-types', '--input-type=module', '--eval', source],
22+
{ stdio: ['ignore', 'pipe', 'pipe'] },
23+
);
24+
let stdout = '';
25+
let stderr = '';
26+
child.stdout.setEncoding('utf8').on('data', (chunk: string) => (stdout += chunk));
27+
child.stderr.setEncoding('utf8').on('data', (chunk: string) => (stderr += chunk));
28+
const [code, signal] = await once(child, 'close');
29+
assert.equal(signal, null);
30+
assert.equal(code, throws ? 1 : 7);
31+
assert.equal(stdout.length, bytes + stdoutTail.length);
32+
assert.ok(stdout.endsWith(stdoutTail));
33+
assert.equal(stderr.length, bytes + stderrTail.length + (throws ? failure.length : 0));
34+
assert.ok(stderr.endsWith(throws ? failure : stderrTail));
35+
});

‎scripts/lib/cli-entrypoint.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
// thrown error into one prefixed line rather than a stack trace.
33

44
export function runEntrypoint(prefix: string, main: () => Promise<number>): void {
5-
main().then(
6-
(code) => process.exit(code),
7-
(error: unknown) => {
8-
process.stderr.write(`${prefix}: ${error instanceof Error ? error.message : error}\n`);
9-
process.exit(1);
10-
},
11-
);
5+
const exitAfterOutput = async (code: number): Promise<never> => {
6+
await Promise.all(
7+
[process.stdout, process.stderr].map(
8+
(stream) =>
9+
new Promise<void>((resolve, reject) =>
10+
stream.write('', (error) => (error ? reject(error) : resolve())),
11+
),
12+
),
13+
);
14+
process.exit(code);
15+
};
16+
main().then(exitAfterOutput, (error: unknown) => {
17+
process.stderr.write(`${prefix}: ${error instanceof Error ? error.message : error}\n`);
18+
return exitAfterOutput(1);
19+
});
1220
}

‎vitest.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export default defineConfig({
185185
// package path remains accounted for.
186186
'scripts/__tests__/size-report-package.test.ts',
187187
'scripts/__tests__/bundled-license-notices.test.ts',
188+
'scripts/lib/cli-entrypoint.test.ts',
188189
// Publish preparation spawns only fixture-owned scripts and proves both Android
189190
// helper families are rebuilt through the shared release/size-report owner.
190191
'scripts/__tests__/prepare-publish-assets.test.ts',

0 commit comments

Comments
 (0)