Skip to content

Commit 17ef1bc

Browse files
committed
refactor(cli): improve code typing and directory structure
Move all cli files to the `cli` directory. Replace `takeWhile.d.ts` with jsdoc types for `takeWhile` function. Fix types. Remove the ability to call cli functions directly (e.g. calling `node copy.js`). Now the same-named functions from cli commands are exported, and all commands can be called via `cli.js`. And some other improvements.
1 parent cf2e583 commit 17ef1bc

17 files changed

+145
-148
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: 6 additions & 5 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
@@ -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);
@@ -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: 12 additions & 34 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";
55
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";
6+
import { getFrameworks } from "./helpers/frameworks.js";
97

108
/*
119
This script rebuilds all frameworks from scratch,
@@ -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: 7 additions & 12 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";
@@ -27,6 +28,7 @@ function runCommand(command, cwd = undefined) {
2728
throw `working directory ${cwd} doesn't exist.`;
2829
}
2930
}
31+
3032
execSync(command, { stdio: "inherit", cwd });
3133
}
3234

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

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"]);
63+
const filesToDelete = ["yarn-lock", "dist", "elm-stuff", "bower_components", "node_modules", "output"].concat(
64+
useCi ? [] : ["package-lock.json"]
65+
);
6966

7067
deleteFrameworkFiles(frameworkPath, filesToDelete);
7168

7269
const installCmd = `npm ${useCi ? "ci" : "install"}`;
7370
runCommand(installCmd, frameworkPath);
74-
7571
const buildCmd = "npm run build-prod";
7672
runCommand(buildCmd, frameworkPath);
7773
}
@@ -81,9 +77,9 @@ function rebuildFramework(framework, useCi) {
8177
* @param {boolean} useCi
8278
*/
8379
export function rebuildFrameworks(frameworks, useCi) {
84-
console.log("rebuild-build-single.js started: useCi", useCi, "frameworks", frameworks);
80+
console.log("Rebuild build single: useCi", useCi, "frameworks", frameworks);
8581

86-
if (!frameworks.length) {
82+
if (frameworks.length === 0) {
8783
console.log("ERROR: Missing arguments. Command: rebuild keyed/framework1 non-keyed/framework2 ...");
8884
process.exit(1);
8985
}
@@ -94,4 +90,3 @@ export function rebuildFrameworks(frameworks, useCi) {
9490

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

cli/rebuild-check-single.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import { execSync } from "node:child_process";
23

34
/*
@@ -23,10 +24,11 @@ function runCommand(command, cwd = undefined) {
2324
}
2425

2526
/**
26-
* @param {string[]} frameworks
27+
* @param {Object} options
28+
* @param {Array<string>} options.frameworks
2729
*/
28-
export function rebuildCheckSingle(frameworks) {
29-
console.log("rebuild-check-single.js started: frameworks", frameworks);
30+
export function rebuildCheckSingle({ frameworks }) {
31+
console.log("Rebuild check single:", "frameworks", frameworks);
3032

3133
const frameworkNames = frameworks.join(" ");
3234

0 commit comments

Comments
 (0)