-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathfixtures.ts
69 lines (59 loc) · 1.47 KB
/
fixtures.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
joinPathFragments,
parseJson,
workspaceRoot,
writeJsonFile,
} from '@nx/devkit';
import {
existsSync,
mkdirSync,
readFileSync,
statSync,
writeFileSync,
} from 'node:fs';
import { rimraf } from 'rimraf';
export const FIXTURES_DIR = joinPathFragments(
workspaceRoot,
'tmp',
'e2e-fixtures',
);
export async function recreateFixturesDir(): Promise<void> {
// Remove any existing e2e fixtures on disk and recreate the location
if (existsSync(FIXTURES_DIR)) {
await rimraf(FIXTURES_DIR);
}
mkdirSync(FIXTURES_DIR, { recursive: true });
}
export class Fixture {
constructor(public root: string) {}
directoryExists(filePath: string): boolean {
try {
return statSync(filePath).isDirectory();
} catch (err) {
return false;
}
}
fileExists(filePath: string): boolean {
try {
return statSync(filePath).isFile();
} catch (err) {
return false;
}
}
exists(filePath: string): boolean {
return this.directoryExists(filePath) || this.fileExists(filePath);
}
readFile(f: string) {
return readFileSync(joinPathFragments(this.root, f), 'utf-8');
}
readJson<T extends object = any>(f: string): T {
const content = this.readFile(f);
return parseJson<T>(content);
}
writeFile(f: string, content = ''): void {
writeFileSync(joinPathFragments(this.root, f), content);
}
writeJson(f: string, content: object): void {
writeJsonFile(joinPathFragments(this.root, f), content);
}
}