-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_all.js
More file actions
executable file
·68 lines (61 loc) · 1.81 KB
/
update_all.js
File metadata and controls
executable file
·68 lines (61 loc) · 1.81 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
import fs from "fs";
import { globSync } from "glob";
import jp from "jsonpath";
if (process.argv.length < 5) {
console.log(
"Usage: \nnode update_all.js filename jsonquery value\nnode update_all.js filename jsonquery value apply"
);
console.log(`\tExample: node update_all.js package.json '$.engines.node' ">=18.0.0" apply\n`);
process.exit(1);
}
let pretend = process.argv[5] !== "apply";
function loadFile(file) {
try {
return JSON.parse(fs.readFileSync(file).toString());
} catch (err) {
console.log("Cannot parse", file);
throw err;
}
}
function saveFile(file, data) {
if (pretend) {
console.log(file);
console.log(JSON.stringify(data, undefined, 2));
return;
}
fs.writeFileSync(file, JSON.stringify(data, undefined, 2));
}
let workspaces = fs
.readFileSync("./pnpm-workspace.yaml", "utf8")
.split("\n")
.filter(l => /^\s*-\s/.test(l))
.map(l => l.replace(/^\s*-\s*/, "").replace(/^["']|["']$/g, "").trim())
.filter(l => l && !l.startsWith("#"));
let filename = process.argv[2]; //"tsconfig.json";
let query = process.argv[3]; //"$.compilerOptions.target";
let value = process.argv[4]; //"es2020";node update_all.js filename jsonquery value
// Try to deduct
if (value === "true") {
value = true;
} else if (value === "false") {
value = false;
} else if (value.match(/^\d+$/) !== null) {
value = parseInt(value);
} else if (value.startsWith("{") || value.startsWith("[")) {
value = JSON.parse(value);
} else if (value === "DELETE") {
value = undefined;
}
// For each workspaces
for (let workspace of workspaces) {
if (!workspace.endsWith("/")) {
workspace += "/";
}
let files = globSync(workspace + filename);
console.log(files);
for (let file of files) {
let content = loadFile(file);
jp.value(content, query, value);
saveFile(file, content);
}
}