Skip to content

resources/utils.ts: npm/git will inherit stdout by default #3724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions resources/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
});
Expand All @@ -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)) {
Expand All @@ -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);
}
}
Expand Down
12 changes: 5 additions & 7 deletions resources/build-docusaurus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand Down
10 changes: 5 additions & 5 deletions resources/diff-npm-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down Expand Up @@ -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');
}
8 changes: 4 additions & 4 deletions resources/gen-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ function getChangeLog(): Promise<string> {
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));
Expand Down
10 changes: 5 additions & 5 deletions resources/integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ 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);
const packageJSON = readPackageJSON(projectPath);

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);
}

Expand Down
72 changes: 61 additions & 11 deletions resources/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,68 @@ export function makeTmpDir(name: string): MakeTmpDirReturn {
};
}

export function npm(
args: ReadonlyArray<string>,
options?: SpawnOptions,
): string {
return spawn('npm', args, options);
interface NPMOptions extends SpawnOptions {
quiet?: boolean;
}

export function git(
args: ReadonlyArray<string>,
options?: SpawnOptions,
): string {
return spawn('git', args, options);
export function npm(options?: NPMOptions) {
const globalOptions = options?.quiet === true ? ['--quiet'] : [];
return {
run(...args: ReadonlyArray<string>): void {
spawn('npm', [...globalOptions, 'run', ...args], options);
},
install(...args: ReadonlyArray<string>): void {
spawn('npm', [...globalOptions, 'install', ...args], options);
},
ci(...args: ReadonlyArray<string>): void {
spawn('npm', [...globalOptions, 'ci', ...args], options);
},
exec(...args: ReadonlyArray<string>): void {
spawn('npm', [...globalOptions, 'exec', ...args], options);
},
pack(...args: ReadonlyArray<string>): string {
return spawnOutput('npm', [...globalOptions, 'pack', ...args], options);
},
diff(...args: ReadonlyArray<string>): 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<string>): void {
spawn('git', ['clone', ...cmdOptions, ...args], options);
},
checkout(...args: ReadonlyArray<string>): void {
spawn('git', ['checkout', ...cmdOptions, ...args], options);
},
revParse(...args: ReadonlyArray<string>): string {
return spawnOutput('git', ['rev-parse', ...cmdOptions, ...args], options);
},
revList(...args: ReadonlyArray<string>): string {
return spawnOutput('git', ['rev-list', ...cmdOptions, ...args], options);
},
catFile(...args: ReadonlyArray<string>): string {
return spawnOutput('git', ['cat-file', ...cmdOptions, ...args], options);
},
log(...args: ReadonlyArray<string>): string {
return spawnOutput('git', ['log', ...cmdOptions, ...args], options);
},
};
}

interface SpawnOptions {
cwd?: string;
env?: typeof process.env;
}

function spawn(
function spawnOutput(
command: string,
args: ReadonlyArray<string>,
options?: SpawnOptions,
Expand All @@ -58,6 +100,14 @@ function spawn(
return result.stdout.toString().trimEnd();
}

function spawn(
command: string,
args: ReadonlyArray<string>,
options?: SpawnOptions,
): void {
childProcess.spawnSync(command, args, { stdio: 'inherit', ...options });
}

export function readdirRecursive(
dirPath: string,
opts: { ignoreDir?: RegExp } = {},
Expand Down