Skip to content

Commit b850a2f

Browse files
resources: add utility function to create tmp dir (#3723)
1 parent 07a95b9 commit b850a2f

File tree

6 files changed

+59
-56
lines changed

6 files changed

+59
-56
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"prettier": "prettier --cache --cache-strategy metadata --write --list-different .",
4545
"prettier:check": "prettier --cache --cache-strategy metadata --check .",
4646
"check:spelling": "cspell --cache --no-progress '**/*'",
47-
"check:integrations": "npm run build:npm && npm run build:deno && mocha --full-trace resources/integration-test.ts",
47+
"check:integrations": "mocha --full-trace resources/integration-test.ts",
4848
"serve": "docusaurus serve --dir websiteDist/ --config website/docusaurus.config.cjs",
4949
"start": "npm run build:website && npm run serve",
5050
"build:website": "ts-node resources/build-docusaurus.ts",

resources/benchmark.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import assert from 'node:assert';
22
import cp from 'node:child_process';
33
import fs from 'node:fs';
4-
import os from 'node:os';
54
import path from 'node:path';
65
import url from 'node:url';
76

8-
import { git, localRepoPath, npm } from './utils.js';
7+
import { git, localRepoPath, makeTmpDir, npm } from './utils.js';
98

109
const NS_PER_SEC = 1e9;
1110
const LOCAL = 'local';
@@ -35,18 +34,13 @@ interface BenchmarkProject {
3534
function prepareBenchmarkProjects(
3635
revisionList: ReadonlyArray<string>,
3736
): Array<BenchmarkProject> {
38-
const tmpDir = path.join(os.tmpdir(), 'graphql-js-benchmark');
39-
fs.rmSync(tmpDir, { recursive: true, force: true });
40-
fs.mkdirSync(tmpDir);
41-
42-
const setupDir = path.join(tmpDir, 'setup');
43-
fs.mkdirSync(setupDir);
37+
const { tmpDirPath } = makeTmpDir('graphql-js-benchmark');
4438

4539
return revisionList.map((revision) => {
4640
console.log(`🍳 Preparing ${revision}...`);
47-
const projectPath = path.join(setupDir, revision);
41+
const projectPath = tmpDirPath('setup', revision);
4842
fs.rmSync(projectPath, { recursive: true, force: true });
49-
fs.mkdirSync(projectPath);
43+
fs.mkdirSync(projectPath, { recursive: true });
5044

5145
fs.cpSync(localRepoPath('benchmark'), path.join(projectPath, 'benchmark'), {
5246
recursive: true,
@@ -71,20 +65,20 @@ function prepareBenchmarkProjects(
7165
function prepareNPMPackage(revision: string) {
7266
if (revision === LOCAL) {
7367
const repoDir = localRepoPath();
74-
const archivePath = path.join(tmpDir, 'graphql-local.tgz');
68+
const archivePath = tmpDirPath('graphql-local.tgz');
7569
fs.renameSync(buildNPMArchive(repoDir), archivePath);
7670
return archivePath;
7771
}
7872

7973
// Returns the complete git hash for a given git revision reference.
8074
const hash = git(['rev-parse', revision]);
8175

82-
const archivePath = path.join(tmpDir, `graphql-${hash}.tgz`);
76+
const archivePath = tmpDirPath(`graphql-${hash}.tgz`);
8377
if (fs.existsSync(archivePath)) {
8478
return archivePath;
8579
}
8680

87-
const repoDir = path.join(tmpDir, hash);
81+
const repoDir = tmpDirPath(hash);
8882
fs.rmSync(repoDir, { recursive: true, force: true });
8983
fs.mkdirSync(repoDir);
9084
git(['clone', '--quiet', localRepoPath(), repoDir]);

resources/build-docusaurus.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
// Should be just: `docusaurus build --out-dir ./websiteDist ./website`
44

55
import fs from 'node:fs';
6-
import os from 'node:os';
7-
import path from 'node:path';
86

9-
import { localRepoPath, npm, readPackageJSON } from './utils.js';
7+
import { localRepoPath, makeTmpDir, npm, readPackageJSON } from './utils.js';
108

11-
const tmpDir = path.join(os.tmpdir(), 'graphql-run-docusaurus');
12-
fs.rmSync(tmpDir, { recursive: true, force: true });
13-
fs.mkdirSync(tmpDir);
9+
const { tmpDirPath } = makeTmpDir('graphql-js-run-docusaurus');
1410

1511
const packageJSON = readPackageJSON();
1612
delete packageJSON.type;
@@ -21,7 +17,7 @@ copyToTmpDir('tsconfig.json');
2117
copyToTmpDir('src');
2218
copyToTmpDir('website');
2319

24-
npm(['install', 'ci'], { cwd: tmpDir });
20+
npm(['install', 'ci'], { cwd: tmpDirPath() });
2521

2622
const env = {
2723
...process.env,
@@ -33,14 +29,13 @@ const docusaurusArgs = [
3329
localRepoPath('websiteDist'),
3430
tmpDirPath('website'),
3531
];
36-
npm(['exec', 'docusaurus', '--', ...docusaurusArgs], { env, cwd: tmpDir });
32+
npm(['exec', 'docusaurus', '--', ...docusaurusArgs], {
33+
env,
34+
cwd: tmpDirPath(),
35+
});
3736

3837
function copyToTmpDir(relativePath: string) {
3938
fs.cpSync(localRepoPath(relativePath), tmpDirPath(relativePath), {
4039
recursive: true,
4140
});
4241
}
43-
44-
function tmpDirPath(...paths: ReadonlyArray<string>): string {
45-
return path.join(tmpDir, ...paths);
46-
}

resources/diff-npm-package.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import assert from 'node:assert';
22
import childProcess from 'node:child_process';
33
import fs from 'node:fs';
4-
import os from 'node:os';
54
import path from 'node:path';
65

7-
import { git, localRepoPath, npm, writeGeneratedFile } from './utils.js';
6+
import {
7+
git,
8+
localRepoPath,
9+
makeTmpDir,
10+
npm,
11+
writeGeneratedFile,
12+
} from './utils.js';
813

914
const LOCAL = 'local';
10-
const tmpDir = path.join(os.tmpdir(), 'graphql-js-npm-diff');
11-
fs.rmSync(tmpDir, { recursive: true, force: true });
12-
fs.mkdirSync(tmpDir);
15+
const { tmpDirPath } = makeTmpDir('graphql-js-npm-diff');
1316

1417
const args = process.argv.slice(2);
1518
let [fromRevision, toRevision] = args;
@@ -87,7 +90,7 @@ function prepareNPMPackage(revision: string): string {
8790
const hash = git(['rev-parse', revision]);
8891
assert(hash != null);
8992

90-
const repoDir = path.join(tmpDir, hash);
93+
const repoDir = tmpDirPath(hash);
9194
fs.rmSync(repoDir, { recursive: true, force: true });
9295
fs.mkdirSync(repoDir);
9396
childProcess.execSync(`git archive "${hash}" | tar -xC "${repoDir}"`);

resources/integration-test.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
import fs from 'node:fs';
2-
import os from 'node:os';
3-
import path from 'node:path';
42

53
import { describe, it } from 'mocha';
64

7-
import { localRepoPath, npm } from './utils.js';
5+
import { localRepoPath, makeTmpDir, npm, readPackageJSON } from './utils.js';
86

97
describe('Integration Tests', () => {
10-
const tmpDir = path.join(os.tmpdir(), 'graphql-js-integrationTmp');
11-
fs.rmSync(tmpDir, { recursive: true, force: true });
12-
fs.mkdirSync(tmpDir);
8+
const { tmpDirPath } = makeTmpDir('graphql-js-integrationTmp');
9+
fs.cpSync(localRepoPath('integrationTests'), tmpDirPath(), {
10+
recursive: true,
11+
});
1312

13+
npm(['run', 'build:npm']);
1414
const distDir = localRepoPath('npmDist');
15-
const archiveName = npm(['--quiet', 'pack', distDir], { cwd: tmpDir });
16-
fs.renameSync(
17-
path.join(tmpDir, archiveName),
18-
path.join(tmpDir, 'graphql.tgz'),
19-
);
15+
const archiveName = npm(['--quiet', 'pack', distDir], { cwd: tmpDirPath() });
16+
fs.renameSync(tmpDirPath(archiveName), tmpDirPath('graphql.tgz'));
2017

21-
function testOnNodeProject(projectName: string) {
22-
const projectPath = localRepoPath('integrationTests', projectName);
18+
npm(['run', 'build:deno']);
2319

24-
const packageJSONPath = path.join(projectPath, 'package.json');
25-
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'));
20+
function testOnNodeProject(projectName: string) {
21+
const projectPath = tmpDirPath(projectName);
22+
const packageJSON = readPackageJSON(projectPath);
2623

2724
it(packageJSON.description, () => {
28-
fs.cpSync(projectPath, path.join(tmpDir, projectName), {
29-
recursive: true,
30-
});
31-
32-
const cwd = path.join(tmpDir, projectName);
3325
// TODO: figure out a way to run it with --ignore-scripts
34-
npm(['--quiet', 'install'], { cwd });
35-
npm(['--quiet', 'test'], { cwd });
26+
npm(['--quiet', 'install'], { cwd: projectPath });
27+
npm(['--quiet', 'test'], { cwd: projectPath });
3628
}).timeout(120000);
3729
}
3830

resources/utils.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'node:assert';
22
import childProcess from 'node:child_process';
33
import fs from 'node:fs';
4+
import os from 'node:os';
45
import path from 'node:path';
56

67
import prettier from 'prettier';
@@ -10,6 +11,20 @@ export function localRepoPath(...paths: ReadonlyArray<string>): string {
1011
return path.join(repoDir, ...paths);
1112
}
1213

14+
interface MakeTmpDirReturn {
15+
tmpDirPath: (...paths: ReadonlyArray<string>) => string;
16+
}
17+
18+
export function makeTmpDir(name: string): MakeTmpDirReturn {
19+
const tmpDir = path.join(os.tmpdir(), name);
20+
fs.rmSync(tmpDir, { recursive: true, force: true });
21+
fs.mkdirSync(tmpDir);
22+
23+
return {
24+
tmpDirPath: (...paths) => path.join(tmpDir, ...paths),
25+
};
26+
}
27+
1328
export function npm(
1429
args: ReadonlyArray<string>,
1530
options?: SpawnOptions,
@@ -133,6 +148,7 @@ export function writeGeneratedFile(filepath: string, body: string): void {
133148
}
134149

135150
interface PackageJSON {
151+
description: string;
136152
version: string;
137153
private?: boolean;
138154
repository?: { url?: string };
@@ -145,6 +161,9 @@ interface PackageJSON {
145161
publishConfig?: { tag?: string };
146162
}
147163

148-
export function readPackageJSON(): PackageJSON {
149-
return JSON.parse(fs.readFileSync(localRepoPath('package.json'), 'utf-8'));
164+
export function readPackageJSON(
165+
dirPath: string = localRepoPath(),
166+
): PackageJSON {
167+
const filepath = path.join(dirPath, 'package.json');
168+
return JSON.parse(fs.readFileSync(filepath, 'utf-8'));
150169
}

0 commit comments

Comments
 (0)