Skip to content

Commit 656b175

Browse files
committed
feat: add a script to update all lockfiles
1 parent 4dbc48c commit 656b175

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

updateLockfiles.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const { execSync } = require("child_process");
4+
const { cwd } = require("process");
5+
const yargs = require("yargs");
6+
7+
const args = yargs(process.argv)
8+
.usage("$0 [--frameworks-dir]")
9+
.help("help")
10+
.string("frameworks-dir")
11+
.default("frameworks-dir", "frameworks")
12+
.array("frameworks-types")
13+
.default("frameworks-types", ["keyed", "non-keyed"])
14+
.number("latest-lockfile-version")
15+
.default("latest-lockfile-version", 3).argv;
16+
17+
/**
18+
* @type {string}
19+
*/
20+
const frameworksDir = args.frameworksDir;
21+
22+
/**
23+
* @type {string[]}
24+
*/
25+
const frameworksTypes = args.frameworksTypes;
26+
27+
/**
28+
* @type {number}
29+
*/
30+
const latestLockfileVersion = args.latestLockfileVersion;
31+
32+
/**
33+
* @typedef {Object} Framework
34+
* @property {string} name - Name of the framework (e.g., "vue", "qwik", "svelte")
35+
* @property {string} type - Type of the framework (e.g., "keyed" or "non-keyed")
36+
*/
37+
38+
/**
39+
* Returns an array with arrays of types and names of frameworks
40+
* @returns {Framework[]}
41+
*/
42+
function getFrameworks() {
43+
const frameworks = frameworksTypes.flatMap((type) =>
44+
fs.readdirSync(path.join(frameworksDir, type)).map((framework) => ({
45+
name: framework,
46+
type,
47+
}))
48+
);
49+
50+
return frameworks;
51+
}
52+
53+
/**
54+
* @param {string} frameworkPath
55+
*/
56+
function runNpmInstall(frameworkPath) {
57+
try {
58+
execSync("npm install --package-lock-only", {
59+
cwd: frameworkPath,
60+
stdio: "inherit",
61+
});
62+
console.log(`npm install completed successfully in ${frameworkPath}`);
63+
} catch (error) {
64+
console.error(`npm install failed in ${frameworkPath}: ${error.message}`);
65+
}
66+
}
67+
68+
/**
69+
* Reads the lockfile, parses it, and returns the lockfile version. If lockfile is not found, returns an error.
70+
* @param {string} packageLockJSONPath
71+
* @returns
72+
*/
73+
function getPackageLockJSONVersion(packageLockJSONPath) {
74+
try {
75+
const packageLockJSON = fs.readFileSync(packageLockJSONPath, "utf-8");
76+
const parsedPackageLockJSON = JSON.parse(packageLockJSON);
77+
return parsedPackageLockJSON.lockfileVersion;
78+
} catch (error) {
79+
if (error.code === "ENOENT") {
80+
console.log("Lockfile wasn't found. Create new!");
81+
return;
82+
}
83+
throw new Error(error.message);
84+
}
85+
}
86+
87+
/**
88+
* Updates the lockfile of one framework
89+
* @param {Framework} framework
90+
*/
91+
function processFramework(framework) {
92+
const { name, type } = framework;
93+
94+
console.log(`Checking ${type} ${name} lockfile`);
95+
96+
const frameworkPath = path.join(cwd(), frameworksDir, type, name);
97+
98+
const packageLockJSONPath = path.join(frameworkPath, "package-lock.json");
99+
const packageLockJSONVersion = getPackageLockJSONVersion(packageLockJSONPath);
100+
101+
if (packageLockJSONVersion === latestLockfileVersion) {
102+
console.log(`Skip update for v${latestLockfileVersion} lockfile`);
103+
return;
104+
}
105+
106+
fs.rmSync(packageLockJSONPath, { force: true });
107+
runNpmInstall(frameworkPath);
108+
}
109+
110+
/**
111+
* Updates all frameworks lockfiles in the frameworks directory.
112+
*/
113+
function processAllFrameworks() {
114+
const frameworks = getFrameworks();
115+
116+
for (const framework of frameworks) {
117+
processFramework(framework);
118+
}
119+
}
120+
121+
processAllFrameworks();

0 commit comments

Comments
 (0)