Skip to content
Closed
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
6 changes: 3 additions & 3 deletions node-src/git/execGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function execGitCommand(
) {
try {
log.debug(`execGitCommand: ${command}`);
const { all } = await runCommand(command, { ...defaultOptions, ...options });
const { all } = await runCommand({ log }, command, { ...defaultOptions, ...options });

if (all === undefined) {
throw new Error(`Unexpected missing git command output for command: '${command}'`);
Expand Down Expand Up @@ -78,7 +78,7 @@ export async function execGitCommandOneLine(
options?: Options
) {
log.debug(`execGitCommandOneLine: ${command}`);
const process = runCommand(command, {
const process = runCommand({ log }, command, {
...defaultOptions,
buffer: false,
...options,
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function execGitCommandCountLines(
options?: Options
) {
log.debug(`execGitCommandCountLines: ${command}`);
const process = runCommand(command, {
const process = runCommand({ log }, command, {
...defaultOptions,
buffer: false,
...options,
Expand Down
8 changes: 6 additions & 2 deletions node-src/lib/getPackageManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getCliCommand, parseNa, parseNr } from '@antfu/ni';

import { Context } from '../types';
import { runCommand } from './shell/shell';

// 'npm' | 'pnpm' | 'yarn' | 'bun'
Expand All @@ -13,13 +14,16 @@ export const getPackageManagerRunCommand = async (args: string[]) => {
};

// e.g. `8.19.2`
export const getPackageManagerVersion = async (packageManager: string) => {
export const getPackageManagerVersion = async (
{ log }: Pick<Context, 'log'>,
packageManager: string
) => {
if (!packageManager) {
throw new Error('No package manager provided');
}

const command = `${packageManager} --version`;
const { stdout } = await runCommand(command);
const { stdout } = await runCommand({ log }, command);

if (stdout === undefined) {
throw new Error(`Unexpected missing output for command: '${command}'`);
Expand Down
18 changes: 16 additions & 2 deletions node-src/lib/shell/shell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { execa, Options, parseCommandString, type ResultPromise } from 'execa';
import { kill } from 'process';
import treeKill from 'tree-kill';

import { Context } from '../../types';

/**
* Run a command in the shell. This is a wrapper around `execa` so .kill() and timeouts kill the
* entire process tree instead of just the first child.
Expand All @@ -9,12 +12,18 @@ import treeKill from 'tree-kill';
* 1. Buffered mode: await the result to get stdout/stderr after completion
* 2. Streaming mode: access .stdout/.stderr properties during execution (requires buffer: false)
*
* @param context Standard context object.
* @param context.log Standard context logger.
* @param command The command to run.
* @param options Execa options. Note: `timeout` is handled internally.
*
* @returns An execa `ResultPromise` with .kill() and timeout overwritten.
*/
export function runCommand(command: string, options: Options = {}): ResultPromise {
export function runCommand(
{ log }: Pick<Context, 'log'>,
command: string,
options: Options = {}
): ResultPromise {
const { timeout, ...optionsWithoutTimeout } = options;
const [cmd, ...args] = parseCommandString(command);
const subprocess = execa(cmd, args, optionsWithoutTimeout);
Expand All @@ -26,7 +35,12 @@ export function runCommand(command: string, options: Options = {}): ResultPromis
return false;
}

treeKill(subprocess.pid);
try {
treeKill(subprocess.pid);
} catch (err) {
log.warn(`Failed to terminate process tree for ${subprocess.pid}: ${err}`);
kill(subprocess.pid);
}
return true;
};

Expand Down
2 changes: 1 addition & 1 deletion node-src/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const buildStorybook = async (ctx: Context) => {
throw new Error('No build command configured');
}

await runCommand(ctx.buildCommand, {
await runCommand(ctx, ctx.buildCommand, {
stdio: [undefined, logFile, undefined],
// When `true`, this will run in the node version set by the
// action (node20), not the version set in the workflow
Expand Down
2 changes: 1 addition & 1 deletion node-src/tasks/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const setRuntimeMetadata = async (ctx: Context) => {
ctx.runtimeMetadata.packageManager = packageManager as any;
Sentry.setTag('packageManager', packageManager);

const packageManagerVersion = await getPackageManagerVersion(packageManager);
const packageManagerVersion = await getPackageManagerVersion(ctx, packageManager);
ctx.runtimeMetadata.packageManagerVersion = packageManagerVersion;
Sentry.setTag('packageManagerVersion', packageManagerVersion);
} catch (err) {
Expand Down
Loading