Skip to content

Commit 4817066

Browse files
fix(cli): launch child commands cross-platform with Execa
Signed-off-by: Aman Varshney <amanvarshney.work@gmail.com>
1 parent 5368acd commit 4817066

7 files changed

Lines changed: 180 additions & 45 deletions

File tree

‎packages/0-framework/3-tooling/cli/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"@internal/foundation": "workspace:0.16.0",
2424
"@prisma/cli-engine": "0.3.0",
2525
"c12": "^3.3.4",
26-
"chokidar": "^4.0.3"
26+
"chokidar": "^4.0.3",
27+
"execa": "^9.6.1"
2728
},
2829
"devDependencies": {
2930
"@internal/tsdown-config": "workspace:0.16.0",

‎packages/0-framework/3-tooling/cli/src/__tests__/run-alchemy.test.ts‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,26 +242,29 @@ describe('spawnAlchemy()', () => {
242242
* a failure: a signal-killed child has NO exit code, and saying otherwise
243243
* loses the only evidence that the user aborted.
244244
*/
245-
test('a signal-killed child comes back as the signal with a null exit code', async () => {
246-
const dir = makeTmpDir();
247-
installFakeAlchemy(dir, [
248-
'process.kill(process.pid, "SIGTERM");',
249-
'setTimeout(() => {}, 5000);',
250-
]);
245+
test.skipIf(process.platform === 'win32')(
246+
'a signal-killed child comes back as the signal with a null exit code',
247+
async () => {
248+
const dir = makeTmpDir();
249+
installFakeAlchemy(dir, [
250+
'process.kill(process.pid, "SIGTERM");',
251+
'setTimeout(() => {}, 5000);',
252+
]);
251253

252-
expect(
253-
await spawnAlchemy({
254-
action: 'deploy',
255-
stackFileRelativePath: '.prisma-composer/alchemy.run.ts',
256-
stage: 'test',
257-
cwd: dir,
258-
env: {},
259-
}),
260-
).toEqual({
261-
exitCode: null,
262-
signal: 'SIGTERM',
263-
});
264-
});
254+
expect(
255+
await spawnAlchemy({
256+
action: 'deploy',
257+
stackFileRelativePath: '.prisma-composer/alchemy.run.ts',
258+
stage: 'test',
259+
cwd: dir,
260+
env: {},
261+
}),
262+
).toEqual({
263+
exitCode: null,
264+
signal: 'SIGTERM',
265+
});
266+
},
267+
);
265268

266269
test('raises the structured error when the app has no alchemy installed', async () => {
267270
const dir = makeTmpDir();

‎packages/0-framework/3-tooling/cli/src/family/runtime.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
* story: this CLI has no login flow and mounts no auth commands, so there is
1313
* nothing to store — the two variables ARE the credential.
1414
*/
15-
import { spawn } from 'node:child_process';
1615
import {
1716
EnvironmentCredentialManager,
1817
type HostProcess,
1918
type ManagementApiClientConfig,
2019
type Runtime,
2120
type SpawnChild,
2221
} from '@prisma/cli-engine';
22+
import { execa } from 'execa';
2323

2424
/** Where the management API lives. Matches the lowering client's default origin; the env var is the escape hatch for staging. */
2525
const DEFAULT_MANAGEMENT_API_BASE_URL = 'https://api.prisma.io';
@@ -33,17 +33,20 @@ const DEFAULT_AUTH_BASE_URL = 'https://auth.prisma.io';
3333
* side of that seam.
3434
*/
3535
const spawnChild: SpawnChild = (request) => {
36-
const child = spawn(request.command, [...request.args], {
36+
const child = execa(request.command, request.args, {
3737
cwd: request.cwd,
3838
stdio: 'inherit',
3939
env: request.env,
40+
extendEnv: false,
41+
reject: false,
4042
});
4143
return {
42-
ended: new Promise((resolve, reject) => {
43-
child.on('error', reject);
44-
child.on('close', (exitCode, signal) => {
45-
resolve({ exitCode, signal });
46-
});
44+
ended: child.then((result) => {
45+
if (result.exitCode === undefined && result.signal === undefined) throw result;
46+
return {
47+
exitCode: result.exitCode ?? null,
48+
signal: result.signal ?? null,
49+
};
4750
}),
4851
kill: (signal) => {
4952
child.kill(signal);

‎packages/0-framework/3-tooling/cli/src/run-alchemy.ts‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
/**
22
* Pipeline step 7 (deploy-cli.md § The pipeline; design-notes.md's "Driving
3-
* Alchemy" call): hand the terminal to the generated stack file. Resolves the
4-
* workspace's own installed `alchemy` bin (walking up `node_modules/.bin`
5-
* from the generated file's package dir) rather than going through
6-
* `bunx`/`npx`, so this works the same under node and bun — the resolved
7-
* bin's own launcher (`alchemy/bin/cli.js`) does its own node/bun dispatch
8-
* from there, driven by the env it inherits.
3+
* Alchemy" call): hand the terminal to the generated stack file.
4+
*
5+
* Resolves the workspace's installed `alchemy` bin. The actual child runner
6+
* uses Execa, which handles package-manager shims and shebangs on Windows
7+
* without a shell while preserving argv boundaries.
98
*
109
* This module composes the invocation; it does not decide how the child is
1110
* started. Under the CLI the engine starts it (`ctx.spawn`), which is what
1211
* makes Ctrl-C reach the child natively and keeps signal policy in one place.
1312
* `spawnAlchemy` is the default for programmatic hosts driving
1413
* `@prisma/composer/control`, which have no engine to borrow.
1514
*/
16-
import { spawn } from 'node:child_process';
1715
import * as fs from 'node:fs';
1816
import * as path from 'node:path';
1917
import { CliStructuredError } from '@internal/foundation/errors';
18+
import { execa } from 'execa';
2019

2120
/** Walks up from `startDir` looking for `node_modules/.bin/alchemy`. */
2221
export function resolveAlchemyBin(startDir: string): string {
@@ -131,15 +130,15 @@ export function alchemyInvocation(input: AlchemyInvocationInput): AlchemyInvocat
131130
*/
132131
export const spawnAlchemy: RunAlchemy = async (invocation) => {
133132
const line = alchemyCommandLine(invocation);
134-
return new Promise<AlchemyOutcome>((resolve, reject) => {
135-
const child = spawn(line.command, [...line.args], {
136-
cwd: line.cwd,
137-
stdio: 'inherit',
138-
env: { ...process.env, ...line.env },
139-
});
140-
child.on('error', reject);
141-
child.on('close', (exitCode, signal) => {
142-
resolve({ exitCode, signal });
143-
});
133+
const result = await execa(line.command, line.args, {
134+
cwd: line.cwd,
135+
stdio: 'inherit',
136+
env: line.env,
137+
reject: false,
144138
});
139+
if (result.exitCode === undefined && result.signal === undefined) throw result;
140+
return {
141+
exitCode: result.exitCode ?? null,
142+
signal: result.signal ?? null,
143+
};
145144
};

‎packages/9-public/composer-cli/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"alchemy": "2.0.0-beta.74",
2626
"c12": "^3.3.4",
2727
"effect": "4.0.0-rc.112",
28-
"esbuild": "^0.28.1"
28+
"esbuild": "^0.28.1",
29+
"execa": "^9.6.1"
2930
},
3031
"peerDependencies": {
3132
"@prisma/cli-engine": "0.3.0"

‎packages/9-public/composer/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"c12": "^3.3.4",
4242
"effect": "4.0.0-rc.112",
4343
"esbuild": "^0.28.1",
44+
"execa": "^9.6.1",
4445
"@prisma/management-api-sdk": "^1.60.0"
4546
},
4647
"devDependencies": {

0 commit comments

Comments
 (0)