-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcopyToTemp.ts
More file actions
97 lines (81 loc) · 2.98 KB
/
copyToTemp.ts
File metadata and controls
97 lines (81 loc) · 2.98 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* eslint-disable no-console */
import { readFileSync, 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);
fixDenoJson(tmpDirPath);
}
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}`);
}
}
}
function fixDenoJson(cwd: string): void {
const denoJsonPath = join(cwd, 'deno.json');
let raw: string;
try {
raw = readFileSync(denoJsonPath, 'utf8');
} catch {
return;
}
const denoJson = JSON.parse(raw) as {
imports?: Record<string, string>;
};
if (!denoJson.imports) {
return;
}
let changed = false;
for (const [key, value] of Object.entries(denoJson.imports)) {
// Fix relative paths (not npm: or https: specifiers)
if (value.startsWith('.') || value.startsWith('/')) {
// Same virtual-dir trick as link: deps to get consistent relative depth
const newPath = join(__dirname, 'virtual-dir/', value);
denoJson.imports[key] = newPath;
console.log(`Fixed deno.json import ${key} to ${newPath}`);
changed = true;
}
}
if (changed) {
writeFileSync(denoJsonPath, JSON.stringify(denoJson, null, 2));
}
}