forked from krausest/js-framework-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild-all.js
152 lines (128 loc) · 4.19 KB
/
rebuild-all.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const takeWhile = require("lodash/takeWhile");
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
/*
This script rebuilds all frameworks from scratch,
it deletes all package.json and package-lock.json files
and invokes npm install and npm run build-prod for all benchmarks
If building a framework fails you can resume building like
npm run rebuild-frameworks --restartWith keyed/react
*/
const cliArgs = process.argv.slice(2);
// Use npm ci or npm install ?
const useCi = cliArgs.includes("--ci");
// Copy package-lock back for docker build or build locally?
const useDocker = cliArgs.includes("--docker");
const restartBuildingWith = cliArgs.find((arg) => !arg.startsWith("--"));
const restartWithFramework = restartBuildingWith || "";
console.log(
"ARGS",
"ci",
useCi,
"docker",
useDocker,
"restartWith",
restartWithFramework,
);
/**
* @typedef {Object} Framework
* @property {string} type - Type of the framework (e.g., "keyed" or "non-keyed")
* @property {string} name - Name of the framework (e.g., "vue", "qwik", "svelte")
*/
/**
* Returns an array of frameworks with their type and name
* @example getFramewokrs()
* @returns {Framework[]}
*/
function getFrameworks() {
const keyedFrameworks = fs
.readdirSync("./frameworks/keyed")
.map((framework) => ({ type: "keyed", name: framework }));
const nonKeyedFrameworks = fs
.readdirSync("./frameworks/non-keyed")
.map((framework) => ({ type: "non-keyed", name: framework }));
return [...keyedFrameworks, ...nonKeyedFrameworks];
}
/**
* @param {Framework}
* @returns {boolean}
*/
function shouldSkipFramework({ type, name }) {
if (!restartWithFramework) return false;
if (restartWithFramework.indexOf("/") > -1) {
return !`${type}/${name}`.startsWith(restartWithFramework);
} else {
return !name.startsWith(restartWithFramework);
}
}
/**
* Run a command synchronously in the specified directory
* @param {string} command - The command to run
* @param {string} cwd - The current working directory (optional)
*/
function runCommand(command, cwd = undefined) {
console.log(command);
execSync(command, { stdio: "inherit", cwd });
}
/**
* Delete specified files in the framework directory
* @param {string} frameworkPath
* @param {string[]} filesToDelete
*/
function deleteFrameworkFiles(frameworkPath, filesToDelete) {
for (const file of filesToDelete) {
const filePath = path.join(frameworkPath, file);
fs.rmSync(filePath, { recursive: true, force: true });
}
console.log(`Deleted: ${filesToDelete}`);
}
/**
* Build single framework
* @param {Framework} framework
* @returns
*/
function buildFramework(framework) {
console.log("Building framework:", framework);
const { type, name } = framework;
const frameworkPath = path.join("frameworks", type, name);
const packageJSONPath = path.join(frameworkPath, "package.json");
if (!fs.existsSync(packageJSONPath)) {
console.log(`WARN: skipping ${framework} since there's no package.json`);
return;
}
// if (fs.existsSync(path)) {
// console.log("deleting folder ",path);
// execSync(`rm -r ${path}`);
// }
// rsync(keyed,name);
const filesToDelete = ((useCi) ? [] : ["package-lock.json"]).concat([
"yarn-lock",
"dist",
"elm-stuff",
"bower_components",
"node_modules",
"output",
]);
deleteFrameworkFiles(frameworkPath, filesToDelete);
const installCmd = `npm ${useCi ? "ci" : "install"}`;
runCommand(installCmd, frameworkPath);
const buildCmd = "npm run build-prod";
runCommand(buildCmd, frameworkPath);
if (useDocker) {
const packageLockPath = path.join(frameworkPath, "package-lock.json");
const destinationPath = path.join("/src", packageLockPath);
fs.copyFileSync(packageLockPath, destinationPath);
}
}
function buildFrameworks() {
const frameworks = getFrameworks();
const skippableFrameworks = takeWhile(frameworks, shouldSkipFramework);
const buildableFrameworks = frameworks.slice(skippableFrameworks.length);
console.log("Building frameworks:", buildableFrameworks);
for (const framework of buildableFrameworks) {
buildFramework(framework);
}
console.log("All frameworks were built!");
}
buildFrameworks();