Skip to content

Commit 52bcc32

Browse files
resources: replace execSync with spawnSync (#3704)
1 parent 9a494d9 commit 52bcc32

File tree

5 files changed

+54
-30
lines changed

5 files changed

+54
-30
lines changed

integrationTests/integration-test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import * as path from 'node:path';
55

66
import { describe, it } from 'mocha';
77

8-
function exec(command: string, options = {}) {
9-
const output = childProcess.execSync(command, {
8+
function npm(args: ReadonlyArray<string>, options = {}): string {
9+
const result = childProcess.spawnSync('npm', [...args], {
10+
maxBuffer: 10 * 1024 * 1024, // 10MB
11+
stdio: ['inherit', 'pipe', 'inherit'],
1012
encoding: 'utf-8',
1113
...options,
1214
});
13-
return output?.trimEnd();
15+
return result.stdout.toString().trimEnd();
1416
}
1517

1618
describe('Integration Tests', () => {
@@ -19,7 +21,7 @@ describe('Integration Tests', () => {
1921
fs.mkdirSync(tmpDir);
2022

2123
const distDir = path.resolve('./npmDist');
22-
const archiveName = exec(`npm --quiet pack ${distDir}`, { cwd: tmpDir });
24+
const archiveName = npm(['--quiet', 'pack', distDir], { cwd: tmpDir });
2325
fs.renameSync(
2426
path.join(tmpDir, archiveName),
2527
path.join(tmpDir, 'graphql.tgz'),
@@ -38,8 +40,8 @@ describe('Integration Tests', () => {
3840

3941
const cwd = path.join(tmpDir, projectName);
4042
// TODO: figure out a way to run it with --ignore-scripts
41-
exec('npm --quiet install', { cwd, stdio: 'inherit' });
42-
exec('npm --quiet test', { cwd, stdio: 'inherit' });
43+
npm(['--quiet', 'install'], { cwd });
44+
npm(['--quiet', 'test'], { cwd });
4345
}).timeout(120000);
4446
}
4547

resources/benchmark.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as os from 'node:os';
55
import * as path from 'node:path';
66
import * as url from 'node:url';
77

8-
import { exec, localRepoPath } from './utils';
8+
import { git, localRepoPath, npm } from './utils';
99

1010
const NS_PER_SEC = 1e9;
1111
const LOCAL = 'local';
@@ -63,7 +63,7 @@ function prepareBenchmarkProjects(
6363
path.join(projectPath, 'package.json'),
6464
JSON.stringify(packageJSON, null, 2),
6565
);
66-
exec('npm --quiet install --ignore-scripts ', { cwd: projectPath });
66+
npm(['--quiet', 'install', '--ignore-scripts'], { cwd: projectPath });
6767

6868
return { revision, projectPath };
6969
});
@@ -77,7 +77,7 @@ function prepareBenchmarkProjects(
7777
}
7878

7979
// Returns the complete git hash for a given git revision reference.
80-
const hash = exec(`git rev-parse "${revision}"`);
80+
const hash = git(['rev-parse', revision]);
8181

8282
const archivePath = path.join(tmpDir, `graphql-${hash}.tgz`);
8383
if (fs.existsSync(archivePath)) {
@@ -87,19 +87,19 @@ function prepareBenchmarkProjects(
8787
const repoDir = path.join(tmpDir, hash);
8888
fs.rmSync(repoDir, { recursive: true, force: true });
8989
fs.mkdirSync(repoDir);
90-
exec(`git clone --quiet "${localRepoPath()}" "${repoDir}"`);
91-
exec(`git checkout --quiet --detach "${hash}"`, { cwd: repoDir });
92-
exec('npm --quiet ci --ignore-scripts', { cwd: repoDir });
90+
git(['clone', '--quiet', localRepoPath(), repoDir]);
91+
git(['checkout', '--quiet', '--detach', hash], { cwd: repoDir });
92+
npm(['--quiet', 'ci', '--ignore-scripts'], { cwd: repoDir });
9393
fs.renameSync(buildNPMArchive(repoDir), archivePath);
9494
fs.rmSync(repoDir, { recursive: true });
9595
return archivePath;
9696
}
9797

9898
function buildNPMArchive(repoDir: string) {
99-
exec('npm --quiet run build:npm', { cwd: repoDir });
99+
npm(['--quiet', 'run', 'build:npm'], { cwd: repoDir });
100100

101101
const distDir = path.join(repoDir, 'npmDist');
102-
const archiveName = exec(`npm --quiet pack ${distDir}`, {
102+
const archiveName = npm(['--quiet', 'pack', distDir], {
103103
cwd: repoDir,
104104
});
105105
return path.join(repoDir, archiveName);

resources/diff-npm-package.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as assert from 'node:assert';
2+
import * as childProcess from 'node:child_process';
23
import * as fs from 'node:fs';
34
import * as os from 'node:os';
45
import * as path from 'node:path';
56

6-
import { exec, localRepoPath, writeGeneratedFile } from './utils';
7+
import { git, localRepoPath, npm, writeGeneratedFile } from './utils';
78

89
const LOCAL = 'local';
910
const tmpDir = path.join(os.tmpdir(), 'graphql-js-npm-diff');
@@ -27,7 +28,7 @@ console.log(`📦 Building NPM package for ${toRevision}...`);
2728
const toPackage = prepareNPMPackage(toRevision);
2829

2930
console.log('➖➕ Generating diff...');
30-
const diff = exec(`npm diff --diff=${fromPackage} --diff=${toPackage}`);
31+
const diff = npm(['diff', '--diff', fromPackage, '--diff', toPackage]);
3132

3233
if (diff === '') {
3334
console.log('No changes found!');
@@ -78,19 +79,19 @@ function generateReport(diffString: string): string {
7879

7980
function prepareNPMPackage(revision: string): string {
8081
if (revision === LOCAL) {
81-
exec('npm --quiet run build:npm', { cwd: localRepoPath() });
82+
npm(['--quiet', 'run', 'build:npm'], { cwd: localRepoPath() });
8283
return localRepoPath('npmDist');
8384
}
8485

8586
// Returns the complete git hash for a given git revision reference.
86-
const hash = exec(`git rev-parse "${revision}"`);
87+
const hash = git(['rev-parse', revision]);
8788
assert(hash != null);
8889

8990
const repoDir = path.join(tmpDir, hash);
9091
fs.rmSync(repoDir, { recursive: true, force: true });
9192
fs.mkdirSync(repoDir);
92-
exec(`git archive "${hash}" | tar -xC "${repoDir}"`);
93-
exec('npm --quiet ci --ignore-scripts', { cwd: repoDir });
94-
exec('npm --quiet run build:npm', { cwd: repoDir });
93+
childProcess.execSync(`git archive "${hash}" | tar -xC "${repoDir}"`);
94+
npm(['--quiet', 'ci', '--ignore-scripts'], { cwd: repoDir });
95+
npm(['--quiet', 'run', 'build:npm'], { cwd: repoDir });
9596
return path.join(repoDir, 'npmDist');
9697
}

resources/gen-changelog.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exec, readPackageJSON } from './utils';
1+
import { git, readPackageJSON } from './utils';
22

33
const packageJSON = readPackageJSON();
44
const labelsConfig: { [label: string]: { section: string; fold?: boolean } } = {
@@ -64,15 +64,15 @@ function getChangeLog(): Promise<string> {
6464
const { version } = packageJSON;
6565

6666
let tag: string | null = null;
67-
let commitsList = exec(`git rev-list --reverse v${version}..`);
67+
let commitsList = git(['rev-list', '--reverse', `v${version}..`]);
6868
if (commitsList === '') {
69-
const parentPackageJSON = exec('git cat-file blob HEAD~1:package.json');
69+
const parentPackageJSON = git(['cat-file', 'blob', 'HEAD~1:package.json']);
7070
const parentVersion = JSON.parse(parentPackageJSON).version;
71-
commitsList = exec(`git rev-list --reverse v${parentVersion}..HEAD~1`);
71+
commitsList = git(['rev-list', '--reverse', `v${parentVersion}..HEAD~1`]);
7272
tag = `v${version}`;
7373
}
7474

75-
const date = exec('git log -1 --format=%cd --date=short');
75+
const date = git(['log', '-1', '--format=%cd', '--date=short']);
7676
return getCommitsInfo(commitsList.split('\n'))
7777
.then((commitsInfo) => getPRsInfo(commitsInfoToPRs(commitsInfo)))
7878
.then((prsInfo) => genChangeLog(tag, date, prsInfo));

resources/utils.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,36 @@ export function localRepoPath(...paths: ReadonlyArray<string>): string {
99
return path.join(__dirname, '..', ...paths);
1010
}
1111

12-
type ExecOptions = Parameters<typeof childProcess.execSync>[1];
13-
export function exec(command: string, options?: ExecOptions): string {
14-
const output = childProcess.execSync(command, {
12+
export function npm(
13+
args: ReadonlyArray<string>,
14+
options?: SpawnOptions,
15+
): string {
16+
return spawn('npm', args, options);
17+
}
18+
19+
export function git(
20+
args: ReadonlyArray<string>,
21+
options?: SpawnOptions,
22+
): string {
23+
return spawn('git', args, options);
24+
}
25+
26+
interface SpawnOptions {
27+
cwd?: string;
28+
}
29+
30+
function spawn(
31+
command: string,
32+
args: ReadonlyArray<string>,
33+
options?: SpawnOptions,
34+
): string {
35+
const result = childProcess.spawnSync(command, args, {
1536
maxBuffer: 10 * 1024 * 1024, // 10MB
1637
stdio: ['inherit', 'pipe', 'inherit'],
1738
encoding: 'utf-8',
1839
...options,
1940
});
20-
return output.toString().trimEnd();
41+
return result.stdout.toString().trimEnd();
2142
}
2243

2344
export function readdirRecursive(

0 commit comments

Comments
 (0)