diff --git a/resources/benchmark.ts b/resources/benchmark.ts index 8dfc301d75..51ac67b30d 100644 --- a/resources/benchmark.ts +++ b/resources/benchmark.ts @@ -57,7 +57,7 @@ function prepareBenchmarkProjects( path.join(projectPath, 'package.json'), JSON.stringify(packageJSON, null, 2), ); - npm(['--quiet', 'install', '--ignore-scripts'], { cwd: projectPath }); + npm({ cwd: projectPath, quiet: true }).install('--ignore-scripts'); return { revision, projectPath }; }); @@ -71,7 +71,7 @@ function prepareBenchmarkProjects( } // Returns the complete git hash for a given git revision reference. - const hash = git(['rev-parse', revision]); + const hash = git().revParse(revision); const archivePath = tmpDirPath(`graphql-${hash}.tgz`); if (fs.existsSync(archivePath)) { @@ -81,21 +81,19 @@ function prepareBenchmarkProjects( const repoDir = tmpDirPath(hash); fs.rmSync(repoDir, { recursive: true, force: true }); fs.mkdirSync(repoDir); - git(['clone', '--quiet', localRepoPath(), repoDir]); - git(['checkout', '--quiet', '--detach', hash], { cwd: repoDir }); - npm(['--quiet', 'ci', '--ignore-scripts'], { cwd: repoDir }); + git({ quiet: true }).clone(localRepoPath(), repoDir); + git({ cwd: repoDir, quiet: true }).checkout('--detach', hash); + npm({ cwd: repoDir, quiet: true }).ci('--ignore-scripts'); fs.renameSync(buildNPMArchive(repoDir), archivePath); fs.rmSync(repoDir, { recursive: true }); return archivePath; } function buildNPMArchive(repoDir: string) { - npm(['--quiet', 'run', 'build:npm'], { cwd: repoDir }); + npm({ cwd: repoDir, quiet: true }).run('build:npm'); const distDir = path.join(repoDir, 'npmDist'); - const archiveName = npm(['--quiet', 'pack', distDir], { - cwd: repoDir, - }); + const archiveName = npm({ cwd: repoDir, quiet: true }).pack(distDir); return path.join(repoDir, archiveName); } } diff --git a/resources/build-docusaurus.ts b/resources/build-docusaurus.ts index bb8a7d7393..8928bac07c 100644 --- a/resources/build-docusaurus.ts +++ b/resources/build-docusaurus.ts @@ -17,22 +17,20 @@ copyToTmpDir('tsconfig.json'); copyToTmpDir('src'); copyToTmpDir('website'); -npm(['install', 'ci'], { cwd: tmpDirPath() }); +npm({ cwd: tmpDirPath(), quiet: true }).ci('--ignore-scripts'); const env = { ...process.env, DOCUSAURUS_GENERATED_FILES_DIR_NAME: tmpDirPath('.docusaurus'), }; -const docusaurusArgs = [ +npm({ env, cwd: tmpDirPath() }).exec( + 'docusaurus', + '--', 'build', '--out-dir', localRepoPath('websiteDist'), tmpDirPath('website'), -]; -npm(['exec', 'docusaurus', '--', ...docusaurusArgs], { - env, - cwd: tmpDirPath(), -}); +); function copyToTmpDir(relativePath: string) { fs.cpSync(localRepoPath(relativePath), tmpDirPath(relativePath), { diff --git a/resources/diff-npm-package.ts b/resources/diff-npm-package.ts index 8523b74ac3..c61f54aa32 100644 --- a/resources/diff-npm-package.ts +++ b/resources/diff-npm-package.ts @@ -31,7 +31,7 @@ console.log(`📦 Building NPM package for ${toRevision}...`); const toPackage = prepareNPMPackage(toRevision); console.log('➖➕ Generating diff...'); -const diff = npm(['diff', '--diff', fromPackage, '--diff', toPackage]); +const diff = npm().diff('--diff', fromPackage, '--diff', toPackage); if (diff === '') { console.log('No changes found!'); @@ -82,19 +82,19 @@ function generateReport(diffString: string): string { function prepareNPMPackage(revision: string): string { if (revision === LOCAL) { - npm(['--quiet', 'run', 'build:npm'], { cwd: localRepoPath() }); + npm({ cwd: localRepoPath(), quiet: true }).run('build:npm'); return localRepoPath('npmDist'); } // Returns the complete git hash for a given git revision reference. - const hash = git(['rev-parse', revision]); + const hash = git().revParse(revision); assert(hash != null); const repoDir = tmpDirPath(hash); fs.rmSync(repoDir, { recursive: true, force: true }); fs.mkdirSync(repoDir); childProcess.execSync(`git archive "${hash}" | tar -xC "${repoDir}"`); - npm(['--quiet', 'ci', '--ignore-scripts'], { cwd: repoDir }); - npm(['--quiet', 'run', 'build:npm'], { cwd: repoDir }); + npm({ cwd: repoDir, quiet: true }).ci('--ignore-scripts'); + npm({ cwd: repoDir, quiet: true }).run('build:npm'); return path.join(repoDir, 'npmDist'); } diff --git a/resources/gen-changelog.ts b/resources/gen-changelog.ts index 998f0213fb..821f29b963 100644 --- a/resources/gen-changelog.ts +++ b/resources/gen-changelog.ts @@ -64,15 +64,15 @@ function getChangeLog(): Promise { const { version } = packageJSON; let tag: string | null = null; - let commitsList = git(['rev-list', '--reverse', `v${version}..`]); + let commitsList = git().revList('--reverse', `v${version}..`); if (commitsList === '') { - const parentPackageJSON = git(['cat-file', 'blob', 'HEAD~1:package.json']); + const parentPackageJSON = git().catFile('blob', 'HEAD~1:package.json'); const parentVersion = JSON.parse(parentPackageJSON).version; - commitsList = git(['rev-list', '--reverse', `v${parentVersion}..HEAD~1`]); + commitsList = git().revList('--reverse', `v${parentVersion}..HEAD~1`); tag = `v${version}`; } - const date = git(['log', '-1', '--format=%cd', '--date=short']); + const date = git().log('-1', '--format=%cd', '--date=short'); return getCommitsInfo(commitsList.split('\n')) .then((commitsInfo) => getPRsInfo(commitsInfoToPRs(commitsInfo))) .then((prsInfo) => genChangeLog(tag, date, prsInfo)); diff --git a/resources/integration-test.ts b/resources/integration-test.ts index 46670db580..a03c9dbb4a 100644 --- a/resources/integration-test.ts +++ b/resources/integration-test.ts @@ -10,12 +10,12 @@ describe('Integration Tests', () => { recursive: true, }); - npm(['run', 'build:npm']); + npm().run('build:npm'); const distDir = localRepoPath('npmDist'); - const archiveName = npm(['--quiet', 'pack', distDir], { cwd: tmpDirPath() }); + const archiveName = npm({ cwd: tmpDirPath(), quiet: true }).pack(distDir); fs.renameSync(tmpDirPath(archiveName), tmpDirPath('graphql.tgz')); - npm(['run', 'build:deno']); + npm().run('build:deno'); function testOnNodeProject(projectName: string) { const projectPath = tmpDirPath(projectName); @@ -23,8 +23,8 @@ describe('Integration Tests', () => { it(packageJSON.description, () => { // TODO: figure out a way to run it with --ignore-scripts - npm(['--quiet', 'install'], { cwd: projectPath }); - npm(['--quiet', 'test'], { cwd: projectPath }); + npm({ cwd: projectPath, quiet: true }).install(); + npm({ cwd: projectPath, quiet: true }).run('test'); }).timeout(120000); } diff --git a/resources/utils.ts b/resources/utils.ts index 90aaec2b5e..701f63c29a 100644 --- a/resources/utils.ts +++ b/resources/utils.ts @@ -25,18 +25,60 @@ export function makeTmpDir(name: string): MakeTmpDirReturn { }; } -export function npm( - args: ReadonlyArray, - options?: SpawnOptions, -): string { - return spawn('npm', args, options); +interface NPMOptions extends SpawnOptions { + quiet?: boolean; } -export function git( - args: ReadonlyArray, - options?: SpawnOptions, -): string { - return spawn('git', args, options); +export function npm(options?: NPMOptions) { + const globalOptions = options?.quiet === true ? ['--quiet'] : []; + return { + run(...args: ReadonlyArray): void { + spawn('npm', [...globalOptions, 'run', ...args], options); + }, + install(...args: ReadonlyArray): void { + spawn('npm', [...globalOptions, 'install', ...args], options); + }, + ci(...args: ReadonlyArray): void { + spawn('npm', [...globalOptions, 'ci', ...args], options); + }, + exec(...args: ReadonlyArray): void { + spawn('npm', [...globalOptions, 'exec', ...args], options); + }, + pack(...args: ReadonlyArray): string { + return spawnOutput('npm', [...globalOptions, 'pack', ...args], options); + }, + diff(...args: ReadonlyArray): string { + return spawnOutput('npm', [...globalOptions, 'diff', ...args], options); + }, + }; +} + +interface GITOptions extends SpawnOptions { + quiet?: boolean; +} + +export function git(options?: GITOptions) { + const cmdOptions = options?.quiet === true ? ['--quiet'] : []; + return { + clone(...args: ReadonlyArray): void { + spawn('git', ['clone', ...cmdOptions, ...args], options); + }, + checkout(...args: ReadonlyArray): void { + spawn('git', ['checkout', ...cmdOptions, ...args], options); + }, + revParse(...args: ReadonlyArray): string { + return spawnOutput('git', ['rev-parse', ...cmdOptions, ...args], options); + }, + revList(...args: ReadonlyArray): string { + return spawnOutput('git', ['rev-list', ...cmdOptions, ...args], options); + }, + catFile(...args: ReadonlyArray): string { + return spawnOutput('git', ['cat-file', ...cmdOptions, ...args], options); + }, + log(...args: ReadonlyArray): string { + return spawnOutput('git', ['log', ...cmdOptions, ...args], options); + }, + }; } interface SpawnOptions { @@ -44,7 +86,7 @@ interface SpawnOptions { env?: typeof process.env; } -function spawn( +function spawnOutput( command: string, args: ReadonlyArray, options?: SpawnOptions, @@ -58,6 +100,14 @@ function spawn( return result.stdout.toString().trimEnd(); } +function spawn( + command: string, + args: ReadonlyArray, + options?: SpawnOptions, +): void { + childProcess.spawnSync(command, args, { stdio: 'inherit', ...options }); +} + export function readdirRecursive( dirPath: string, opts: { ignoreDir?: RegExp } = {},