Skip to content

Commit fd05c64

Browse files
committed
refactor, perf, feat: improve scripts in root
Add JSDoc types and comments. Add script customisation via CLI using `yargs`. Improve performance. Improve naming (remove different names for the same variables and functions to remove confusion). (`rebuild-check-single.js`) Fix the file description. (`cleanup.js`) Improve performance of deleting framework files by using native `fs.rmSync` and making the logic of deleting all files in a separate function.
1 parent 6add7da commit fd05c64

File tree

7 files changed

+267
-119
lines changed

7 files changed

+267
-119
lines changed

checkObsolete.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,39 @@ const { execSync } = require("child_process");
22
const fs = require("fs");
33
const JSON5 = require("json5");
44
const path = require("path");
5+
const yargs = require("yargs");
56

6-
const DEBUG = false;
7+
const args = yargs(process.argv)
8+
.usage("$0 [--debug]")
9+
.help()
10+
.boolean("debug")
11+
.default("debug", false).argv;
712

813
/**
9-
* Returns an array with arrays of types and names of frameworks
14+
* @type {boolean}
15+
*/
16+
const DEBUG = args.debug;
17+
18+
/**
19+
* @typedef {Object} Framework
20+
* @property {string} name - Name of the framework (e.g., "vue", "qwik", "svelte")
21+
* @property {string} type - Type of the framework (e.g., "keyed" or "non-keyed")
22+
*/
23+
24+
/**
25+
* Returns an array of frameworks with their type and name
1026
* @example getFramewokrs()
11-
* @returns [{type:"keyed", name:"vue"},{type:"keyed", name:"qwik"},{type:"non-keyed", name:"svelte"}]
27+
* @returns {Framework[]}
1228
*/
1329
function getFrameworks() {
14-
const keyedFrameworks = fs
15-
.readdirSync("./frameworks/keyed")
16-
.map((framework) => ({ type: "keyed", name: framework }));
17-
const nonKeyedFrameworks = fs
18-
.readdirSync("./frameworks/non-keyed")
19-
.map((framework) => ({ type: "non-keyed", name: framework }));
20-
return [...keyedFrameworks, ...nonKeyedFrameworks];
30+
const framewokrsTypes = ["keyed", "non-keyed"];
31+
const framewokrs = framewokrsTypes.flatMap((type) =>
32+
fs
33+
.readdirSync(path.join("framewokrs", type))
34+
.map((framework) => ({ name: framework, type }))
35+
);
36+
37+
return framewokrs;
2138
}
2239

2340
const frameworks = getFrameworks();

cleanup.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,65 @@
11
const fs = require("fs");
22
const path = require("path");
3+
const yargs = require("yargs");
4+
5+
const args = yargs(process.argv)
6+
.help()
7+
.string("framework-dir")
8+
.default("framework-dir", "frameworks")
9+
.array("keyed-types")
10+
.default("keyed-types", ["keyed", "non-keyed"]).argv;
11+
12+
/**
13+
* @type {string}
14+
*/
15+
const frameworksDir = args.frameworkDir;
316

417
/**
5-
* @param {string} dir
18+
* @type {string[]}
619
*/
7-
function rmIfExists(dir) {
8-
if (fs.existsSync(dir)) {
9-
console.log("Cleaning", dir);
10-
fs.rmSync(dir, { recursive: true });
20+
const keyedTypes = args.keyedTypes;
21+
22+
const frameworksPath = path.resolve(frameworksDir);
23+
24+
const filesToDelete = [
25+
"package-lock.json",
26+
"yarn-lock",
27+
"dist",
28+
"elm-stuff",
29+
"bower_components",
30+
"node_modules",
31+
];
32+
33+
/**
34+
* Delete specified files in the framework directory
35+
* @param {string} frameworkPath
36+
* @param {string[]} filesToDelete
37+
*/
38+
function deleteFrameworkFiles(frameworkPath, filesToDelete) {
39+
for (const file of filesToDelete) {
40+
const filePath = path.join(frameworkPath, file);
41+
fs.rmSync(filePath, { recursive: true, force: true });
1142
}
43+
console.log(`Deleted: ${filesToDelete}`);
1244
}
1345

1446
/**
15-
* @param {string} basePath
47+
* Cleans all framework directories of package-lock.json, yarn-lock and the elm-stuff, node-modules, bower-components and dist directories.
48+
* @param {string} frameworksPath
1649
* @param {string} keyedTypes
1750
*/
18-
function cleanFrameworkDirectories(basePath, keyedTypes) {
51+
function cleanFrameworkDirectories(frameworksPath, keyedTypes) {
1952
for (const keyedType of keyedTypes) {
20-
const frameworkDir = path.resolve(basePath, keyedType);
53+
const frameworkDir = path.resolve(frameworksPath, keyedType);
2154
const directories = fs.readdirSync(frameworkDir);
2255

2356
for (const directory of directories) {
2457
const frameworkPath = path.resolve(frameworkDir, directory);
25-
console.log("cleaning ", frameworkPath);
26-
27-
rmIfExists(path.join(frameworkPath, "package-lock.json"));
28-
rmIfExists(path.join(frameworkPath, "yarn.lock"));
29-
rmIfExists(path.join(frameworkPath, "node_modules"));
30-
rmIfExists(path.join(frameworkPath, "dist"));
31-
rmIfExists(path.join(frameworkPath, "elm-stuff"));
32-
rmIfExists(path.join(frameworkPath, "bower_components"));
58+
console.log(`cleaning ${frameworkPath}`);
59+
60+
deleteFrameworkFiles(frameworkPath, filesToDelete);
3361
}
3462
}
3563
}
3664

37-
const keyedTypes = ["keyed", "non-keyed"];
38-
const frameworksPath = path.resolve("frameworks");
39-
4065
cleanFrameworkDirectories(frameworksPath, keyedTypes);

configureStyles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require("path");
44

55
const args = yargs(process.argv)
66
.usage("$0 [--bootstrap --minimal]")
7-
.help("help")
7+
.help()
88
.boolean("bootstrap")
99
.default("bootstrap", false)
1010
.boolean("minimal")

rebuild-all.js

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ const takeWhile = require("lodash/takeWhile");
22
const { execSync } = require("child_process");
33
const fs = require("fs");
44
const path = require("path");
5+
const yargs = require("yargs");
6+
7+
const args = yargs(process.argv.slice(2))
8+
.usage("$0 [--ci --docker keyed/framework1 ... non-keyed/frameworkN]")
9+
.help()
10+
.boolean("ci")
11+
.default("ci", false)
12+
.describe("ci", "Use npm ci or npm install?")
13+
.boolean("docker")
14+
.default("docker", false)
15+
.describe(
16+
"docker",
17+
"Copy package-lock back for docker build or build locally?"
18+
).argv;
519

620
/*
721
This script rebuilds all frameworks from scratch,
@@ -12,31 +26,46 @@ If building a framework fails you can resume building like
1226
npm run rebuild-frameworks --restartWith keyed/react
1327
*/
1428

15-
const cliArgs = process.argv.slice(2);
16-
17-
// Use npm ci or npm install ?
18-
const useCi = cliArgs.includes("--ci");
29+
/**
30+
* Use npm ci or npm install?
31+
* @type {boolean}
32+
*/
33+
const useCi = args.ci;
1934

20-
// Copy package-lock back for docker build or build locally?
21-
const useDocker = cliArgs.includes("--docker");
35+
/**
36+
* Copy package-lock back for docker build or build locally?
37+
* @type {boolean}
38+
*/
39+
const useDocker = args.docker;
2240

23-
const restartBuildingWith = cliArgs.find((arg) => !arg.startsWith("--"));
41+
const restartBuildingWith = args._.find((arg) => !arg.startsWith("--"));
2442
const restartWithFramework = restartBuildingWith || "";
2543

2644
console.log(
2745
"ARGS",
46+
args,
2847
"ci",
2948
useCi,
3049
"docker",
3150
useDocker,
3251
"restartWith",
33-
restartWithFramework,
52+
restartWithFramework
3453
);
3554

55+
const filesToDelete = [
56+
"yarn-lock",
57+
"dist",
58+
"elm-stuff",
59+
"bower_components",
60+
"node_modules",
61+
"output",
62+
useCi && "package-lock.json",
63+
].filter(Boolean);
64+
3665
/**
3766
* @typedef {Object} Framework
38-
* @property {string} type - Type of the framework (e.g., "keyed" or "non-keyed")
3967
* @property {string} name - Name of the framework (e.g., "vue", "qwik", "svelte")
68+
* @property {string} type - Type of the framework (e.g., "keyed" or "non-keyed")
4069
*/
4170

4271
/**
@@ -45,13 +74,14 @@ console.log(
4574
* @returns {Framework[]}
4675
*/
4776
function getFrameworks() {
48-
const keyedFrameworks = fs
49-
.readdirSync("./frameworks/keyed")
50-
.map((framework) => ({ type: "keyed", name: framework }));
51-
const nonKeyedFrameworks = fs
52-
.readdirSync("./frameworks/non-keyed")
53-
.map((framework) => ({ type: "non-keyed", name: framework }));
54-
return [...keyedFrameworks, ...nonKeyedFrameworks];
77+
const keyedTypes = ["keyed", "non-keyed"];
78+
const framewokrs = keyedTypes.flatMap((type) =>
79+
fs
80+
.readdirSync(path.join("frameworks", type))
81+
.map((framework) => ({ name: framework, type }))
82+
);
83+
84+
return framewokrs;
5585
}
5686

5787
/**
@@ -68,7 +98,7 @@ function shouldSkipFramework({ type, name }) {
6898
}
6999

70100
/**
71-
* Run a command synchronously in the specified directory
101+
* Run a command synchronously in the specified directory and log command
72102
* @param {string} command - The command to run
73103
* @param {string} cwd - The current working directory (optional)
74104
*/
@@ -111,15 +141,6 @@ function buildFramework(framework) {
111141
// execSync(`rm -r ${path}`);
112142
// }
113143
// rsync(keyed,name);
114-
const filesToDelete = [
115-
"yarn-lock",
116-
"dist",
117-
"elm-stuff",
118-
"bower_components",
119-
"node_modules",
120-
"output",
121-
useCi ? "" : "package-lock.json",
122-
];
123144

124145
deleteFrameworkFiles(frameworkPath, filesToDelete);
125146

@@ -141,7 +162,7 @@ function buildFrameworks() {
141162
const skippableFrameworks = takeWhile(frameworks, shouldSkipFramework);
142163
const buildableFrameworks = frameworks.slice(skippableFrameworks.length);
143164

144-
console.log("Building frameworks:", buildableFrameworks);
165+
// console.log("Building frameworks:", buildableFrameworks);
145166

146167
for (const framework of buildableFrameworks) {
147168
buildFramework(framework);

0 commit comments

Comments
 (0)