Skip to content

Commit 2f960b3

Browse files
jamietannaClaude Sonnet 4.5
andcommitted
refactor(tools): migrate to execa
For consistency, we should use `execa` for all of our command execution. Co-authored-by: Claude Sonnet 4.5 <jamie.tanna+github-copilot@mend.io>
1 parent 536d877 commit 2f960b3

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

tools/mkdocs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { SpawnSyncReturns } from 'child_process';
21
import { Command } from 'commander';
2+
import type { ExecaSyncReturnValue } from 'execa';
33
import fs from 'fs-extra';
44
import { logger } from '../lib/logger/index.ts';
55
import { generateDocs } from './docs/index.ts';
@@ -68,13 +68,13 @@ async function prepareDocs(opts: any): Promise<void> {
6868
}
6969
}
7070

71-
function checkResult(res: SpawnSyncReturns<string>): void {
71+
function checkResult(res: ExecaSyncReturnValue<string>): void {
7272
if (res.signal) {
7373
logger.error(`Signal received: ${res.signal}`);
7474
process.exit(-1);
75-
} else if (res.status && res.status !== 0) {
75+
} else if (res.exitCode) {
7676
logger.error(`Error occured:\n${res.stderr || res.stdout}`);
77-
process.exit(res.status);
77+
process.exit(res.exitCode);
7878
} else {
7979
logger.debug(`Build completed:\n${res.stdout || res.stderr}`);
8080
}

tools/prepare-deps.mjs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
import { execSync } from 'child_process';
1+
import { execaSync } from 'execa';
22

33
function testRe2() {
4-
execSync(
5-
`node -e "try{require('re2')('.*').exec('test')}catch(e){console.error(e);if(e.code === 'ERR_DLOPEN_FAILED' && e.message.includes('NODE_MODULE_VERSION')) process.exit(1); else process.exit(-1)}"`,
4+
execaSync(
5+
'node',
6+
[
7+
'-e',
8+
`try{require('re2')('.*').exec('test')}catch(e){console.error(e);if(e.code === 'ERR_DLOPEN_FAILED' && e.message.includes('NODE_MODULE_VERSION')) process.exit(1); else process.exit(-1)}`,
9+
],
610
{ stdio: 'inherit' },
711
);
812
console.log(`Ok.`);
913
}
1014

1115
function testSqlite() {
12-
execSync(
13-
`node -e "try{new require('better-sqlite3')(':memory:')}catch(e){console.error(e);if(e.code === 'ERR_DLOPEN_FAILED' && e.message.includes('NODE_MODULE_VERSION')) process.exit(1); else process.exit(-1)}"`,
16+
execaSync(
17+
'node',
18+
[
19+
'-e',
20+
`try{new require('better-sqlite3')(':memory:')}catch(e){console.error(e);if(e.code === 'ERR_DLOPEN_FAILED' && e.message.includes('NODE_MODULE_VERSION')) process.exit(1); else process.exit(-1)}`,
21+
],
1422
{ stdio: 'inherit' },
1523
);
1624
console.log(`Ok.`);
@@ -23,9 +31,9 @@ function testSqlite() {
2331
} catch (e) {
2432
console.error(`Failed.\n${e}`);
2533
try {
26-
if (e.status === 1) {
34+
if (e.exitCode === 1) {
2735
console.log(`Retry re2 install ...`);
28-
execSync('pnpm rb re2', {
36+
execaSync('pnpm', ['rb', 're2'], {
2937
stdio: 'inherit',
3038
});
3139
testRe2();
@@ -46,9 +54,9 @@ function testSqlite() {
4654
} catch (e) {
4755
console.error(`Failed.\n${e}`);
4856
try {
49-
if (e.status === 1) {
57+
if (e.exitCode === 1) {
5058
console.log(`Retry better-sqlite3 install ...`);
51-
execSync('pnpm rb better-sqlite3', {
59+
execaSync('pnpm', ['rb', 'better-sqlite3'], {
5260
stdio: 'inherit',
5361
});
5462
testSqlite();

tools/prepare-release.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ function build(): void {
4343
if (res.signal) {
4444
logger.error(`Signal received: ${res.signal}`);
4545
process.exit(-1);
46-
} else if (res.status && res.status !== 0) {
46+
} else if (res.exitCode) {
4747
logger.error(`Error occured:\n${res.stderr || res.stdout}`);
48-
process.exit(res.status);
48+
process.exit(res.exitCode);
4949
} else {
5050
logger.debug(`Build succeeded:\n${res.stdout || res.stderr}`);
5151
}

tools/utils/docker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function bake(
7070
if (result.signal) {
7171
logger.error(`Signal received: ${result.signal}`);
7272
process.exit(-1);
73-
} else if (result.status && result.status !== 0) {
73+
} else if (result.exitCode) {
7474
if (tries > 0) {
7575
logger.debug(`Error occured:\n ${result.stderr}`);
7676
const delay = opts.delay ? toMs(opts.delay) : null;
@@ -81,7 +81,7 @@ export async function bake(
8181
} else {
8282
logger.error(`Error occured:\n${result.stderr}`);
8383
if (opts.exitOnError !== false) {
84-
process.exit(result.status);
84+
process.exit(result.exitCode);
8585
}
8686
return null;
8787
}
@@ -109,10 +109,10 @@ export function sign(
109109
if (result.signal) {
110110
logger.error(`Signal received: ${result.signal}`);
111111
process.exit(-1);
112-
} else if (result.status && result.status !== 0) {
112+
} else if (result.exitCode) {
113113
logger.error(`Error occured:\n${result.stderr}`);
114114
if (opts.exitOnError !== false) {
115-
process.exit(result.status);
115+
process.exit(result.exitCode);
116116
}
117117
} else {
118118
logger.debug(`Succeeded:\n${result.stdout || result.stderr}`);

tools/utils/exec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import {
2-
type SpawnSyncOptions,
3-
type SpawnSyncReturns,
4-
spawnSync,
5-
} from 'node:child_process';
1+
import type { ExecaSyncReturnValue, SyncOptions } from 'execa';
2+
import { execaSync } from 'execa';
63

74
const maxBuffer = 20 * 1024 * 1024;
85

96
/**
10-
* Execute a command
7+
* Execute a command synchronously using execa
118
* @param {string} cmd
129
* @param {string[]} args
1310
*/
1411
export function exec(
1512
cmd: string,
1613
args: string[] = [],
17-
opts: SpawnSyncOptions = {},
18-
): SpawnSyncReturns<string> {
19-
// args from shelljs
20-
return spawnSync(cmd, args, { ...opts, maxBuffer, encoding: 'utf8' });
14+
opts: SyncOptions = {},
15+
): ExecaSyncReturnValue<string> {
16+
return execaSync(cmd, args, {
17+
...opts,
18+
maxBuffer,
19+
encoding: 'utf8',
20+
});
2121
}

0 commit comments

Comments
 (0)