Skip to content

Commit 494418e

Browse files
committed
Merge branch 'refactor-cli' of https://github.com/nakrovati/js-framework-benchmark into nakrovati-refactor-cli
2 parents 6b4da1d + 80f8975 commit 494418e

19 files changed

+187
-305
lines changed

cli.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import { program } from "commander";
23

34
import {
@@ -6,43 +7,67 @@ import {
67
configureStyles,
78
copyProjectToDist,
89
createFrameworkZipArchive,
9-
updateLockfilesOfAllFrameworks,
10+
updateFrameworkLockfiles,
11+
rebuildAllFrameworks,
12+
rebuildSingleFramework,
1013
} from "./cli/index.js";
1114

12-
program.command("zip").action(createFrameworkZipArchive);
15+
program.command("zip").description("Create a zip archive of frameworks").action(createFrameworkZipArchive);
1316

14-
program.command("copy").action(copyProjectToDist);
17+
program.command("copy").description("Copy project to dist directory").action(copyProjectToDist);
1518

1619
program
1720
.command("check-obsolete")
21+
.description("Check for obsolete frameworks in the frameworks directory")
1822
.option("--debug [boolean]", "", false)
1923
.action((options) => {
2024
checkObsoleteFrameworks(options);
2125
});
2226

2327
program
2428
.command("cleanup")
29+
.description(
30+
"Clean all framework directories of package-lock.json, yarn-lock and the elm-stuff, node-modules, bower-components and dist directories"
31+
)
2532
.option("--frameworks-dir-path [string]", "", "frameworks")
26-
.option("--frameworks-types [Array<string>]", "", ["keyed", "non-keyed"])
33+
.option("--frameworks-types [types...]", "", ["keyed", "non-keyed"])
2734
.action((options) => {
2835
cleanFrameworkDirectories(options);
2936
});
3037

3138
program
3239
.command("update-lockfiles")
40+
.description("Update lockfiles for all frameworks in the frameworks directory")
3341
.option("--frameworks-dir-path [string]", "", "frameworks")
34-
.option("--frameworks-types [Array<string>]", "", ["keyed", "non-keyed"])
35-
.option("--latest-lockfile-version [number]", "", 3)
42+
.option("--frameworks-types [types...]", "", ["keyed", "non-keyed"])
43+
.option("--latest-lockfile-version [number]", "", "3")
3644
.action((options) => {
37-
updateLockfilesOfAllFrameworks(options);
45+
updateFrameworkLockfiles(options);
3846
});
3947

4048
program
4149
.command("configure-styles")
50+
.description("Configure CSS styles for all frameworks in the frameworks directory")
4251
.option("--bootstrap [boolean]", "", false)
4352
.option("--minimal [boolean]", "", false)
4453
.action(async (options) => {
4554
await configureStyles(options);
4655
});
4756

57+
program
58+
.command("rebuild-all")
59+
.option("--ci [boolean]", "", false)
60+
.option("--restart-with-framework [string]", "", "")
61+
.action((options) => {
62+
rebuildAllFrameworks({ restartWithFramework: options.restartWithFramework, useCi: options.ci });
63+
});
64+
65+
program
66+
.command("rebuild-single")
67+
.option("--frameworks [frameworks...]", "", [])
68+
.option("--ci [boolean]", "", false)
69+
.action((options) => {
70+
rebuildSingleFramework(options);
71+
});
72+
4873
program.parse();

cli/check-obsolete.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
// @ts-check
12
import JSON5 from "json5";
23
import { execSync } from "node:child_process";
34
import * as fs from "node:fs";
45
import path from "node:path";
56

6-
import { getFrameworks } from "../utils/frameworks/index.js";
7+
import { getFrameworks } from "./helpers/frameworks.js";
78

89
/**
910
* @typedef {Object} Framework
@@ -61,7 +62,7 @@ function maybeObsolete(packageName) {
6162

6263
const modifiedDate = new Date(timeData.modified);
6364
const isObsolete = modifiedDate < obsoleteDate;
64-
const formattedDate = modifiedDate.toISOString().substring(0, 10);
65+
const formattedDate = modifiedDate.toISOString().slice(0, 10);
6566

6667
return { isObsolete, lastUpdate: formattedDate, packageName };
6768
} catch (error) {
@@ -81,8 +82,10 @@ const manualChecks = [];
8182
* @param {Object} options
8283
* @param {boolean} options.debug
8384
*/
84-
function checkObsoleteFrameworks(options) {
85-
const DEBUG = options.debug ?? false;
85+
export function checkObsoleteFrameworks({ debug }) {
86+
console.log("Check obsolete frameworks", "debug", debug);
87+
88+
const DEBUG = debug;
8689

8790
for (const { name, type } of frameworks) {
8891
const frameworkPath = path.join("frameworks", type, name);
@@ -93,7 +96,7 @@ function checkObsoleteFrameworks(options) {
9396
continue;
9497
}
9598

96-
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, "utf-8"));
99+
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, "utf8"));
97100
const mainPackages = packageJSON?.["js-framework-benchmark"]?.frameworkVersionFromPackage;
98101

99102
if (!mainPackages) {
@@ -106,7 +109,7 @@ function checkObsoleteFrameworks(options) {
106109
}
107110

108111
const packages = mainPackages.split(":");
109-
const isPackageObsolete = packages.map(maybeObsolete);
112+
const isPackageObsolete = packages.map((element) => maybeObsolete(element));
110113

111114
if (DEBUG) {
112115
console.log(`Results for ${type}/${name} ${isPackageObsolete}`);
@@ -132,5 +135,3 @@ function checkObsoleteFrameworks(options) {
132135
if (manualChecks.length > 0)
133136
console.warn("\nThe following frameworks must be checked manually\n" + manualChecks.join("\n"));
134137
}
135-
136-
export { checkObsoleteFrameworks };

cli/cleanup.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import * as fs from "node:fs";
23
import path from "node:path";
34

@@ -22,8 +23,14 @@ function deleteFrameworkFiles(frameworkPath, filesToDelete) {
2223
* @param {string} options.frameworksDirPath
2324
* @param {Array<string>} options.frameworksTypes
2425
*/
25-
function cleanFrameworkDirectories(options) {
26-
const { frameworksDirPath, frameworksTypes } = options;
26+
export function cleanFrameworkDirectories({ frameworksDirPath, frameworksTypes }) {
27+
console.log(
28+
"Clean framework directories",
29+
"frameworksDirPath",
30+
frameworksDirPath,
31+
"frameworksTypes",
32+
frameworksTypes
33+
);
2734

2835
for (const frameworkType of frameworksTypes) {
2936
const frameworkDir = path.resolve(frameworksDirPath, frameworkType);
@@ -36,5 +43,3 @@ function cleanFrameworkDirectories(options) {
3643
}
3744
}
3845
}
39-
40-
export { cleanFrameworkDirectories };

cli/configure-styles.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import * as fs from "node:fs";
23
import path from "node:path";
34

@@ -19,12 +20,12 @@ async function copyAndGenerateSharedStyles(sourceCss, mainCss) {
1920
* @param {Object} options
2021
* @param {boolean} options.bootstrap
2122
* @param {boolean} options.minimal
22-
* @returns
2323
*/
24-
async function configureStyles(options) {
25-
const { bootstrap, minimal } = options;
24+
export async function configureStyles({ bootstrap, minimal }) {
25+
console.log("Configure styles", "bootstrap", bootstrap, "minimal", minimal);
2626

2727
try {
28+
// @ts-ignore
2829
if (bootstrap ^ minimal) {
2930
console.log("ERROR: You must either choose bootstrap or minimal");
3031
return;
@@ -43,5 +44,3 @@ async function configureStyles(options) {
4344
console.error("An error occurred:", error.message);
4445
}
4546
}
46-
47-
export { configureStyles };

cli/copy.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import * as fs from "node:fs";
23
import path from "node:path";
34

@@ -88,7 +89,9 @@ function processDirectories() {
8889
* Creates a dist directory, copies `table.html` from `webdriver-ts` and `index.html` into it,
8990
* and then starts copying the project folders recursively using `processDirectories()`.
9091
*/
91-
function copyProjectToDist() {
92+
export function copyProjectToDist() {
93+
console.log("Copying project to dist directory");
94+
9295
fs.rmSync("dist", { force: true, recursive: true });
9396
fs.mkdirSync(path.join("dist", "webdriver-ts"), { recursive: true });
9497

@@ -97,5 +100,3 @@ function copyProjectToDist() {
97100

98101
processDirectories();
99102
}
100-
101-
export { copyProjectToDist };

utils/frameworks/index.js renamed to cli/helpers/frameworks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import * as fs from "node:fs";
23
import path from "node:path";
34

cli/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ export { checkObsoleteFrameworks } from "./check-obsolete.js";
22
export { cleanFrameworkDirectories } from "./cleanup.js";
33
export { configureStyles } from "./configure-styles.js";
44
export { copyProjectToDist } from "./copy.js";
5-
export { updateLockfilesOfAllFrameworks } from "./update-lockfiles.js";
5+
export { updateFrameworkLockfiles } from "./update-lockfiles.js";
66
export { createFrameworkZipArchive } from "./zip.js";
7+
export { rebuildAllFrameworks } from "./rebuild-all-frameworks.js";
8+
export { rebuildSingleFramework } from "./rebuild-single-framework.js";

rebuild-all.js renamed to cli/rebuild-all-frameworks.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
// @ts-check
12
import { execSync } from "node:child_process";
23
import * as fs from "node:fs";
34
import path from "node:path";
4-
import yargs from "yargs";
5-
import { takeWhile } from "./utils/common/index.js";
6-
import { getFrameworks } from "./utils/frameworks/index.js";
7-
import { hideBin } from "yargs/helpers";
8-
import esMain from "es-main";
5+
import { takeWhile } from "./utils/index.js";
6+
import { getFrameworks } from "./helpers/frameworks.js";
97

108
/*
119
This script rebuilds all frameworks from scratch,
@@ -29,7 +27,7 @@ npm run rebuild-frameworks --restartWith keyed/react
2927
*/
3028
function shouldSkipFramework({ type, name }, restartWithFramework) {
3129
if (!restartWithFramework) return false;
32-
if (restartWithFramework.indexOf("/") > -1) {
30+
if (restartWithFramework.includes("/")) {
3331
return !`${type}/${name}`.startsWith(restartWithFramework);
3432
} else {
3533
return !name.startsWith(restartWithFramework);
@@ -41,7 +39,7 @@ function shouldSkipFramework({ type, name }, restartWithFramework) {
4139
* @param {string} command - The command to run
4240
* @param {string} [cwd] - The current working directory (optional)
4341
*/
44-
function runCommand(command, cwd = undefined) {
42+
function runCommand(command, cwd) {
4543
console.log(command);
4644
execSync(command, { stdio: "inherit", cwd });
4745
}
@@ -66,7 +64,7 @@ function deleteFrameworkFiles(frameworkPath, filesToDelete) {
6664
* @returns
6765
*/
6866
function buildFramework(framework, useCi) {
69-
console.log("Building framework:", framework);
67+
console.log("Building framework:", "framework", framework, "ci", useCi);
7068

7169
const { type, name } = framework;
7270
const frameworkPath = path.join("frameworks", type, name);
@@ -82,14 +80,9 @@ function buildFramework(framework, useCi) {
8280
// }
8381
// rsync(keyed,name);
8482

85-
const filesToDelete = [
86-
"yarn-lock",
87-
"dist",
88-
"elm-stuff",
89-
"bower_components",
90-
"node_modules",
91-
"output",
92-
].concat(useCi ? [] : ["package-lock.json"]);
83+
const filesToDelete = ["yarn-lock", "dist", "elm-stuff", "bower_components", "node_modules", "output"].concat(
84+
useCi ? [] : ["package-lock.json"]
85+
);
9386

9487
deleteFrameworkFiles(frameworkPath, filesToDelete);
9588

@@ -101,10 +94,13 @@ function buildFramework(framework, useCi) {
10194
}
10295

10396
/**
104-
* @param {string} restartWithFramework
105-
* @param {boolean} useCi
97+
* @param {Object} options
98+
* @param {string} options.restartWithFramework
99+
* @param {boolean} options.useCi
106100
*/
107-
function buildFrameworks(restartWithFramework, useCi) {
101+
export function rebuildAllFrameworks({ restartWithFramework, useCi }) {
102+
console.log("Rebuild all frameworks", "ci", useCi, "restartWith", restartWithFramework);
103+
108104
const frameworks = getFrameworks();
109105

110106
const skippableFrameworks = takeWhile(frameworks, (framework) =>
@@ -120,21 +116,3 @@ function buildFrameworks(restartWithFramework, useCi) {
120116

121117
console.log("All frameworks were built!");
122118
}
123-
124-
if (esMain(import.meta)) {
125-
const args = yargs(hideBin(process.argv))
126-
.usage("$0 [--ci keyed/framework1 ... non-keyed/frameworkN]")
127-
.help()
128-
.boolean("ci")
129-
.default("ci", false)
130-
.default("restartWith", "")
131-
.describe("ci", "Use npm ci or npm install?")
132-
.parseSync();
133-
134-
const useCi = args.ci;
135-
const restartWithFramework = args.restartWith;
136-
137-
console.log("ARGS", args, "ci", useCi, "restartWith", restartWithFramework);
138-
139-
buildFrameworks(restartWithFramework, useCi);
140-
}

cli/rebuild-build-single.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import { execSync } from "node:child_process";
23
import * as fs from "node:fs";
34
import path from "node:path";
@@ -20,13 +21,12 @@ Pass list of frameworks
2021
* @param {string} command - The command to run
2122
* @param {string|undefined} cwd - The current working directory (optional)
2223
*/
23-
function runCommand(command, cwd = undefined) {
24+
function runCommand(command, cwd) {
2425
console.log(command);
25-
if (cwd) {
26-
if (!fs.existsSync(cwd)) {
27-
throw `working directory ${cwd} doesn't exist.`;
28-
}
26+
if (cwd && !fs.existsSync(cwd)) {
27+
throw `working directory ${cwd} doesn't exist.`;
2928
}
29+
3030
execSync(command, { stdio: "inherit", cwd });
3131
}
3232

@@ -58,20 +58,14 @@ function rebuildFramework(framework, useCi) {
5858
const [keyed, name] = components;
5959
const frameworkPath = path.join("frameworks", keyed, name);
6060

61-
const filesToDelete = [
62-
"yarn-lock",
63-
"dist",
64-
"elm-stuff",
65-
"bower_components",
66-
"node_modules",
67-
"output",
68-
].concat(useCi ? [] : ["package-lock.json"]);
61+
const filesToDelete = ["yarn-lock", "dist", "elm-stuff", "bower_components", "node_modules", "output"].concat(
62+
useCi ? [] : ["package-lock.json"]
63+
);
6964

7065
deleteFrameworkFiles(frameworkPath, filesToDelete);
7166

7267
const installCmd = `npm ${useCi ? "ci" : "install"}`;
7368
runCommand(installCmd, frameworkPath);
74-
7569
const buildCmd = "npm run build-prod";
7670
runCommand(buildCmd, frameworkPath);
7771
}
@@ -81,9 +75,9 @@ function rebuildFramework(framework, useCi) {
8175
* @param {boolean} useCi
8276
*/
8377
export function rebuildFrameworks(frameworks, useCi) {
84-
console.log("rebuild-build-single.js started: useCi", useCi, "frameworks", frameworks);
78+
console.log("Rebuild build single: useCi", useCi, "frameworks", frameworks);
8579

86-
if (!frameworks.length) {
80+
if (frameworks.length === 0) {
8781
console.log("ERROR: Missing arguments. Command: rebuild keyed/framework1 non-keyed/framework2 ...");
8882
process.exit(1);
8983
}
@@ -94,4 +88,3 @@ export function rebuildFrameworks(frameworks, useCi) {
9488

9589
console.log("rebuild-build-single.js finished: Build finsished sucessfully!");
9690
}
97-

0 commit comments

Comments
 (0)