Skip to content

Commit be42e7c

Browse files
resources: add new localRepoPath utility function (#3610)
1 parent 8799d19 commit be42e7c

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

resources/benchmark.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as fs from 'node:fs';
44
import * as os from 'node:os';
55
import * as path from 'node:path';
66

7+
import { localRepoPath } from './utils';
8+
79
const NS_PER_SEC = 1e9;
810
const LOCAL = 'local';
911

@@ -22,10 +24,6 @@ function runBenchmarks() {
2224
}
2325
}
2426

25-
function localDir(...paths: ReadonlyArray<string>) {
26-
return path.join(__dirname, '..', ...paths);
27-
}
28-
2927
function exec(command: string, options = {}) {
3028
const result = cp.execSync(command, {
3129
encoding: 'utf-8',
@@ -58,7 +56,7 @@ function prepareBenchmarkProjects(
5856
fs.rmSync(projectPath, { recursive: true, force: true });
5957
fs.mkdirSync(projectPath);
6058

61-
fs.cpSync(localDir('benchmark'), path.join(projectPath, 'benchmark'), {
59+
fs.cpSync(localRepoPath('benchmark'), path.join(projectPath, 'benchmark'), {
6260
recursive: true,
6361
});
6462

@@ -80,7 +78,7 @@ function prepareBenchmarkProjects(
8078

8179
function prepareNPMPackage(revision: string) {
8280
if (revision === LOCAL) {
83-
const repoDir = localDir();
81+
const repoDir = localRepoPath();
8482
const archivePath = path.join(tmpDir, 'graphql-local.tgz');
8583
fs.renameSync(buildNPMArchive(repoDir), archivePath);
8684
return archivePath;
@@ -334,7 +332,7 @@ function getArguments(argv: ReadonlyArray<string>) {
334332

335333
function findAllBenchmarks() {
336334
return fs
337-
.readdirSync(localDir('benchmark'), { withFileTypes: true })
335+
.readdirSync(localRepoPath('benchmark'), { withFileTypes: true })
338336
.filter((dirent) => dirent.isFile())
339337
.map((dirent) => dirent.name)
340338
.filter((name) => name.endsWith('-benchmark.js'))

resources/build-npm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as ts from 'typescript';
77
import { addExtensionToImportPaths } from './add-extension-to-import-paths';
88
import { inlineInvariant } from './inline-invariant';
99
import {
10+
localRepoPath,
1011
readdirRecursive,
1112
readPackageJSON,
1213
showDirStats,
@@ -18,7 +19,7 @@ fs.mkdirSync('./npmDist');
1819

1920
// Based on https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#getting-the-dts-from-a-javascript-file
2021
const tsConfig = JSON.parse(
21-
fs.readFileSync(require.resolve('../tsconfig.json'), 'utf-8'),
22+
fs.readFileSync(localRepoPath('tsconfig.json'), 'utf-8'),
2223
);
2324
assert(
2425
tsConfig.compilerOptions,

resources/diff-npm-package.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import * as fs from 'node:fs';
33
import * as os from 'node:os';
44
import * as path from 'node:path';
55

6-
import { exec, execOutput } from './utils';
6+
import { exec, execOutput, localRepoPath } from './utils';
77

88
const LOCAL = 'local';
9-
const localRepoDir = path.join(__dirname, '..');
109
const tmpDir = path.join(os.tmpdir(), 'graphql-js-npm-diff');
1110
fs.rmSync(tmpDir, { recursive: true, force: true });
1211
fs.mkdirSync(tmpDir);
@@ -33,7 +32,7 @@ const diff = execOutput(`npm diff --diff=${fromPackage} --diff=${toPackage}`);
3332
if (diff === '') {
3433
console.log('No changes found!');
3534
} else {
36-
const reportPath = path.join(localRepoDir, 'npm-dist-diff.html');
35+
const reportPath = localRepoPath('npm-dist-diff.html');
3736
fs.writeFileSync(reportPath, generateReport(diff));
3837
console.log('Report saved to: ', reportPath);
3938
}
@@ -76,10 +75,11 @@ function generateReport(diffString: string): string {
7675
</html>
7776
`;
7877
}
78+
7979
function prepareNPMPackage(revision: string): string {
8080
if (revision === LOCAL) {
81-
exec('npm --quiet run build:npm', { cwd: localRepoDir });
82-
return path.join(localRepoDir, 'npmDist');
81+
exec('npm --quiet run build:npm', { cwd: localRepoPath() });
82+
return localRepoPath('npmDist');
8383
}
8484

8585
// Returns the complete git hash for a given git revision reference.

resources/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import * as path from 'node:path';
55

66
import * as prettier from 'prettier';
77

8+
export function localRepoPath(...paths: ReadonlyArray<string>): string {
9+
return path.join(__dirname, '..', ...paths);
10+
}
11+
812
export function exec(command: string, options?: { cwd: string }): void {
913
childProcess.execSync(command, options);
1014
}
@@ -97,7 +101,7 @@ export function showDirStats(dirPath: string): void {
97101
}
98102

99103
const prettierConfig = JSON.parse(
100-
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
104+
fs.readFileSync(localRepoPath('.prettierrc'), 'utf-8'),
101105
);
102106

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

121125
export function readPackageJSON(): PackageJSON {
122-
return JSON.parse(
123-
fs.readFileSync(require.resolve('../package.json'), 'utf-8'),
124-
);
126+
return JSON.parse(fs.readFileSync(localRepoPath('package.json'), 'utf-8'));
125127
}

0 commit comments

Comments
 (0)