@uxf/scripts 11.23.2 → 11.28.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ require("../src/uxf-unused/cli")()
3
+ .then((exitCode) => {
4
+ process.exitCode = exitCode;
5
+ })
6
+ .catch((e) => {
7
+ console.error(e);
8
+ process.exitCode = 1;
9
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/scripts",
3
- "version": "11.23.2",
3
+ "version": "11.28.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -9,7 +9,8 @@
9
9
  "uxf-release": "bin/uxf-release.js",
10
10
  "uxf-sitemap-check": "bin/uxf-sitemap-check.js",
11
11
  "uxf-sitemap-meta-export": "bin/uxf-sitemap-meta-export.js",
12
- "uxf-i18n-namespaces-gen": "bin/uxf-i18n-namespaces-gen.js"
12
+ "uxf-i18n-namespaces-gen": "bin/uxf-i18n-namespaces-gen.js",
13
+ "uxf-unused": "bin/uxf-unused.js"
13
14
  },
14
15
  "scripts": {
15
16
  "build": "",
@@ -29,7 +30,8 @@
29
30
  "axios": "1.6.7",
30
31
  "cheerio": "1.0.0-rc.12",
31
32
  "dayjs": "1.11.10",
32
- "madge": "6.1.0",
33
+ "fast-glob": "3.3.2",
34
+ "madge": "8.0.0",
33
35
  "yargs": "17.7.2"
34
36
  }
35
37
  }
@@ -0,0 +1,50 @@
1
+ const fg = require("fast-glob");
2
+ const madge = require("madge");
3
+ const path = require("path");
4
+ const fs = require("fs");
5
+
6
+ function getTSConfig() {
7
+ const tsConfigPath = path.resolve(process.cwd(), "tsconfig.json");
8
+ return fs.existsSync(tsConfigPath) ? tsConfigPath : undefined;
9
+ }
10
+
11
+ function getTree(subtree, tree, destination, viewedFiles = []) {
12
+ if (!subtree || subtree.length === 0) {
13
+ return;
14
+ }
15
+
16
+ for (let child of subtree) {
17
+ if (viewedFiles.includes(child)) {
18
+ continue;
19
+ }
20
+
21
+ const nextSubtree = tree[child];
22
+
23
+ viewedFiles.push(child);
24
+
25
+ if (nextSubtree) {
26
+ destination.push(nextSubtree);
27
+ }
28
+
29
+ if (nextSubtree && nextSubtree.length > 0) {
30
+ getTree(nextSubtree, tree, destination, viewedFiles);
31
+ }
32
+ }
33
+ }
34
+
35
+ module.exports = function loadPageImports(directory, fileExtensions = ["ts", "tsx"]) {
36
+ const pages = fg.sync(directory);
37
+
38
+ return madge(process.cwd(), { tsConfig: getTSConfig(), fileExtensions }).then((res) => {
39
+ const tree = res.obj();
40
+
41
+ return pages.reduce((previousValue, currentValue) => {
42
+ const filesOnPath = [];
43
+ getTree([currentValue], tree, filesOnPath);
44
+
45
+ previousValue[currentValue] = Array.from(new Set(filesOnPath.flat(Number.POSITIVE_INFINITY)));
46
+
47
+ return previousValue;
48
+ }, {});
49
+ });
50
+ };
@@ -0,0 +1,29 @@
1
+ const { argv, env } = require("process");
2
+
3
+ module.exports = async () => {
4
+ const cli = require("yargs")
5
+ .command("$0", "UXF find and remove unused files NextJS project", (yargs) => {
6
+ yargs.demandCommand(0, 0).usage(`
7
+ Usage:
8
+ uxf-unused [options]`);
9
+ })
10
+ .option("p", { alias: "pagesDirectory", default: "src/pages/**/*.(ts|tsx)" })
11
+ .option("f", { alias: "allFilesDirectory", default: "src/**/*.(ts|tsx)" })
12
+ .option("r", { alias: "removeFiles", boolean: true, default: false })
13
+ .option("h", { alias: "help" })
14
+ .strict(false)
15
+ .exitProcess(false);
16
+
17
+ try {
18
+ const { help, pagesDirectory, allFilesDirectory, removeFiles } = cli.parse(argv.slice(2));
19
+
20
+ if (Boolean(help)) {
21
+ return 0;
22
+ }
23
+
24
+ await require("./index")(pagesDirectory, allFilesDirectory, removeFiles);
25
+ } catch (e) {
26
+ console.error(e);
27
+ return 1;
28
+ }
29
+ };
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env node
2
+ const loadPageImports = require("../shared/load-page-imports");
3
+ const fg = require("fast-glob");
4
+ const path = require("path");
5
+ const fs = require("fs");
6
+
7
+ function unique(value, index, array) {
8
+ return array.indexOf(value) === index;
9
+ }
10
+
11
+ async function main(pagesDirectory, allFilesDirectory, shouldRemoveFiles) {
12
+ const pageImports = await loadPageImports(pagesDirectory);
13
+
14
+ const pages = Object.keys(pageImports);
15
+
16
+ const usedFiles = Object.values(pageImports).flat().filter(unique).map(path.normalize);
17
+ const allFiles = fg.sync(allFilesDirectory).map(path.normalize);
18
+
19
+ allFiles
20
+ .filter((e) => !usedFiles.includes(e) && !pages.includes(e))
21
+ .forEach((unusedFile) => {
22
+ if (shouldRemoveFiles) {
23
+ fs.rmSync(unusedFile);
24
+ }
25
+ console.log(unusedFile);
26
+ });
27
+ }
28
+
29
+ module.exports = main;