Skip to content

Commit 7767863

Browse files
committed
perf, refactor: improve rebuild-all.js
Add or fix JSDoc comments. Replace deleting files with execSync with fs.rmSync. As a result, the execution time of the rebuildFrameworks function decreased by ~10 times (*1) and memory consumption by 50% (*1). Improve performance of getting args. Move the rebuild of the framework into the rebuildFramework function. Change the framework view from the [dir,name] array to the {type, name} object. *1 - Measured without installCmd and buildCmd calls.
1 parent ad49a81 commit 7767863

File tree

1 file changed

+69
-51
lines changed

1 file changed

+69
-51
lines changed

rebuild-all.js

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ If building a framework fails you can resume building like
1212
npm run rebuild-frameworks --restartWith keyed/react
1313
*/
1414

15-
const [, , ...cliArgs] = process.argv;
15+
const cliArgs = process.argv.slice(2);
1616

1717
// Use npm ci or npm install ?
1818
const useCi = cliArgs.includes("--ci");
@@ -30,70 +30,109 @@ console.log(
3030
"docker",
3131
useDocker,
3232
"restartWith",
33-
restartWithFramework
33+
restartWithFramework,
3434
);
3535

3636
/**
37-
* Returns an array with arrays of types and names of frameworks
37+
* @typedef {Object} Framework
38+
* @property {string} type - Type of the framework (e.g., "keyed" or "non-keyed")
39+
* @property {string} name - Name of the framework (e.g., "vue", "qwik", "svelte")
40+
*/
41+
42+
/**
43+
* Returns an array of frameworks with their type and name
3844
* @example getFramewokrs()
39-
* @returns [["keyed", "vue"],["keyed", "qwik"],["non-keyed", "svelte"]]
45+
* @returns {Framework[]}
4046
*/
4147
function getFrameworks() {
4248
const keyedFrameworks = fs
4349
.readdirSync("./frameworks/keyed")
44-
.map((framework) => ["keyed", framework]);
50+
.map((framework) => ({ type: "keyed", name: framework }));
4551
const nonKeyedFrameworks = fs
4652
.readdirSync("./frameworks/non-keyed")
47-
.map((framework) => ["non-keyed", framework]);
53+
.map((framework) => ({ type: "non-keyed", name: framework }));
4854
return [...keyedFrameworks, ...nonKeyedFrameworks];
4955
}
5056

5157
/**
52-
* @param {[string,string]} tuple
58+
* @param {Framework}
5359
* @returns {boolean}
5460
*/
55-
function shouldSkipFramework([dir, name]) {
61+
function shouldSkipFramework({ type, name }) {
5662
if (!restartWithFramework) return false;
5763
if (restartWithFramework.indexOf("/") > -1) {
58-
return !`${dir}/${name}`.startsWith(restartWithFramework);
64+
return !`${type}/${name}`.startsWith(restartWithFramework);
5965
} else {
6066
return !name.startsWith(restartWithFramework);
6167
}
6268
}
6369

6470
/**
65-
* @param {string} frameworkPath
71+
* Run a command synchronously in the specified directory
72+
* @param {string} command - The command to run
73+
* @param {string} cwd - The current working directory (optional)
6674
*/
67-
function removeFiles(frameworkPath) {
68-
const rmCmd = `rm -rf ${
69-
useCi ? "" : "package-lock.json"
70-
} yarn.lock dist elm-stuff bower_components node_modules output`;
71-
console.log(rmCmd);
72-
execSync(rmCmd, {
73-
cwd: frameworkPath,
74-
stdio: "inherit",
75-
});
75+
function runCommand(command, cwd = undefined) {
76+
console.log(command);
77+
execSync(command, { stdio: "inherit", cwd });
7678
}
7779

7880
/**
81+
* Delete specified files in the framework directory
7982
* @param {string} frameworkPath
83+
* @param {string[]} filesToDelete
8084
*/
81-
function installAndBuild(frameworkPath) {
82-
const installCmd = `npm ${useCi ? "ci" : "install"} && npm run build-prod`;
83-
console.log(installCmd);
84-
execSync(installCmd, {
85-
cwd: frameworkPath,
86-
stdio: "inherit",
87-
});
85+
function deleteFrameworkFiles(frameworkPath, filesToDelete) {
86+
for (const file of filesToDelete) {
87+
const filePath = path.join(frameworkPath, file);
88+
fs.rmSync(filePath, { recursive: true, force: true });
89+
}
90+
console.log(`Deleted: ${filesToDelete}`);
8891
}
8992

9093
/**
91-
* @param {string} frameworkPath
94+
* Build single framework
95+
* @param {Framework} framework
96+
* @returns
9297
*/
93-
function copyPackageLock(frameworkPath) {
98+
function buildFramework(framework) {
99+
console.log("Building framework:", framework);
100+
101+
const { type, name } = framework;
102+
const frameworkPath = path.join("frameworks", type, name);
103+
const packageJSONPath = path.join(frameworkPath, "package.json");
104+
105+
if (!fs.existsSync(packageJSONPath)) {
106+
console.log(`WARN: skipping ${framework} since there's no package.json`);
107+
return;
108+
}
109+
// if (fs.existsSync(path)) {
110+
// console.log("deleting folder ",path);
111+
// execSync(`rm -r ${path}`);
112+
// }
113+
// 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+
];
123+
124+
deleteFrameworkFiles(frameworkPath, filesToDelete);
125+
126+
const installCmd = `npm ${useCi ? "ci" : "install"}`;
127+
runCommand(installCmd, frameworkPath);
128+
129+
const buildCmd = "npm run build-prod";
130+
runCommand(buildCmd, frameworkPath);
131+
94132
if (useDocker) {
95133
const packageLockPath = path.join(frameworkPath, "package-lock.json");
96-
fs.copyFileSync(packageLockPath, path.join("/src", packageLockPath));
134+
const destinationPath = path.join("/src", packageLockPath);
135+
fs.copyFileSync(packageLockPath, destinationPath);
97136
}
98137
}
99138

@@ -105,28 +144,7 @@ function buildFrameworks() {
105144
console.log("Building frameworks:", buildableFrameworks);
106145

107146
for (const framework of buildableFrameworks) {
108-
console.log("Building framework:", framework);
109-
110-
const [keyed, name] = framework;
111-
const frameworkPath = path.join("frameworks", keyed, name);
112-
113-
if (!fs.existsSync(`${frameworkPath}/package.json`)) {
114-
console.log(
115-
"WARN: skipping ",
116-
framework,
117-
" since there's no package.json"
118-
);
119-
continue;
120-
}
121-
// if (fs.existsSync(path)) {
122-
// console.log("deleting folder ",path);
123-
// execSync(`rm -r ${path}`);
124-
// }
125-
// rsync(keyed,name);
126-
127-
removeFiles(frameworkPath);
128-
installAndBuild(frameworkPath);
129-
copyPackageLock(frameworkPath);
147+
buildFramework(framework);
130148
}
131149

132150
console.log("All frameworks were built!");

0 commit comments

Comments
 (0)