-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcopyToTemp.ts
More file actions
76 lines (64 loc) · 2.54 KB
/
copyToTemp.ts
File metadata and controls
76 lines (64 loc) · 2.54 KB
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
70
71
72
73
74
75
76
/* eslint-disable no-console */
import { readFileSync, unlinkSync, writeFileSync } from 'fs';
import { cp } from 'fs/promises';
import { join } from 'path';
export async function copyToTemp(originalPath: string, tmpDirPath: string): Promise<void> {
// copy files to tmp dir
await cp(originalPath, tmpDirPath, { recursive: true });
fixPackageJson(tmpDirPath);
// On develop/master, we want to ignore the lock file to always test with fresh dependencies
if (process.env.E2E_IGNORE_LOCKFILE === 'true') {
deleteLockfile(tmpDirPath);
}
}
function deleteLockfile(cwd: string): void {
const lockfilePath = join(cwd, 'pnpm-lock.yaml');
try {
unlinkSync(lockfilePath);
console.log(`Deleted lockfile at ${lockfilePath} (E2E_IGNORE_LOCKFILE=true)`);
} catch {
// Lock file doesn't exist, that's fine
}
}
function fixPackageJson(cwd: string): void {
const packageJsonPath = join(cwd, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
volta?: Record<string, unknown>;
};
// 1. Fix file dependencies
if (packageJson.dependencies) {
fixFileLinkDependencies(packageJson.dependencies);
}
if (packageJson.devDependencies) {
fixFileLinkDependencies(packageJson.devDependencies);
}
// 2. Fix volta extends
if (!packageJson.volta) {
throw new Error("No volta config found, please add one to the test app's package.json!");
}
if (typeof packageJson.volta.extends === 'string') {
const extendsPath = packageJson.volta.extends;
// We add a virtual dir to ensure that the relative depth is consistent
// dirPath is relative to ./../test-applications/xxx
const newPath = join(__dirname, 'virtual-dir/', extendsPath);
packageJson.volta.extends = newPath;
console.log(`Fixed volta.extends to ${newPath}`);
} else {
console.log('No volta.extends found');
}
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
function fixFileLinkDependencies(dependencyObj: Record<string, string>): void {
for (const [key, value] of Object.entries(dependencyObj)) {
if (value.startsWith('link:')) {
const dirPath = value.replace('link:', '');
// We add a virtual dir to ensure that the relative depth is consistent
// dirPath is relative to ./../test-applications/xxx
const newPath = join(__dirname, 'virtual-dir/', dirPath);
dependencyObj[key] = `link:${newPath}`;
console.log(`Fixed ${key} dependency to ${newPath}`);
}
}
}