-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore-workspace.js
More file actions
67 lines (61 loc) · 1.57 KB
/
Copy pathrestore-workspace.js
File metadata and controls
67 lines (61 loc) · 1.57 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
const fs = require("fs");
const path = require("path");
const packagesToRestore = [
"@query/api",
"@query/auth",
"@query/db",
"@query/ui",
"@query/eslint-config",
"@query/prettier-config",
"@query/tailwind-config",
"@query/tsconfig",
];
function restore(dir) {
let entries;
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (
!entry.name.includes("node_modules") &&
!entry.name.includes(".next")
) {
restore(fullPath);
}
} else if (entry.name === "package.json") {
try {
const fd = fs.openSync(fullPath, "r+");
const content = fs.readFileSync(fd, "utf8");
const json = JSON.parse(content);
let changed = false;
for (const section of [
"dependencies",
"devDependencies",
"peerDependencies",
]) {
if (json[section]) {
for (const pkg of packagesToRestore) {
if (json[section][pkg] === "*") {
json[section][pkg] = "workspace:*";
changed = true;
}
}
}
}
if (changed) {
const output = JSON.stringify(json, null, 2) + "\n";
fs.ftruncateSync(fd);
fs.writeSync(fd, output, 0);
}
fs.closeSync(fd);
} catch (e) {
console.error("Error processing " + fullPath + ": " + e.message);
}
}
}
}
restore(process.cwd());