qleaner 1.0.5 → 1.0.7
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.
- package/bin/cli.js +14 -5
- package/command.js +73 -7
- package/package.json +1 -1
- package/src/demo/book/hidden.tsx +1 -0
- package/src/demo/demo.tsx +2 -0
- package/src/hello.find.tsx +2 -0
- package/src/layer/shell/shell.tsx +1 -0
- package/src/main.js +2 -1
- package/src/notused.tsx +0 -0
package/bin/cli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const { Command } = require("commander");
|
|
3
|
-
const getFiles = require("../command");
|
|
3
|
+
const { getFiles, unUsedFiles } = require("../command");
|
|
4
4
|
|
|
5
5
|
const program = new Command();
|
|
6
6
|
|
|
@@ -10,16 +10,25 @@ program
|
|
|
10
10
|
.version("1.0.0");
|
|
11
11
|
|
|
12
12
|
program
|
|
13
|
-
.command("qlean")
|
|
13
|
+
.command("qlean-list")
|
|
14
14
|
.description("List all the imports in the project")
|
|
15
15
|
.argument("<path>", "The path to the directory to scan for imports")
|
|
16
16
|
.option("-l, --list-files", "List all the files in the project")
|
|
17
17
|
.option("-i, --list-imports", "List all the imports in the project")
|
|
18
|
+
.option("-e, --exclude-dir <dir>", "Exclude directories from the scan")
|
|
18
19
|
.action(async (path, options) => {
|
|
19
|
-
console.log('path', path);
|
|
20
|
-
console.log('options', options);
|
|
21
20
|
const imports = await getFiles(path, options);
|
|
22
21
|
});
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
program.command("qlean-scan")
|
|
24
|
+
.description("Scan the project for unused files")
|
|
25
|
+
.argument("<path>", "The path to the directory to scan for unused files")
|
|
26
|
+
.option("-e, --exclude-dir <dir>", "Exclude directories from the scan")
|
|
27
|
+
.action(async (path, options) => {
|
|
28
|
+
const unusedFiles = await unUsedFiles(path, options);
|
|
29
|
+
console.clear()
|
|
30
|
+
unusedFiles.forEach((file) => {
|
|
31
|
+
console.log(file);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
25
34
|
program.parse(process.argv);
|
package/command.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
const fg = require("fast-glob");
|
|
2
2
|
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
3
4
|
const parser = require("@babel/parser");
|
|
4
5
|
const traverse = require("@babel/traverse").default;
|
|
5
6
|
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
async function getFiles(directory = "src", options) {
|
|
9
|
+
const contentPaths = [`${directory}/**/*.{tsx,ts,js,jsx}`];
|
|
10
|
+
if (options.excludeDir) {
|
|
11
|
+
contentPaths.push(`!${options.excludeDir}/**`);
|
|
12
|
+
}
|
|
13
|
+
const files = await fg(contentPaths);
|
|
9
14
|
const imports = [];
|
|
10
15
|
for (const file of files) {
|
|
11
16
|
const code = fs.readFileSync(file, "utf8");
|
|
12
17
|
const ast = parser.parse(code, {
|
|
13
18
|
sourceType: "module",
|
|
14
|
-
plugins: [
|
|
15
|
-
"jsx",
|
|
16
|
-
"typescript",
|
|
17
|
-
],
|
|
19
|
+
plugins: ["jsx", "typescript"],
|
|
18
20
|
});
|
|
19
|
-
|
|
20
21
|
traverse(ast, {
|
|
21
22
|
ImportDeclaration: ({ node }) => {
|
|
22
23
|
imports.push({
|
|
@@ -54,5 +55,70 @@ module.exports = async function getFiles(directory = "src", options) {
|
|
|
54
55
|
console.log(`${importStatement.file}:${importStatement.line}:${importStatement.column} ${importStatement.from}`);
|
|
55
56
|
})
|
|
56
57
|
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function unUsedFiles(directory = "src", options) {
|
|
61
|
+
const contentPaths = [`${directory}/**/*.{tsx,ts,js,jsx}`];
|
|
62
|
+
if (options.excludeDir) {
|
|
63
|
+
contentPaths.push(`!${options.excludeDir}/**`);
|
|
64
|
+
}
|
|
65
|
+
const files = await fg(contentPaths);
|
|
66
|
+
const imports = [];
|
|
67
|
+
const unusedFiles = [];
|
|
68
|
+
for (const file of files) {
|
|
69
|
+
const code = fs.readFileSync(file, "utf8");
|
|
70
|
+
const ast = parser.parse(code, {
|
|
71
|
+
sourceType: "module",
|
|
72
|
+
plugins: [
|
|
73
|
+
"jsx",
|
|
74
|
+
"typescript",
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
traverse(ast, {
|
|
79
|
+
ImportDeclaration: ({ node }) => {
|
|
80
|
+
imports.push({
|
|
81
|
+
from: node.source.value,
|
|
82
|
+
file: file,
|
|
83
|
+
line: node.loc.start.line,
|
|
84
|
+
column: node.loc.start.column,
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
}
|
|
57
89
|
|
|
90
|
+
// console.log('imports', imports);
|
|
91
|
+
// console.log('files', files);
|
|
92
|
+
for (const file of files) {
|
|
93
|
+
let i = 0;
|
|
94
|
+
// console.log('Checking', file);
|
|
95
|
+
let isFound = false;
|
|
96
|
+
while (!isFound && i < imports.length) {
|
|
97
|
+
|
|
98
|
+
if (compareFiles(file, imports[i].from)) {
|
|
99
|
+
console.log('Found', file, imports[i].from);
|
|
100
|
+
isFound = true;
|
|
101
|
+
break;
|
|
102
|
+
}else if(i === imports.length - 1) {
|
|
103
|
+
console.log('Not Found', file, imports[i].from);
|
|
104
|
+
unusedFiles.push(file);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
i++;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return unusedFiles;
|
|
58
111
|
}
|
|
112
|
+
|
|
113
|
+
function compareFiles(filePath, importPath) {
|
|
114
|
+
const importNoExt = importPath.replace(/\.[^/.]+$/, "");
|
|
115
|
+
const prefixImportPath = importNoExt.replace(/^(?:\.{1,2}\/|[@~]+\/)+/, "");
|
|
116
|
+
// console.log('importNoExt', importNoExt, ' prefixImportPath', prefixImportPath, ' filePath', filePath, ' ', filePath.includes(prefixImportPath));
|
|
117
|
+
|
|
118
|
+
return filePath.includes(prefixImportPath);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
module.exports = {
|
|
122
|
+
getFiles,
|
|
123
|
+
unUsedFiles,
|
|
124
|
+
};
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import main from '../../main';
|
package/src/demo/demo.tsx
CHANGED
package/src/hello.find.tsx
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import hidden from '../../demo/book/hidden'
|
package/src/main.js
CHANGED
package/src/notused.tsx
ADDED
|
File without changes
|