diff --git a/resources/benchmark.ts b/resources/benchmark.ts index b40cf55fb68..b351cefe0b7 100644 --- a/resources/benchmark.ts +++ b/resources/benchmark.ts @@ -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'; @@ -22,10 +24,6 @@ function runBenchmarks() { } } -function localDir(...paths: ReadonlyArray) { - return path.join(__dirname, '..', ...paths); -} - function exec(command: string, options = {}) { const result = cp.execSync(command, { encoding: 'utf-8', @@ -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, }); @@ -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; @@ -334,7 +332,7 @@ function getArguments(argv: ReadonlyArray) { 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')) diff --git a/resources/build-npm.ts b/resources/build-npm.ts index 42715a09de5..db076de988d 100644 --- a/resources/build-npm.ts +++ b/resources/build-npm.ts @@ -11,6 +11,7 @@ import { readPackageJSON, showDirStats, writeGeneratedFile, + localRepoPath, } from './utils'; fs.rmSync('./npmDist', { recursive: true, force: true }); @@ -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, diff --git a/resources/diff-npm-package.ts b/resources/diff-npm-package.ts index 5ae2790f03f..6d5fbb1f01a 100644 --- a/resources/diff-npm-package.ts +++ b/resources/diff-npm-package.ts @@ -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); @@ -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); } @@ -76,10 +75,11 @@ function generateReport(diffString: string): string { `; } + 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. diff --git a/resources/utils.ts b/resources/utils.ts index a1f9e3e6205..c8d3bb7123f 100644 --- a/resources/utils.ts +++ b/resources/utils.ts @@ -5,6 +5,10 @@ import * as path from 'node:path'; import * as prettier from 'prettier'; +export function localRepoPath(...paths: ReadonlyArray): string { + return path.join(__dirname, '..', ...paths); +} + export function exec(command: string, options?: { cwd: string }): void { childProcess.execSync(command, options); } @@ -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 { @@ -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')); }