Skip to content

Commit

Permalink
fix: show kill command
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Oct 4, 2023
1 parent 20be42c commit c00a6c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 10 additions & 6 deletions packages/wb/src/scripts/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function runWithSpawn(
printStart(printableScript, 'Start (detailed)', true);
}
if (argv.dryRun) {
finishedScript(printableScript, 0, opts);
printFinishedAndExitIfNeeded(printableScript, 0, opts);
return 0;
}

Expand All @@ -46,7 +46,7 @@ export async function runWithSpawn(
killOnExit: true,
verbose: argv.verbose,
});
finishedScript(printableScript, ret.status, opts);
printFinishedAndExitIfNeeded(printableScript, ret.status, opts);
return ret.status ?? 1;
}

Expand Down Expand Up @@ -80,7 +80,7 @@ export function runWithSpawnInParallel(
printStart(printableScript, 'Start (parallel)', true);
if (argv.dryRun) {
printStart(printableScript, 'Start (log)');
finishedScript(printableScript, 0, opts);
printFinishedAndExitIfNeeded(printableScript, 0, opts);
return;
}

Expand All @@ -96,7 +96,7 @@ export function runWithSpawnInParallel(
printStart(printableScript, 'Start (log)');
const out = ret.stdout.trim();
if (out) console.info(out);
finishedScript(printableScript, ret.status, opts);
printFinishedAndExitIfNeeded(printableScript, ret.status, opts);
});
}

Expand All @@ -110,15 +110,19 @@ function normalizeScript(script: string): [string, string] {
return [newScript.replaceAll('YARN ', 'yarn '), newScript.replaceAll('YARN ', binExists ? '' : 'yarn ')];
}

function printStart(normalizedScript: string, prefix = 'Start', weak = false): void {
export function printStart(normalizedScript: string, prefix = 'Start', weak = false): void {
console.info(
'\n' +
(weak ? chalk.gray : chalk.cyan)(chalk.bold(`${prefix}:`), normalizedScript) +
chalk.gray(` at ${project.dirPath}`)
);
}

function finishedScript(script: string, exitCode: number | null, opts: Omit<Options, 'timeout'>): void {
export function printFinishedAndExitIfNeeded(
script: string,
exitCode: number | null,
opts: Omit<Options, 'timeout'>
): void {
if (exitCode === 0) {
console.info(chalk.green(chalk.bold('Finished:'), script));
} else {
Expand Down
17 changes: 12 additions & 5 deletions packages/wb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { spawnSync } from 'node:child_process';

import killPortProcess from 'kill-port';

import { project } from './project.js';
import { printFinishedAndExitIfNeeded, printStart } from './scripts/run.js';

const killed = new Set<number | string>();

export async function killPortProcessImmediatelyAndOnExit(port: number): Promise<void> {
Expand All @@ -12,8 +15,9 @@ export async function killPortProcessImmediatelyAndOnExit(port: number): Promise
killed.add(port);
await killPortProcessHandlingErrors(port);
};
process.on('beforeExit', killFunc);
process.on('SIGINT', killFunc);
for (const signal of ['beforeExit', 'SIGINT', 'SIGTERM', 'SIGQUIT']) {
process.on(signal, killFunc);
}
}

async function killPortProcessHandlingErrors(port: number): Promise<void> {
Expand All @@ -29,8 +33,11 @@ export function spawnSyncOnExit(command: string): void {
if (killed.has(command)) return;

killed.add(command);
spawnSync(command, { shell: true, stdio: 'inherit' });
printStart(command, 'Start', true);
const { status } = spawnSync(command, { cwd: project.dirPath, shell: true, stdio: 'inherit' });
printFinishedAndExitIfNeeded(command, status, {});
};
process.on('beforeExit', killFunc);
process.on('SIGINT', killFunc);
for (const signal of ['beforeExit', 'SIGINT', 'SIGTERM', 'SIGQUIT']) {
process.on(signal, killFunc);
}
}

0 comments on commit c00a6c0

Please sign in to comment.