Skip to content

Commit 9d2ffcb

Browse files
committed
refactor(rebuild-build-single): improve readability
1 parent ad49a81 commit 9d2ffcb

File tree

1 file changed

+81
-50
lines changed

1 file changed

+81
-50
lines changed

rebuild-build-single.js

Lines changed: 81 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
var _ = require('lodash');
2-
var exec = require('child_process').execSync;
3-
var fs = require('fs');
4-
var path = require('path');
5-
var yargs = require('yargs');
6-
const rimraf = require('rimraf');
1+
const { execSync } = require("child_process");
2+
const fs = require("fs");
3+
const path = require("path");
74

8-
let args = process.argv.length <= 2 ? [] : process.argv.slice(2, process.argv.length);
5+
const cliArgs = process.argv.length <= 2 ? [] : process.argv.slice(2);
96

107
// Use npm ci or npm install ?
11-
let ci = args.includes("--ci");
8+
const useCi = cliArgs.includes("--ci");
9+
1210
// Copy package-lock back for docker build or build locally?
13-
let docker = args.includes("--docker");
11+
const useDocker = cliArgs.includes("--docker");
1412

15-
let frameworks = args.filter(a => !a.startsWith("--"));
13+
const frameworks = cliArgs.filter((a) => !a.startsWith("--"));
1614

17-
console.log("rebuild-build-single.js started: args", args, "ci", ci, "docker", docker, "frameworks", frameworks);
15+
console.log(
16+
"rebuild-build-single.js started: args",
17+
cliArgs,
18+
"useCi",
19+
useCi,
20+
"useDocker",
21+
useDocker,
22+
"frameworks",
23+
frameworks
24+
);
1825

1926
/*
2027
rebuild-single.js [--ci] [--docker] [keyed/framework1 ... non-keyed/frameworkN]
@@ -29,48 +36,72 @@ it calls npm ci and npm run build-prod for the benchmark
2936
Pass list of frameworks
3037
*/
3138

39+
/**
40+
* Log command and run it with execSync
41+
* @param {string} command
42+
* @param {string|URL|undefined} cwd
43+
*/
44+
function runCommand(command, cwd = undefined) {
45+
console.log(command);
46+
execSync(command, { stdio: "inherit", cwd });
47+
}
48+
49+
/**
50+
* @param {string} framework
51+
*/
52+
function rebuildFramework(framework) {
53+
const components = framework.split("/");
3254

33-
if (frameworks.length == 0) {
34-
console.log("ERROR: Missing arguments. Command: docker-rebuild keyed/framework1 non-keyed/framework2 ...");
55+
if (components.length !== 2) {
56+
console.log(
57+
`ERROR: invalid name ${framework}. It must contain exactly one /.`
58+
);
3559
process.exit(1);
36-
}
60+
}
3761

38-
for (f of frameworks) {
39-
let components = f.split("/");
40-
if (components.length != 2) {
41-
console.log(`ERROR: invalid name ${f}. It must contain exactly one /.`)
42-
process.exit(1);
43-
}
44-
let [keyed, name] = components;
45-
let path = `frameworks/${keyed}/${name}`
46-
if (docker) {
47-
if (fs.existsSync(path)) {
48-
console.log("deleting folder ", path);
49-
exec(`rm -r ${path}`);
50-
}
51-
let rsync_cmd = `rsync -avC --exclude elm-stuff --exclude dist --exclude output ${ci ? '' : '--exclude package-lock.json'} --exclude tmp --exclude node_modules --exclude bower_components /src/frameworks/${keyed}/${name} /build/frameworks/${keyed}/`;
52-
console.log(rsync_cmd);
53-
exec(rsync_cmd,
54-
{
55-
stdio: 'inherit'
56-
});
57-
}
58-
let rm_cmd = `rm -rf ${ci ? '' : 'package-lock.json'} yarn.lock dist elm-stuff bower_components node_modules output`;
59-
console.log(rm_cmd);
60-
exec(rm_cmd, {
61-
cwd: path,
62-
stdio: 'inherit'
63-
});
64-
let install_cmd = `npm ${ci ? 'ci' : 'install'} && npm run build-prod`;
65-
console.log(install_cmd);
66-
exec(install_cmd, {
67-
cwd: path,
68-
stdio: 'inherit'
69-
});
70-
if (docker) {
71-
let packageLockPath = path + "/package-lock.json";
72-
fs.copyFileSync(packageLockPath, "/src/" + packageLockPath)
62+
const [keyed, name] = components;
63+
const pathToFramework = path.join("frameworks", keyed, name);
64+
65+
if (useDocker) {
66+
if (fs.existsSync(pathToFramework)) {
67+
console.log("deleting folder ", pathToFramework);
68+
fs.rmSync(pathToFramework, { recursive: true });
7369
}
70+
71+
const rsyncCmd = `rsync -avC --exclude elm-stuff --exclude dist --exclude output ${
72+
useCi ? "" : "--exclude package-lock.json"
73+
} --exclude tmp --exclude node_modules --exclude bower_components /src/frameworks/${keyed}/${name} /build/frameworks/${keyed}/`;
74+
runCommand(rsyncCmd);
75+
}
76+
77+
const rmCmd = `rm -rf ${
78+
useCi ? "" : "package-lock.json"
79+
} yarn.lock dist elm-stuff bower_components node_modules output`;
80+
runCommand(rmCmd, pathToFramework);
81+
82+
const installCmd = `npm ${useCi ? "ci" : "install"} && npm run build-prod`;
83+
runCommand(installCmd, pathToFramework);
84+
85+
if (useDocker) {
86+
const packageJSONPath = path.join(pathToFramework, "package-lock.json");
87+
const destinationPath = path.join("/src", packageJSONPath);
88+
fs.copyFileSync(packageJSONPath, destinationPath);
89+
}
90+
}
91+
92+
function rebuildFrameworks() {
93+
if (!frameworks.length) {
94+
console.log(
95+
"ERROR: Missing arguments. Command: docker-rebuild keyed/framework1 non-keyed/framework2 ..."
96+
);
97+
process.exit(1);
98+
}
99+
100+
for (const framework of frameworks) {
101+
rebuildFramework(framework);
102+
}
103+
104+
console.log("rebuild-build-single.js finished: Build finsished sucessfully!");
74105
}
75106

76-
console.log("rebuild-build-single.js finished: Build finsished sucessfully!");
107+
rebuildFrameworks();

0 commit comments

Comments
 (0)