Skip to content

Commit 579a35f

Browse files
committed
feat: add --detect-unused-files-from
1 parent 3dbe843 commit 579a35f

File tree

3 files changed

+63
-31
lines changed

3 files changed

+63
-31
lines changed

README.md

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,46 +76,52 @@
7676
dpdm -T ./src/index.ts
7777
```
7878

79+
5. Find unused files by `index.js` in `src` directory:
80+
81+
```bash
82+
dpdm --no-tree --no-warning --no-circular --detect-unused-files-from 'src/**/*.*' 'index.js'
83+
```
84+
7985
### Options
8086

8187
```bash
8288
$ dpdm --help
83-
dpdm [options] <files...>
89+
dpdm.ts [options] <files...>
8490

8591
Analyze the files' dependencies.
8692
8793
Positionals:
88-
files The file paths or globs [string]
94+
files The file paths or globs [string]
8995
9096
Options:
91-
--version Show version number [boolean]
92-
--context the context directory to shorten path, default is current
93-
directory [string]
94-
--extensions, --ext comma separated extensions to resolve
95-
[string] [default: ".ts,.tsx,.mjs,.js,.jsx,.json"]
96-
--js comma separated extensions indicate the file is js like
97-
[string] [default: ".ts,.tsx,.mjs,.js,.jsx"]
98-
--include included filenames regexp in string, default includes all files
99-
[string] [default: ".*"]
100-
--exclude excluded filenames regexp in string, set as empty string to
101-
include all files [string] [default: "node_modules"]
102-
-o, --output output json to file [string]
103-
--tree print tree to stdout [boolean] [default: true]
104-
--circular print circular to stdout [boolean] [default: true]
105-
--warning print warning to stdout [boolean] [default: true]
106-
--tsconfig the tsconfig path, which is used for resolve path alias,
107-
default is tsconfig.json if it exists in context directory
108-
[string]
109-
-T, --transform transform typescript modules to javascript before analyze, it
110-
allows you to omit types dependency in typescript
111-
[boolean] [default: false]
112-
--exit-code exit with specified code, the value format is CASE:CODE,
113-
`circular` is the only supported CASE, CODE should be a integer
114-
between 0 and 128. For example: `dpdm --exit-code circular:1`
115-
the program will exit with code 1 if circular dependency found.
116-
[string]
117-
--progress show progress bar [boolean] [default: true]
118-
-h, --help Show help [boolean]
97+
--version Show version number [boolean]
98+
--context the context directory to shorten path, default is current
99+
directory [string]
100+
--extensions, --ext comma separated extensions to resolve
101+
[string] [default: ".ts,.tsx,.mjs,.js,.jsx,.json"]
102+
--js comma separated extensions indicate the file is js like
103+
[string] [default: ".ts,.tsx,.mjs,.js,.jsx"]
104+
--include included filenames regexp in string, default includes all files
105+
[string] [default: ".*"]
106+
--exclude excluded filenames regexp in string, set as empty string to
107+
include all files [string] [default: "node_modules"]
108+
-o, --output output json to file [string]
109+
--tree print tree to stdout [boolean] [default: true]
110+
--circular print circular to stdout [boolean] [default: true]
111+
--warning print warning to stdout [boolean] [default: true]
112+
--tsconfig the tsconfig path, which is used for resolve path alias, default
113+
is tsconfig.json if it exists in context directory [string]
114+
-T, --transform transform typescript modules to javascript before analyze, it
115+
allows you to omit types dependency in typescript
116+
[boolean] [default: false]
117+
--exit-code exit with specified code, the value format is CASE:CODE,
118+
`circular` is the only supported CASE, CODE should be a integer
119+
between 0 and 128. For example: `dpdm --exit-code circular:1` the
120+
program will exit with code 1 if circular dependency found.
121+
[string]
122+
--progress show progress bar [boolean] [default: true]
123+
--detect-unused-files-from this file is a glob, used for finding unused files [string]
124+
-h, --help Show help [boolean]
119125
```
120126
121127
### Example output

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dpdm",
3-
"version": "3.11.0",
3+
"version": "3.12.0",
44
"description": "Analyze circular dependencies in your JavaScript/TypeScript projects.",
55
"keywords": [
66
"circular",

src/bin/dpdm.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ async function main() {
104104
desc: 'show progress bar',
105105
default: process.stdout.isTTY && !process.env.CI,
106106
})
107+
.option('detect-unused-files-from', {
108+
type: 'string',
109+
desc: 'this file is a glob, used for finding unused files',
110+
})
107111
.alias('h', 'help')
108112
.wrap(Math.min(yargs.terminalWidth(), 100)).argv;
109113

@@ -211,6 +215,28 @@ async function main() {
211215
console.log(prettyWarning(parseWarnings(tree)));
212216
console.log('');
213217
}
218+
if (argv.detectUnusedFilesFrom) {
219+
const allFiles = await glob(argv.detectUnusedFilesFrom);
220+
const shortAllFiles = allFiles.map((v) => path.relative(context, v));
221+
const unusedFiles = shortAllFiles.filter((v) => !(v in tree)).sort();
222+
console.log(chalk.bold.cyan('• Unused files'));
223+
if (unusedFiles.length === 0) {
224+
console.log(
225+
chalk.bold.green(
226+
' ✅ Congratulations, no unused file was found in your project. (total: ' +
227+
allFiles.length +
228+
', used: ' +
229+
Object.keys(tree).length +
230+
')',
231+
),
232+
);
233+
} else {
234+
const len = unusedFiles.length.toString().length;
235+
unusedFiles.forEach((f, i) => {
236+
console.log('%s) %s', i.toString().padStart(len, '0'), f);
237+
});
238+
}
239+
}
214240
for (const [label, code] of exitCodes) {
215241
switch (label) {
216242
case 'circular':

0 commit comments

Comments
 (0)