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
38 changes: 30 additions & 8 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFileSync, spawnSync, type SpawnSyncReturns } from 'node:child_process';
import { spawnSync, type SpawnSyncReturns } from 'node:child_process';
import type { SpawnSyncOptions } from 'node:child_process';
import os from 'node:os';
import process from 'node:process';

Expand Down Expand Up @@ -79,15 +80,15 @@ export function runCommandAndEcho(

export function runInheritedRaw(command: string, args: string[], cwd: string): void {
printCommand(command, args);
const result = spawnSync(command, args, { cwd, stdio: 'inherit' });
const result = spawnCommand(command, args, { cwd, stdio: 'inherit' });
if (result.status !== 0) {
throw commandError(command, args, result);
}
}

export function runQuiet(command: string, args: string[], cwd: string): void {
printCommand(command, args);
const result = spawnSync(command, args, { cwd, stdio: 'ignore' });
const result = spawnCommand(command, args, { cwd, stdio: 'ignore' });
if (result.status !== 0) {
throw new Error(`Command failed: ${[command, ...args].join(' ')}`);
}
Expand All @@ -107,14 +108,20 @@ function quoteArg(value: string): string {
}

export function commandExists(command: string): boolean {
return spawnSync(command, ['--version'], { stdio: 'ignore' }).status === 0;
return spawnCommand(command, ['--version'], { stdio: 'ignore' }).status === 0;
}

export function execVersion(command: string): string {
return execFileSync(command, ['--version'], {
const result = spawnCommand(command, ['--version'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
}).trim();
});

if (result.status !== 0 || typeof result.stdout !== 'string') {
throw commandError(command, ['--version'], result);
}

return result.stdout.trim();
}

function spawnWithConfig(
Expand All @@ -124,7 +131,7 @@ function spawnWithConfig(
config: RuntimeConfig,
stdio: 'pipe' | 'inherit' | ['ignore', 'inherit', 'inherit']
): SpawnSyncReturns<Buffer> {
return spawnSync(command, args, {
return spawnCommand(command, args, {
cwd,
env: {
...process.env,
Expand All @@ -134,13 +141,28 @@ function spawnWithConfig(
},
stdio,
maxBuffer: 64 * 1024 * 1024,
}) as SpawnSyncReturns<Buffer>;
}

export function shellForPlatform(platform: NodeJS.Platform): boolean {
return platform === 'win32';
}

function spawnCommand(
command: string,
args: string[],
options: SpawnSyncOptions
): SpawnSyncReturns<Buffer | string> {
return spawnSync(command, args, {
...options,
shell: shellForPlatform(process.platform),
});
}

function commandError(
command: string,
args: string[],
result: SpawnSyncReturns<Buffer>
result: SpawnSyncReturns<Buffer | string>
): Error {
const stdout = bufferToString(result.stdout).trim();
const stderr = bufferToString(result.stderr).trim();
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function maskSecret(value: string): string {
return `${value.slice(0, 4)}...${value.slice(-4)}`;
}

export function bufferToString(value: Buffer | null): string {
export function bufferToString(value: Buffer | string | null): string {
if (typeof value === 'string') return value;
return value ? value.toString('utf8') : '';
}
8 changes: 7 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
parseD1DatabaseId,
parseKVNamespaceId,
} from '../src/cloudflare.ts';
import { runCommand } from '../src/commands.ts';
import { runCommand, shellForPlatform } from '../src/commands.ts';
import { ensureEnvFiles, formatEnvValue } from '../src/env.ts';
import { isCliEntrypoint } from '../src/index.ts';
import { getInstallPlan } from '../src/preflight.ts';
Expand Down Expand Up @@ -301,6 +301,12 @@ describe('wrangler config writing', () => {
});

describe('command runner', () => {
it('uses the shell on Windows so .cmd shims can be resolved from PATH', () => {
expect(shellForPlatform('win32')).toBe(true);
expect(shellForPlatform('darwin')).toBe(false);
expect(shellForPlatform('linux')).toBe(false);
});

it('prints the command and injects Cloudflare environment variables', () => {
const config = createTestConfig();
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined);
Expand Down
Loading