-
Notifications
You must be signed in to change notification settings - Fork 0
/
node10stubs.mjs
86 lines (74 loc) · 2.44 KB
/
node10stubs.mjs
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
import fs from "fs/promises";
import path from "path";
async function findPackageJson(directory) {
const packagePath = path.join(directory, "package.json");
try {
await fs.access(packagePath);
return packagePath;
} catch (error) {
const parentDir = path.dirname(directory);
if (parentDir === directory) {
throw new Error("package.json not found");
}
return findPackageJson(parentDir);
}
}
async function processSubPackages(packageJsonPath, exports, cleanup = false) {
const baseDir = path.dirname(packageJsonPath);
for (const [subDir, _] of Object.entries(exports)) {
// package.json is already right where Node10 resolution would expect it.
if (subDir.endsWith("package.json")) continue;
// No need for Node10 resolution for component.config.ts
if (subDir.endsWith("convex.config.js")) continue;
// . just works with Node10 resolution
if (subDir === ".") continue;
console.log(subDir);
const newDir = path.join(baseDir, subDir);
const newPackageJsonPath = path.join(newDir, "package.json");
if (cleanup) {
try {
await fs.rm(newDir, { recursive: true, force: true });
} catch (error) {
console.error(`Failed to remove ${newDir}:`, error.message);
}
} else {
const newPackageJson = {
main: `../dist/commonjs/${subDir}/index.js`,
module: `../dist/esm/${subDir}/index.js`,
types: `../dist/commonjs/${subDir}/index.d.ts`,
};
await fs.mkdir(newDir, { recursive: true });
await fs.writeFile(
newPackageJsonPath,
JSON.stringify(newPackageJson, null, 2)
);
}
}
}
async function main() {
try {
const isCleanup = process.argv.includes("--cleanup");
const isAddFiles = process.argv.includes("--addFiles");
const packageJsonPath = await findPackageJson(process.cwd());
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
if (!packageJson.exports) {
throw new Error("exports not found in package.json");
}
if (isAddFiles) {
return;
}
await processSubPackages(packageJsonPath, packageJson.exports, isCleanup);
if (isCleanup) {
console.log(
"Node10 module resolution compatibility stub directories removed."
);
} else {
console.log(
"Node10 module resolution compatibility stub directories created"
);
}
} catch (error) {
console.error("Error:", error.message);
}
}
main();