Skip to content

Commit 14dff78

Browse files
committed
refactor: move CLI scripts to the cli directory
1 parent c747026 commit 14dff78

File tree

10 files changed

+160
-131
lines changed

10 files changed

+160
-131
lines changed

cli.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { program } from "commander";
2+
3+
import {
4+
checkObsoleteFrameworks,
5+
cleanFrameworkDirectories,
6+
configureStyles,
7+
copyProjectToDist,
8+
createFrameworkZipArchive,
9+
updateLockfilesOfAllFrameworks,
10+
} from "./cli/index.js";
11+
12+
program.command("zip").action(createFrameworkZipArchive);
13+
14+
program.command("copy").action(copyProjectToDist);
15+
16+
program
17+
.command("check-obsolete")
18+
.option("--debug [boolean]", "", false)
19+
.action((options) => {
20+
checkObsoleteFrameworks(options);
21+
});
22+
23+
program
24+
.command("cleanup")
25+
.option("--frameworks-dir-path [string]", "", "frameworks")
26+
.option("--frameworks-types [Array<string>]", "", ["keyed", "non-keyed"])
27+
.action((options) => {
28+
cleanFrameworkDirectories(options);
29+
});
30+
31+
program
32+
.command("update-lockfiles")
33+
.option("--frameworks-dir-path [string]", "", "frameworks")
34+
.option("--frameworks-types [Array<string>]", "", ["keyed", "non-keyed"])
35+
.option("--latest-lockfile-version [number]", "", 3)
36+
.action((options) => {
37+
updateLockfilesOfAllFrameworks(options);
38+
});
39+
40+
program
41+
.command("configure-styles")
42+
.option("--bootstrap [boolean]", "", false)
43+
.option("--minimal [boolean]", "", false)
44+
.action(async (options) => {
45+
await configureStyles(options);
46+
});
47+
48+
program.parse();

check-obsolete.js renamed to cli/check-obsolete.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1+
import JSON5 from "json5";
12
import { execSync } from "node:child_process";
23
import * as fs from "node:fs";
34
import path from "node:path";
4-
import JSON5 from "json5";
5-
import yargs from "yargs";
6-
import { getFrameworks } from "./utils/frameworks/index.js";
7-
8-
const args = yargs(process.argv)
9-
.usage("$0 [--debug]")
10-
.help()
11-
.boolean("debug")
12-
.default("debug", false).argv;
135

14-
/**
15-
* @type {boolean}
16-
*/
17-
const DEBUG = args.debug;
6+
import { getFrameworks } from "../utils/frameworks/index.js";
187

198
/**
209
* @typedef {Object} Framework
@@ -80,12 +69,12 @@ function maybeObsolete(packageName) {
8069
const isObsolete = modifiedDate < obsoleteDate;
8170
const formattedDate = modifiedDate.toISOString().substring(0, 10);
8271

83-
return { isObsolete, packageName, lastUpdate: formattedDate };
72+
return { isObsolete, lastUpdate: formattedDate, packageName };
8473
} catch (error) {
8574
console.error(
8675
`Failed to execute npm view for ${packageName}. Error Code ${error.status} and message: ${error.message}`,
8776
);
88-
return { isObsolete: false, packageName, lastUpdate: null };
77+
return { isObsolete: false, lastUpdate: null, packageName };
8978
}
9079
}
9180

@@ -95,9 +84,13 @@ const manualChecks = [];
9584
/**
9685
* Checks frameworks in frameworks/keyed and frameworks/non-keyed for obsolescence,
9786
* the presence of package.json and the presence of the frameworkVersionFromPackage property
87+
* @param {Object} options
88+
* @param {boolean} options.debug
9889
*/
99-
function checkFrameworks() {
100-
for (const { type, name } of frameworks) {
90+
function checkObsoleteFrameworks(options) {
91+
const DEBUG = options.debug ?? false;
92+
93+
for (const { name, type } of frameworks) {
10194
const frameworkPath = path.join("frameworks", type, name);
10295
const packageJSONPath = path.join(frameworkPath, "package.json");
10396

@@ -159,4 +152,4 @@ function checkFrameworks() {
159152
);
160153
}
161154

162-
checkFrameworks();
155+
export { checkObsoleteFrameworks };
Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
import * as fs from "node:fs";
22
import path from "node:path";
3-
import yargs from "yargs";
4-
5-
const args = yargs(process.argv)
6-
.help()
7-
.string("framework-dir")
8-
.default("framework-dir", "frameworks")
9-
.array("keyed-types")
10-
.default("keyed-types", ["keyed", "non-keyed"]).argv;
11-
12-
/**
13-
* @type {string}
14-
*/
15-
const frameworksDir = args.frameworkDir;
16-
17-
/**
18-
* @type {string[]}
19-
*/
20-
const keyedTypes = args.keyedTypes;
21-
22-
const frameworksPath = path.resolve(frameworksDir);
233

244
const filesToDelete = [
255
"package-lock.json",
@@ -38,28 +18,30 @@ const filesToDelete = [
3818
function deleteFrameworkFiles(frameworkPath, filesToDelete) {
3919
for (const file of filesToDelete) {
4020
const filePath = path.join(frameworkPath, file);
41-
fs.rmSync(filePath, { recursive: true, force: true });
21+
fs.rmSync(filePath, { force: true, recursive: true });
4222
}
4323
console.log(`Deleted: ${filesToDelete}`);
4424
}
4525

4626
/**
4727
* Cleans all framework directories of package-lock.json, yarn-lock and the elm-stuff, node-modules, bower-components and dist directories.
48-
* @param {string} frameworksPath
49-
* @param {string} keyedTypes
28+
* @param {Object} options
29+
* @param {string} options.frameworksDirPath
30+
* @param {Array<string>} options.frameworksTypes
5031
*/
51-
function cleanFrameworkDirectories(frameworksPath, keyedTypes) {
52-
for (const keyedType of keyedTypes) {
53-
const frameworkDir = path.resolve(frameworksPath, keyedType);
32+
function cleanFrameworkDirectories(options) {
33+
const { frameworksDirPath, frameworksTypes } = options;
34+
35+
for (const frameworkType of frameworksTypes) {
36+
const frameworkDir = path.resolve(frameworksDirPath, frameworkType);
5437
const directories = fs.readdirSync(frameworkDir);
5538

5639
for (const directory of directories) {
5740
const frameworkPath = path.resolve(frameworkDir, directory);
5841
console.log(`cleaning ${frameworkPath}`);
59-
6042
deleteFrameworkFiles(frameworkPath, filesToDelete);
6143
}
6244
}
6345
}
6446

65-
cleanFrameworkDirectories(frameworksPath, keyedTypes);
47+
export { cleanFrameworkDirectories };

configure-styles.js renamed to cli/configure-styles.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import * as fs from "node:fs";
22
import path from "node:path";
3-
import yargs from "yargs";
4-
5-
const args = yargs(process.argv)
6-
.usage("$0 [--bootstrap --minimal]")
7-
.help()
8-
.boolean("bootstrap")
9-
.default("bootstrap", false)
10-
.boolean("minimal")
11-
.default("minimal", false).argv;
123

134
// Function to copy CSS and generate shared-styles.html
145
async function copyAndGenerateSharedStyles(sourceCss, mainCss) {
@@ -27,16 +18,23 @@ async function copyAndGenerateSharedStyles(sourceCss, mainCss) {
2718
);
2819
}
2920

30-
// Main function
31-
async function configureStyles() {
21+
/**
22+
* @param {Object} options
23+
* @param {boolean} options.bootstrap
24+
* @param {boolean} options.minimal
25+
* @returns
26+
*/
27+
async function configureStyles(options) {
28+
const { bootstrap, minimal } = options;
29+
3230
try {
33-
if (args.bootstrap ^ args.minimal) {
31+
if (bootstrap ^ minimal) {
3432
console.log("ERROR: You must either choose bootstrap or minimal");
3533
return;
3634
}
3735

3836
// Read and copy the appropriate CSS file
39-
if (args.bootstrap) {
37+
if (bootstrap) {
4038
await copyAndGenerateSharedStyles(
4139
path.join("css", "useOriginalBootstrap.css"),
4240
path.join("css", "bootstrap", "dist", "css", "bootstrap.min.css"),
@@ -52,4 +50,4 @@ async function configureStyles() {
5250
}
5351
}
5452

55-
configureStyles();
53+
export { configureStyles };

copy.js renamed to cli/copy.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function shouldInclude(name) {
1717
const isTargetWeb = name.includes("/target/web");
1818

1919
console.log(
20-
`File: ${name}\nIs Binding Scala: ${isBindingScala}\nIs Target: ${isTarget}\nIs Target Web: ${isTargetWeb}`
20+
`File: ${name}\nIs Binding Scala: ${isBindingScala}\nIs Target: ${isTarget}\nIs Target Web: ${isTargetWeb}`,
2121
);
2222

2323
if (isTarget) {
@@ -73,7 +73,7 @@ function copyFolderRecursiveSync(sourcePath, destinationPath) {
7373
function processDirectories() {
7474
const directories = fs.readdirSync(".");
7575
const nonHiddenDirectories = directories.filter(
76-
(directory) => !directory.startsWith(".")
76+
(directory) => !directory.startsWith("."),
7777
);
7878

7979
for (const directory of nonHiddenDirectories) {
@@ -99,13 +99,11 @@ function copyProjectToDist() {
9999

100100
fs.copyFileSync(
101101
path.join("webdriver-ts", "table.html"),
102-
path.join("dist", "webdriver-ts", "table.html")
102+
path.join("dist", "webdriver-ts", "table.html"),
103103
);
104104
fs.copyFileSync("index.html", path.join("dist", "index.html"));
105105

106106
processDirectories();
107107
}
108108

109-
copyProjectToDist();
110-
111-
module.exports = { copyProjectToDist };
109+
export { copyProjectToDist };

cli/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { checkObsoleteFrameworks } from "./check-obsolete.js";
2+
export { cleanFrameworkDirectories } from "./cleanup.js";
3+
export { configureStyles } from "./configure-styles.js";
4+
export { copyProjectToDist } from "./copy.js";
5+
export { updateLockfilesOfAllFrameworks } from "./update-lockfiles.js";
6+
export { createFrameworkZipArchive } from "./zip.js";
Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,17 @@
11
import { execSync } from "node:child_process";
2-
import { cwd } from "node:process";
32
import * as fs from "node:fs";
43
import path from "node:path";
5-
import yargs from "yargs";
6-
import { getFrameworks } from "./utils/frameworks/index.js";
7-
8-
const args = yargs(process.argv)
9-
.usage("$0 [--frameworks-dir]")
10-
.help("help")
11-
.string("frameworks-dir")
12-
.default("frameworks-dir", "frameworks")
13-
.array("frameworks-types")
14-
.default("frameworks-types", ["keyed", "non-keyed"])
15-
.number("latest-lockfile-version")
16-
.default("latest-lockfile-version", 3).argv;
17-
18-
/**
19-
* @type {string}
20-
*/
21-
const frameworksDir = args.frameworksDir;
22-
23-
/**
24-
* @type {string[]}
25-
*/
26-
const frameworksTypes = args.frameworksTypes;
4+
import { cwd } from "node:process";
275

28-
/**
29-
* @type {number}
30-
*/
31-
const latestLockfileVersion = args.latestLockfileVersion;
6+
import { getFrameworks } from "../utils/frameworks/index.js";
327

338
/**
349
* @typedef {Object} Framework
3510
* @property {string} name - Name of the framework (e.g., "vue", "qwik", "svelte")
3611
* @property {string} type - Type of the framework (e.g., "keyed" or "non-keyed")
3712
*/
3813

39-
/**
40-
* @param {string} frameworkPath
41-
*/
14+
/** @param {string} frameworkPath */
4215
function runNpmInstall(frameworkPath) {
4316
try {
4417
execSync("npm install --package-lock-only", {
@@ -73,13 +46,19 @@ function getPackageLockJSONVersion(packageLockJSONPath) {
7346
/**
7447
* Updates the lockfile of one framework
7548
* @param {Framework} framework
49+
* @param {number} latestLockfileVersion
50+
* @param {string} frameworkDirPath
7651
*/
77-
function processFramework(framework) {
52+
function updateFrameworkLockfile(
53+
framework,
54+
latestLockfileVersion,
55+
frameworkDirPath,
56+
) {
7857
const { name, type } = framework;
7958

8059
console.log(`Checking ${type} ${name} lockfile`);
8160

82-
const frameworkPath = path.join(cwd(), frameworksDir, type, name);
61+
const frameworkPath = path.join(cwd(), frameworkDirPath, type, name);
8362

8463
const packageLockJSONPath = path.join(frameworkPath, "package-lock.json");
8564
const packageLockJSONVersion = getPackageLockJSONVersion(packageLockJSONPath);
@@ -95,13 +74,23 @@ function processFramework(framework) {
9574

9675
/**
9776
* Updates all frameworks lockfiles in the frameworks directory.
77+
* @param {Object} options
78+
* @param {string} options.frameworksDirPath
79+
* @param {string} options.frameworksTypes
80+
* @param {string} options.latestLockfileVersion
9881
*/
99-
function processAllFrameworks() {
100-
const frameworks = getFrameworks(frameworksDir, frameworksTypes);
82+
function updateLockfilesOfAllFrameworks(options) {
83+
const { frameworksDirPath, frameworksTypes, latestLockfileVersion } = options;
84+
85+
const frameworks = getFrameworks(frameworksDirPath, frameworksTypes);
10186

10287
for (const framework of frameworks) {
103-
processFramework(framework);
88+
updateFrameworkLockfile(
89+
framework,
90+
latestLockfileVersion,
91+
frameworksDirPath,
92+
);
10493
}
10594
}
10695

107-
processAllFrameworks();
96+
export { updateLockfilesOfAllFrameworks };

0 commit comments

Comments
 (0)