deslop-cli 0.0.10
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/LICENSE +21 -0
- package/README.md +67 -0
- package/dist/cli.mjs +146 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Million Software, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# deslop-cli
|
|
2
|
+
|
|
3
|
+
CLI for [deslop-js](https://github.com/aidenybai/deslop-js) — find unused files, exports, dependencies, and circular imports.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g deslop-cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Node.js 22 or later.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Pass an explicit project root when possible (especially in monorepos):
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
deslop ./my-app
|
|
19
|
+
deslop analyze ./my-app
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Analyze the current directory:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
deslop
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Output JSON:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
deslop ./my-app --json
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Fail CI when unused code is found (files, exports, or dependencies — not circular imports):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
deslop ./my-app --fail-on-issues
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Fail CI when circular imports are found:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
deslop ./my-app --fail-on-cycles
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Options
|
|
47
|
+
|
|
48
|
+
| Option | Description |
|
|
49
|
+
| ------------------------- | ----------------------------------------------------------------- |
|
|
50
|
+
| `[root]` | Project root directory (default: `.`; must exist) |
|
|
51
|
+
| `-e, --entry <pattern>` | Entry point glob patterns |
|
|
52
|
+
| `-i, --ignore <pattern>` | Glob patterns to exclude |
|
|
53
|
+
| `--extensions <ext>` | File extensions to scan (e.g. `.ts` `.vue`) |
|
|
54
|
+
| `--tsconfig <path>` | Path to tsconfig.json for alias resolution |
|
|
55
|
+
| `--report-types` | Include type-only exports in results |
|
|
56
|
+
| `--include-entry-exports` | Report unused exports from entry files |
|
|
57
|
+
| `--json` | Output results as JSON |
|
|
58
|
+
| `--fail-on-issues` | Exit 1 when unused files, exports, or dependencies are found |
|
|
59
|
+
| `--fail-on-cycles` | Exit 1 when circular imports are found |
|
|
60
|
+
|
|
61
|
+
### Exit codes
|
|
62
|
+
|
|
63
|
+
| Code | Meaning |
|
|
64
|
+
| ---- | -------------------------------------------- |
|
|
65
|
+
| `0` | Success (no failure flags triggered) |
|
|
66
|
+
| `1` | Issues found (per `--fail-on-*` flags) or runtime error |
|
|
67
|
+
| `2` | Invalid project root |
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { analyze, defineConfig } from "deslop-js";
|
|
4
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
import { dirname, join, resolve } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
|
|
8
|
+
//#region src/constants.ts
|
|
9
|
+
const PACKAGE_JSON_FILENAME = "package.json";
|
|
10
|
+
const MISSING_PACKAGE_JSON_WARNING = "Warning: no package.json found in project root; dependency analysis may be incomplete.";
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/format-result.ts
|
|
14
|
+
const formatIssueCount = (count, singularLabel, pluralLabel) => {
|
|
15
|
+
return `${count} unused ${count === 1 ? singularLabel : pluralLabel}`;
|
|
16
|
+
};
|
|
17
|
+
const formatHumanReadableResult = (result) => {
|
|
18
|
+
const lines = [];
|
|
19
|
+
lines.push(`Analyzed ${result.totalFiles} files (${result.totalExports} exports) in ${result.analysisTimeMs.toFixed(0)}ms`);
|
|
20
|
+
lines.push("");
|
|
21
|
+
if (result.unusedFiles.length > 0) {
|
|
22
|
+
lines.push(formatIssueCount(result.unusedFiles.length, "file", "files"));
|
|
23
|
+
for (const unusedFile of result.unusedFiles) lines.push(` ${unusedFile.path}`);
|
|
24
|
+
lines.push("");
|
|
25
|
+
}
|
|
26
|
+
if (result.unusedExports.length > 0) {
|
|
27
|
+
lines.push(formatIssueCount(result.unusedExports.length, "export", "exports"));
|
|
28
|
+
for (const unusedExport of result.unusedExports) lines.push(` ${unusedExport.path}:${unusedExport.line} ${unusedExport.name}`);
|
|
29
|
+
lines.push("");
|
|
30
|
+
}
|
|
31
|
+
if (result.unusedDependencies.length > 0) {
|
|
32
|
+
lines.push(formatIssueCount(result.unusedDependencies.length, "dependency", "dependencies"));
|
|
33
|
+
for (const unusedDependency of result.unusedDependencies) {
|
|
34
|
+
const dependencyKind = unusedDependency.isDevDependency ? "dev" : "prod";
|
|
35
|
+
lines.push(` ${unusedDependency.name} (${dependencyKind})`);
|
|
36
|
+
}
|
|
37
|
+
lines.push("");
|
|
38
|
+
}
|
|
39
|
+
if (result.circularDependencies.length > 0) {
|
|
40
|
+
const cycleLabel = result.circularDependencies.length === 1 ? "cycle" : "cycles";
|
|
41
|
+
lines.push(`${result.circularDependencies.length} circular ${cycleLabel}`);
|
|
42
|
+
for (const circularDependency of result.circularDependencies) lines.push(` ${circularDependency.files.join(" → ")}`);
|
|
43
|
+
lines.push("");
|
|
44
|
+
}
|
|
45
|
+
if (result.unusedFiles.length + result.unusedExports.length + result.unusedDependencies.length + result.circularDependencies.length === 0) lines.push("No unused files, exports, dependencies, or circular imports found.");
|
|
46
|
+
return lines.join("\n").trimEnd() + "\n";
|
|
47
|
+
};
|
|
48
|
+
const hasUnusedIssues = (result) => result.unusedFiles.length > 0 || result.unusedExports.length > 0 || result.unusedDependencies.length > 0;
|
|
49
|
+
const hasCircularIssues = (result) => result.circularDependencies.length > 0;
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/utils/validate-root-directory.ts
|
|
53
|
+
const validateRootDirectory = (root) => {
|
|
54
|
+
const resolvedPath = resolve(root);
|
|
55
|
+
if (!existsSync(resolvedPath)) return {
|
|
56
|
+
isValid: false,
|
|
57
|
+
resolvedPath,
|
|
58
|
+
errorMessage: `Project root does not exist: ${resolvedPath}`,
|
|
59
|
+
missingPackageJson: false
|
|
60
|
+
};
|
|
61
|
+
if (!statSync(resolvedPath).isDirectory()) return {
|
|
62
|
+
isValid: false,
|
|
63
|
+
resolvedPath,
|
|
64
|
+
errorMessage: `Project root is not a directory: ${resolvedPath}`,
|
|
65
|
+
missingPackageJson: false
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
isValid: true,
|
|
69
|
+
resolvedPath,
|
|
70
|
+
missingPackageJson: !existsSync(join(resolvedPath, PACKAGE_JSON_FILENAME))
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/run-analyze.ts
|
|
76
|
+
const defaultAnalyzeOutput = () => ({
|
|
77
|
+
stdout: process.stdout,
|
|
78
|
+
stderr: process.stderr
|
|
79
|
+
});
|
|
80
|
+
const resolveAnalyzeExitCode = (result, options) => {
|
|
81
|
+
if (options.failOnIssues && hasUnusedIssues(result)) return 1;
|
|
82
|
+
if (options.failOnCycles && hasCircularIssues(result)) return 1;
|
|
83
|
+
return 0;
|
|
84
|
+
};
|
|
85
|
+
const runAnalyze = async (options, output = defaultAnalyzeOutput()) => {
|
|
86
|
+
const rootValidation = validateRootDirectory(options.root);
|
|
87
|
+
if (!rootValidation.isValid) {
|
|
88
|
+
output.stderr.write(`deslop: ${rootValidation.errorMessage}\n`);
|
|
89
|
+
return 2;
|
|
90
|
+
}
|
|
91
|
+
if (rootValidation.missingPackageJson) output.stderr.write(`deslop: ${MISSING_PACKAGE_JSON_WARNING}\n`);
|
|
92
|
+
const result = await analyze(defineConfig({
|
|
93
|
+
rootDir: rootValidation.resolvedPath,
|
|
94
|
+
entryPatterns: options.entry,
|
|
95
|
+
ignorePatterns: options.ignore ?? [],
|
|
96
|
+
includeExtensions: options.extensions,
|
|
97
|
+
tsConfigPath: options.tsconfig,
|
|
98
|
+
reportTypes: options.reportTypes,
|
|
99
|
+
includeEntryExports: options.includeEntryExports
|
|
100
|
+
}));
|
|
101
|
+
if (options.json) output.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
102
|
+
else output.stdout.write(formatHumanReadableResult(result));
|
|
103
|
+
return resolveAnalyzeExitCode(result, options);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/utils/read-package-version.ts
|
|
108
|
+
const isPackageJsonWithVersion = (value) => typeof value === "object" && value !== null && "version" in value && typeof value.version === "string";
|
|
109
|
+
const readPackageVersion = (moduleUrl) => {
|
|
110
|
+
const packageJsonPath = resolve(dirname(fileURLToPath(moduleUrl)), "../package.json");
|
|
111
|
+
const parsedJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
112
|
+
if (!isPackageJsonWithVersion(parsedJson)) throw new Error(`Invalid package.json at ${packageJsonPath}: missing version field`);
|
|
113
|
+
return parsedJson.version;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/cli.ts
|
|
118
|
+
const toAnalyzeOptions = (root, optionValues) => ({
|
|
119
|
+
root: root ?? ".",
|
|
120
|
+
entry: optionValues.entry,
|
|
121
|
+
ignore: optionValues.ignore,
|
|
122
|
+
extensions: optionValues.extensions,
|
|
123
|
+
tsconfig: optionValues.tsconfig,
|
|
124
|
+
reportTypes: Boolean(optionValues.reportTypes),
|
|
125
|
+
includeEntryExports: Boolean(optionValues.includeEntryExports),
|
|
126
|
+
json: Boolean(optionValues.json),
|
|
127
|
+
failOnIssues: Boolean(optionValues.failOnIssues),
|
|
128
|
+
failOnCycles: Boolean(optionValues.failOnCycles)
|
|
129
|
+
});
|
|
130
|
+
const runAnalyzeAction = async (root, optionValues) => {
|
|
131
|
+
const exitCode = await runAnalyze(toAnalyzeOptions(root, optionValues));
|
|
132
|
+
process.exitCode = exitCode;
|
|
133
|
+
};
|
|
134
|
+
const addAnalyzeOptions = (command) => command.argument("[root]", "project root directory", ".").option("-e, --entry <pattern...>", "entry point glob patterns").option("-i, --ignore <pattern...>", "glob patterns to exclude from analysis").option("--extensions <extension...>", "file extensions to scan (e.g. .ts .vue)").option("--tsconfig <path>", "path to tsconfig.json for path alias resolution").option("--report-types", "include type-only exports in results").option("--include-entry-exports", "report unused exports from entry files").option("--json", "output results as JSON").option("--fail-on-issues", "exit with code 1 when unused files, exports, or dependencies are found").option("--fail-on-cycles", "exit with code 1 when circular imports are found");
|
|
135
|
+
const program = new Command();
|
|
136
|
+
program.name("deslop").description("Find unused files, exports, dependencies, and circular imports in JavaScript projects").version(readPackageVersion(import.meta.url));
|
|
137
|
+
addAnalyzeOptions(program).action(runAnalyzeAction);
|
|
138
|
+
addAnalyzeOptions(program.command("analyze").description("Find unused files, exports, dependencies, and circular imports")).action(runAnalyzeAction);
|
|
139
|
+
program.parseAsync(process.argv).catch((error) => {
|
|
140
|
+
const message = error instanceof Error ? error.stack ?? error.message : String(error);
|
|
141
|
+
process.stderr.write(`deslop: ${message}\n`);
|
|
142
|
+
process.exitCode = 1;
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "deslop-cli",
|
|
3
|
+
"version": "0.0.10",
|
|
4
|
+
"description": "CLI for deslop-js — find unused files, exports, and dependencies",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cli",
|
|
7
|
+
"dead-code",
|
|
8
|
+
"dependencies",
|
|
9
|
+
"exports",
|
|
10
|
+
"javascript",
|
|
11
|
+
"typescript",
|
|
12
|
+
"unused"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Aiden Bai",
|
|
17
|
+
"email": "aiden@million.dev"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"bin": {
|
|
21
|
+
"deslop": "./dist/cli.mjs"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"package.json",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"commander": "^14.0.3",
|
|
34
|
+
"deslop-js": "0.0.10"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^25.6.0"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "vp pack",
|
|
44
|
+
"dev": "vp pack --watch",
|
|
45
|
+
"test": "node --import tsx --test tests/*.test.ts",
|
|
46
|
+
"typecheck": "tsc --noEmit"
|
|
47
|
+
}
|
|
48
|
+
}
|