Skip to content

resources: add new localRepoPath utility function #3610

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
May 28, 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
12 changes: 5 additions & 7 deletions resources/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';

import { localRepoPath } from './utils';

const NS_PER_SEC = 1e9;
const LOCAL = 'local';

Expand All @@ -22,10 +24,6 @@ function runBenchmarks() {
}
}

function localDir(...paths: ReadonlyArray<string>) {
return path.join(__dirname, '..', ...paths);
}

function exec(command: string, options = {}) {
const result = cp.execSync(command, {
encoding: 'utf-8',
Expand Down Expand Up @@ -58,7 +56,7 @@ function prepareBenchmarkProjects(
fs.rmSync(projectPath, { recursive: true, force: true });
fs.mkdirSync(projectPath);

fs.cpSync(localDir('benchmark'), path.join(projectPath, 'benchmark'), {
fs.cpSync(localRepoPath('benchmark'), path.join(projectPath, 'benchmark'), {
recursive: true,
});

Expand All @@ -80,7 +78,7 @@ function prepareBenchmarkProjects(

function prepareNPMPackage(revision: string) {
if (revision === LOCAL) {
const repoDir = localDir();
const repoDir = localRepoPath();
const archivePath = path.join(tmpDir, 'graphql-local.tgz');
fs.renameSync(buildNPMArchive(repoDir), archivePath);
return archivePath;
Expand Down Expand Up @@ -334,7 +332,7 @@ function getArguments(argv: ReadonlyArray<string>) {

function findAllBenchmarks() {
return fs
.readdirSync(localDir('benchmark'), { withFileTypes: true })
.readdirSync(localRepoPath('benchmark'), { withFileTypes: true })
.filter((dirent) => dirent.isFile())
.map((dirent) => dirent.name)
.filter((name) => name.endsWith('-benchmark.js'))
Expand Down
3 changes: 2 additions & 1 deletion resources/build-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as ts from 'typescript';
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
import { inlineInvariant } from './inline-invariant';
import {
localRepoPath,
readdirRecursive,
readPackageJSON,
showDirStats,
Expand All @@ -18,7 +19,7 @@ fs.mkdirSync('./npmDist');

// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
const tsConfig = JSON.parse(
fs.readFileSync(require.resolve('../tsconfig.json'), 'utf-8'),
fs.readFileSync(localRepoPath('tsconfig.json'), 'utf-8'),
);
assert(
tsConfig.compilerOptions,
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 @@ -3,10 +3,9 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';

import { exec, execOutput } from './utils';
import { exec, execOutput, localRepoPath } from './utils';

const LOCAL = 'local';
const localRepoDir = path.join(__dirname, '..');
const tmpDir = path.join(os.tmpdir(), 'graphql-js-npm-diff');
fs.rmSync(tmpDir, { recursive: true, force: true });
fs.mkdirSync(tmpDir);
Expand All @@ -33,7 +32,7 @@ const diff = execOutput(`npm diff --diff=${fromPackage} --diff=${toPackage}`);
if (diff === '') {
console.log('No changes found!');
} else {
const reportPath = path.join(localRepoDir, 'npm-dist-diff.html');
const reportPath = localRepoPath('npm-dist-diff.html');
fs.writeFileSync(reportPath, generateReport(diff));
console.log('Report saved to: ', reportPath);
}
Expand Down Expand Up @@ -76,10 +75,11 @@ function generateReport(diffString: string): string {
</html>
`;
}

function prepareNPMPackage(revision: string): string {
if (revision === LOCAL) {
exec('npm --quiet run build:npm', { cwd: localRepoDir });
return path.join(localRepoDir, 'npmDist');
exec('npm --quiet run build:npm', { cwd: localRepoPath() });
return localRepoPath('npmDist');
}

// Returns the complete git hash for a given git revision reference.
Expand Down
10 changes: 6 additions & 4 deletions resources/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import * as path from 'node:path';

import * as prettier from 'prettier';

export function localRepoPath(...paths: ReadonlyArray<string>): string {
return path.join(__dirname, '..', ...paths);
}

export function exec(command: string, options?: { cwd: string }): void {
childProcess.execSync(command, options);
}
Expand Down Expand Up @@ -97,7 +101,7 @@ export function showDirStats(dirPath: string): void {
}

const prettierConfig = JSON.parse(
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
fs.readFileSync(localRepoPath('.prettierrc'), 'utf-8'),
);

export function writeGeneratedFile(filepath: string, body: string): void {
Expand All @@ -119,7 +123,5 @@ interface PackageJSON {
}

export function readPackageJSON(): PackageJSON {
return JSON.parse(
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),
);
return JSON.parse(fs.readFileSync(localRepoPath('package.json'), 'utf-8'));
}