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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
"@clack/prompts": "catalog:prod",
"ansis": "catalog:prod",
"cac": "catalog:prod",
"cross-spawn": "catalog:prod",
"fast-npm-meta": "catalog:prod",
"fzf": "catalog:prod",
"yaml": "catalog:prod"
},
"devDependencies": {
"@sxzz/eslint-config": "^7.8.3",
"@sxzz/prettier-config": "^2.3.1",
"@types/cross-spawn": "^6.0.6",
"@types/node": "^25.3.5",
"@typescript/native-preview": "7.0.0-dev.20260309.1",
"bumpp": "^10.4.1",
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ catalogs:
'@clack/prompts': ^1.1.0
ansis: ^4.2.0
cac: ^7.0.0
cross-spawn: ^7.0.6
fast-npm-meta: ^1.4.2
fzf: ^0.5.2
yaml: ^2.7.0
Expand Down
35 changes: 35 additions & 0 deletions src/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import spawn from 'cross-spawn'

export interface RunCommandOptions {
command: string
args?: string[]
cwd?: string
}

/**
* Run a command synchronously with inherited stdio.
*
* Uses `cross-spawn` instead of `node:child_process` so that package manager
* binaries installed as `.cmd`/`.ps1` shims on Windows (pnpm, npm, yarn, ...)
* resolve correctly. Arguments are escaped by `cross-spawn` and never
* interpreted by a shell, so user-provided package names are safe.
*
* Mirrors `execFileSync` failure semantics: throws the spawn error (e.g.
* ENOENT) as-is, or an Error carrying the child's exit `status` when it exits
* non-zero or is killed by a signal.
*/
export function runCommand(options: RunCommandOptions): void {
const { command, args = [], cwd } = options
const result = spawn.sync(command, args, { cwd, stdio: 'inherit' })
if (result.error) throw result.error
if (result.status !== 0) {
const reason =
result.status === null
? `was killed by signal ${result.signal}`
: `exited with code ${result.status}`
throw Object.assign(
new Error(`Command failed: ${command} ${args.join(' ')} ${reason}`),
{ status: result.status ?? 1, signal: result.signal },
)
}
}
11 changes: 6 additions & 5 deletions src/providers/bun.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { execFileSync } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import { detectFromPackageJson } from '../detect.ts'
import { runCommand } from '../exec.ts'
import {
readPackageItem,
resolveWorkspacePackages,
Expand Down Expand Up @@ -200,13 +200,13 @@ export function createBunProvider(cwd = process.cwd()): Provider {

// 3. Run bun install
log('Running bun install')
execFileSync('bun', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'bun', args: ['install'], cwd })

return Promise.resolve()
},

install() {
execFileSync('bun', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'bun', args: ['install'], cwd })
return Promise.resolve()
},

Expand All @@ -215,9 +215,10 @@ export function createBunProvider(cwd = process.cwd()): Provider {
const command = `bun ${args.join(' ')}`
options.logger?.(command)
if (options.execute !== false) {
execFileSync('bun', args, {
runCommand({
command: 'bun',
args,
cwd: options.cwd ?? cwd,
stdio: 'inherit',
})
}
return Promise.resolve(command)
Expand Down
11 changes: 6 additions & 5 deletions src/providers/npm.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { execFileSync } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import { detectFromPackageJson } from '../detect.ts'
import { runCommand } from '../exec.ts'
import {
readPackageItem,
resolveWorkspacePackages,
Expand Down Expand Up @@ -77,13 +77,13 @@ export function createNpmProvider(cwd = process.cwd()): Provider {
}

log('Running npm install')
execFileSync('npm', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'npm', args: ['install'], cwd })

return Promise.resolve()
},

install() {
execFileSync('npm', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'npm', args: ['install'], cwd })
return Promise.resolve()
},

Expand All @@ -97,9 +97,10 @@ export function createNpmProvider(cwd = process.cwd()): Provider {
const command = `npm ${args.join(' ')}`
options.logger?.(command)
if (options.execute !== false) {
execFileSync('npm', args, {
runCommand({
command: 'npm',
args,
cwd: options.cwd ?? cwd,
stdio: 'inherit',
})
}
return Promise.resolve(command)
Expand Down
11 changes: 6 additions & 5 deletions src/providers/pnpm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { execFileSync } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import { parseDocument } from 'yaml'
import { detectFromPackageJson } from '../detect.ts'
import { runCommand } from '../exec.ts'
import {
readPackageItem,
resolveWorkspacePackages,
Expand Down Expand Up @@ -138,13 +138,13 @@ export function createPnpmProvider(cwd = process.cwd()): Provider {

// 3. Run pnpm install
log('Running pnpm install')
execFileSync('pnpm', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'pnpm', args: ['install'], cwd })

return Promise.resolve()
},

install() {
execFileSync('pnpm', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'pnpm', args: ['install'], cwd })
return Promise.resolve()
},

Expand All @@ -153,9 +153,10 @@ export function createPnpmProvider(cwd = process.cwd()): Provider {
const command = `pnpm ${args.join(' ')}`
options.logger?.(command)
if (options.execute !== false) {
execFileSync('pnpm', args, {
runCommand({
command: 'pnpm',
args,
cwd: options.cwd ?? cwd,
stdio: 'inherit',
})
}
return Promise.resolve(command)
Expand Down
11 changes: 6 additions & 5 deletions src/providers/vlt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { execFileSync } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import { detectFromPackageJson } from '../detect.ts'
import { runCommand } from '../exec.ts'
import {
readPackageItem,
resolveWorkspacePackages,
Expand Down Expand Up @@ -164,13 +164,13 @@ export function createVltProvider(cwd = process.cwd()): Provider {

// 3. Run vlt install
log('Running vlt install')
execFileSync('vlt', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'vlt', args: ['install'], cwd })

return Promise.resolve()
},

install() {
execFileSync('vlt', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'vlt', args: ['install'], cwd })
return Promise.resolve()
},

Expand All @@ -179,9 +179,10 @@ export function createVltProvider(cwd = process.cwd()): Provider {
const command = `vlt ${args.join(' ')}`
options.logger?.(command)
if (options.execute !== false) {
execFileSync('vlt', args, {
runCommand({
command: 'vlt',
args,
cwd: options.cwd ?? cwd,
stdio: 'inherit',
})
}
return Promise.resolve(command)
Expand Down
11 changes: 6 additions & 5 deletions src/providers/yarn.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { execFileSync } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import { parseDocument } from 'yaml'
import { detectFromPackageJson } from '../detect.ts'
import { runCommand } from '../exec.ts'
import {
readPackageItem,
resolveWorkspacePackages,
Expand Down Expand Up @@ -133,13 +133,13 @@ export function createYarnProvider(cwd = process.cwd()): Provider {

// 3. Run yarn install
log('Running yarn install')
execFileSync('yarn', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'yarn', args: ['install'], cwd })

return Promise.resolve()
},

install() {
execFileSync('yarn', ['install'], { cwd, stdio: 'inherit' })
runCommand({ command: 'yarn', args: ['install'], cwd })
return Promise.resolve()
},

Expand All @@ -148,9 +148,10 @@ export function createYarnProvider(cwd = process.cwd()): Provider {
const command = `yarn ${args.join(' ')}`
options.logger?.(command)
if (options.execute !== false) {
execFileSync('yarn', args, {
runCommand({
command: 'yarn',
args,
cwd: options.cwd ?? cwd,
stdio: 'inherit',
})
}
return Promise.resolve(command)
Expand Down
80 changes: 80 additions & 0 deletions tests/exec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import spawn from 'cross-spawn'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { runCommand } from '../src/exec.ts'
import { createBunProvider } from '../src/providers/bun.ts'
import { createNpmProvider } from '../src/providers/npm.ts'
import { createPnpmProvider } from '../src/providers/pnpm.ts'
import { createVltProvider } from '../src/providers/vlt.ts'
import { createYarnProvider } from '../src/providers/yarn.ts'

vi.mock('cross-spawn', () => ({
default: { sync: vi.fn() },
}))

const mockedSync = vi.mocked(spawn.sync)

type SyncResult = ReturnType<typeof spawn.sync>

function result(
partial: Partial<Pick<SyncResult, 'status' | 'signal' | 'error'>>,
): SyncResult {
const value: Partial<SyncResult> = {
pid: 1,
status: 0,
signal: null,
...partial,
}
return value as SyncResult
}

beforeEach(() => {
mockedSync.mockReset()
mockedSync.mockReturnValue(result({}))
})

describe('runCommand', () => {
it('spawns through cross-spawn so Windows .cmd shims resolve', () => {
runCommand({ command: 'pnpm', args: ['install'], cwd: '/repo' })
expect(mockedSync).toHaveBeenCalledWith('pnpm', ['install'], {
cwd: '/repo',
stdio: 'inherit',
})
})

it('throws with the exit status when the command fails', () => {
mockedSync.mockReturnValue(result({ status: 2 }))
expect(() =>
runCommand({ command: 'pnpm', args: ['install'], cwd: '/repo' }),
).toThrow(expect.objectContaining({ status: 2 }))
})

it('rethrows spawn errors such as ENOENT', () => {
const error = Object.assign(new Error('spawnSync pnpm ENOENT'), {
code: 'ENOENT',
})
mockedSync.mockReturnValue(result({ status: null, error }))
expect(() =>
runCommand({ command: 'pnpm', args: ['install'], cwd: '/repo' }),
).toThrow('spawnSync pnpm ENOENT')
})
})

describe('provider install', () => {
it.each([
['pnpm', createPnpmProvider],
['npm', createNpmProvider],
['yarn', createYarnProvider],
['bun', createBunProvider],
['vlt', createVltProvider],
] as const)(
'%s install uses the cross-platform spawner',
async (bin, create) => {
await create('/workspace').install()
expect(mockedSync).toHaveBeenCalledWith(
bin,
['install'],
expect.objectContaining({ cwd: '/workspace', stdio: 'inherit' }),
)
},
)
})
Loading
Loading