Skip to content

Commit 80c44e5

Browse files
resources/utils.ts: npm/git will inherit stdout by default (#3724)
1 parent b850a2f commit 80c44e5

File tree

6 files changed

+87
-41
lines changed

6 files changed

+87
-41
lines changed

resources/benchmark.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function prepareBenchmarkProjects(
5757
path.join(projectPath, 'package.json'),
5858
JSON.stringify(packageJSON, null, 2),
5959
);
60-
npm(['--quiet', 'install', '--ignore-scripts'], { cwd: projectPath });
60+
npm({ cwd: projectPath, quiet: true }).install('--ignore-scripts');
6161

6262
return { revision, projectPath };
6363
});
@@ -71,7 +71,7 @@ function prepareBenchmarkProjects(
7171
}
7272

7373
// Returns the complete git hash for a given git revision reference.
74-
const hash = git(['rev-parse', revision]);
74+
const hash = git().revParse(revision);
7575

7676
const archivePath = tmpDirPath(`graphql-${hash}.tgz`);
7777
if (fs.existsSync(archivePath)) {
@@ -81,21 +81,19 @@ function prepareBenchmarkProjects(
8181
const repoDir = tmpDirPath(hash);
8282
fs.rmSync(repoDir, { recursive: true, force: true });
8383
fs.mkdirSync(repoDir);
84-
git(['clone', '--quiet', localRepoPath(), repoDir]);
85-
git(['checkout', '--quiet', '--detach', hash], { cwd: repoDir });
86-
npm(['--quiet', 'ci', '--ignore-scripts'], { cwd: repoDir });
84+
git({ quiet: true }).clone(localRepoPath(), repoDir);
85+
git({ cwd: repoDir, quiet: true }).checkout('--detach', hash);
86+
npm({ cwd: repoDir, quiet: true }).ci('--ignore-scripts');
8787
fs.renameSync(buildNPMArchive(repoDir), archivePath);
8888
fs.rmSync(repoDir, { recursive: true });
8989
return archivePath;
9090
}
9191

9292
function buildNPMArchive(repoDir: string) {
93-
npm(['--quiet', 'run', 'build:npm'], { cwd: repoDir });
93+
npm({ cwd: repoDir, quiet: true }).run('build:npm');
9494

9595
const distDir = path.join(repoDir, 'npmDist');
96-
const archiveName = npm(['--quiet', 'pack', distDir], {
97-
cwd: repoDir,
98-
});
96+
const archiveName = npm({ cwd: repoDir, quiet: true }).pack(distDir);
9997
return path.join(repoDir, archiveName);
10098
}
10199
}

resources/build-docusaurus.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@ copyToTmpDir('tsconfig.json');
1717
copyToTmpDir('src');
1818
copyToTmpDir('website');
1919

20-
npm(['install', 'ci'], { cwd: tmpDirPath() });
20+
npm({ cwd: tmpDirPath(), quiet: true }).ci('--ignore-scripts');
2121

2222
const env = {
2323
...process.env,
2424
DOCUSAURUS_GENERATED_FILES_DIR_NAME: tmpDirPath('.docusaurus'),
2525
};
26-
const docusaurusArgs = [
26+
npm({ env, cwd: tmpDirPath() }).exec(
27+
'docusaurus',
28+
'--',
2729
'build',
2830
'--out-dir',
2931
localRepoPath('websiteDist'),
3032
tmpDirPath('website'),
31-
];
32-
npm(['exec', 'docusaurus', '--', ...docusaurusArgs], {
33-
env,
34-
cwd: tmpDirPath(),
35-
});
33+
);
3634

3735
function copyToTmpDir(relativePath: string) {
3836
fs.cpSync(localRepoPath(relativePath), tmpDirPath(relativePath), {

resources/diff-npm-package.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ console.log(`📦 Building NPM package for ${toRevision}...`);
3131
const toPackage = prepareNPMPackage(toRevision);
3232

3333
console.log('➖➕ Generating diff...');
34-
const diff = npm(['diff', '--diff', fromPackage, '--diff', toPackage]);
34+
const diff = npm().diff('--diff', fromPackage, '--diff', toPackage);
3535

3636
if (diff === '') {
3737
console.log('No changes found!');
@@ -82,19 +82,19 @@ function generateReport(diffString: string): string {
8282

8383
function prepareNPMPackage(revision: string): string {
8484
if (revision === LOCAL) {
85-
npm(['--quiet', 'run', 'build:npm'], { cwd: localRepoPath() });
85+
npm({ cwd: localRepoPath(), quiet: true }).run('build:npm');
8686
return localRepoPath('npmDist');
8787
}
8888

8989
// Returns the complete git hash for a given git revision reference.
90-
const hash = git(['rev-parse', revision]);
90+
const hash = git().revParse(revision);
9191
assert(hash != null);
9292

9393
const repoDir = tmpDirPath(hash);
9494
fs.rmSync(repoDir, { recursive: true, force: true });
9595
fs.mkdirSync(repoDir);
9696
childProcess.execSync(`git archive "${hash}" | tar -xC "${repoDir}"`);
97-
npm(['--quiet', 'ci', '--ignore-scripts'], { cwd: repoDir });
98-
npm(['--quiet', 'run', 'build:npm'], { cwd: repoDir });
97+
npm({ cwd: repoDir, quiet: true }).ci('--ignore-scripts');
98+
npm({ cwd: repoDir, quiet: true }).run('build:npm');
9999
return path.join(repoDir, 'npmDist');
100100
}

resources/gen-changelog.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ function getChangeLog(): Promise<string> {
6464
const { version } = packageJSON;
6565

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

75-
const date = 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/integration-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ describe('Integration Tests', () => {
1010
recursive: true,
1111
});
1212

13-
npm(['run', 'build:npm']);
13+
npm().run('build:npm');
1414
const distDir = localRepoPath('npmDist');
15-
const archiveName = npm(['--quiet', 'pack', distDir], { cwd: tmpDirPath() });
15+
const archiveName = npm({ cwd: tmpDirPath(), quiet: true }).pack(distDir);
1616
fs.renameSync(tmpDirPath(archiveName), tmpDirPath('graphql.tgz'));
1717

18-
npm(['run', 'build:deno']);
18+
npm().run('build:deno');
1919

2020
function testOnNodeProject(projectName: string) {
2121
const projectPath = tmpDirPath(projectName);
2222
const packageJSON = readPackageJSON(projectPath);
2323

2424
it(packageJSON.description, () => {
2525
// TODO: figure out a way to run it with --ignore-scripts
26-
npm(['--quiet', 'install'], { cwd: projectPath });
27-
npm(['--quiet', 'test'], { cwd: projectPath });
26+
npm({ cwd: projectPath, quiet: true }).install();
27+
npm({ cwd: projectPath, quiet: true }).run('test');
2828
}).timeout(120000);
2929
}
3030

resources/utils.ts

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,68 @@ export function makeTmpDir(name: string): MakeTmpDirReturn {
2525
};
2626
}
2727

28-
export function npm(
29-
args: ReadonlyArray<string>,
30-
options?: SpawnOptions,
31-
): string {
32-
return spawn('npm', args, options);
28+
interface NPMOptions extends SpawnOptions {
29+
quiet?: boolean;
3330
}
3431

35-
export function git(
36-
args: ReadonlyArray<string>,
37-
options?: SpawnOptions,
38-
): string {
39-
return spawn('git', args, options);
32+
export function npm(options?: NPMOptions) {
33+
const globalOptions = options?.quiet === true ? ['--quiet'] : [];
34+
return {
35+
run(...args: ReadonlyArray<string>): void {
36+
spawn('npm', [...globalOptions, 'run', ...args], options);
37+
},
38+
install(...args: ReadonlyArray<string>): void {
39+
spawn('npm', [...globalOptions, 'install', ...args], options);
40+
},
41+
ci(...args: ReadonlyArray<string>): void {
42+
spawn('npm', [...globalOptions, 'ci', ...args], options);
43+
},
44+
exec(...args: ReadonlyArray<string>): void {
45+
spawn('npm', [...globalOptions, 'exec', ...args], options);
46+
},
47+
pack(...args: ReadonlyArray<string>): string {
48+
return spawnOutput('npm', [...globalOptions, 'pack', ...args], options);
49+
},
50+
diff(...args: ReadonlyArray<string>): string {
51+
return spawnOutput('npm', [...globalOptions, 'diff', ...args], options);
52+
},
53+
};
54+
}
55+
56+
interface GITOptions extends SpawnOptions {
57+
quiet?: boolean;
58+
}
59+
60+
export function git(options?: GITOptions) {
61+
const cmdOptions = options?.quiet === true ? ['--quiet'] : [];
62+
return {
63+
clone(...args: ReadonlyArray<string>): void {
64+
spawn('git', ['clone', ...cmdOptions, ...args], options);
65+
},
66+
checkout(...args: ReadonlyArray<string>): void {
67+
spawn('git', ['checkout', ...cmdOptions, ...args], options);
68+
},
69+
revParse(...args: ReadonlyArray<string>): string {
70+
return spawnOutput('git', ['rev-parse', ...cmdOptions, ...args], options);
71+
},
72+
revList(...args: ReadonlyArray<string>): string {
73+
return spawnOutput('git', ['rev-list', ...cmdOptions, ...args], options);
74+
},
75+
catFile(...args: ReadonlyArray<string>): string {
76+
return spawnOutput('git', ['cat-file', ...cmdOptions, ...args], options);
77+
},
78+
log(...args: ReadonlyArray<string>): string {
79+
return spawnOutput('git', ['log', ...cmdOptions, ...args], options);
80+
},
81+
};
4082
}
4183

4284
interface SpawnOptions {
4385
cwd?: string;
4486
env?: typeof process.env;
4587
}
4688

47-
function spawn(
89+
function spawnOutput(
4890
command: string,
4991
args: ReadonlyArray<string>,
5092
options?: SpawnOptions,
@@ -58,6 +100,14 @@ function spawn(
58100
return result.stdout.toString().trimEnd();
59101
}
60102

103+
function spawn(
104+
command: string,
105+
args: ReadonlyArray<string>,
106+
options?: SpawnOptions,
107+
): void {
108+
childProcess.spawnSync(command, args, { stdio: 'inherit', ...options });
109+
}
110+
61111
export function readdirRecursive(
62112
dirPath: string,
63113
opts: { ignoreDir?: RegExp } = {},

0 commit comments

Comments
 (0)