Skip to content

Commit

Permalink
feat: add --detect-unused-files-from
Browse files Browse the repository at this point in the history
  • Loading branch information
acrazing committed Jan 30, 2023
1 parent 3dbe843 commit 579a35f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 31 deletions.
66 changes: 36 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,46 +76,52 @@
dpdm -T ./src/index.ts
```

5. Find unused files by `index.js` in `src` directory:

```bash
dpdm --no-tree --no-warning --no-circular --detect-unused-files-from 'src/**/*.*' 'index.js'
```

### Options

```bash
$ dpdm --help
dpdm [options] <files...>
dpdm.ts [options] <files...>

Analyze the files' dependencies.
Positionals:
files The file paths or globs [string]
files The file paths or globs [string]
Options:
--version Show version number [boolean]
--context the context directory to shorten path, default is current
directory [string]
--extensions, --ext comma separated extensions to resolve
[string] [default: ".ts,.tsx,.mjs,.js,.jsx,.json"]
--js comma separated extensions indicate the file is js like
[string] [default: ".ts,.tsx,.mjs,.js,.jsx"]
--include included filenames regexp in string, default includes all files
[string] [default: ".*"]
--exclude excluded filenames regexp in string, set as empty string to
include all files [string] [default: "node_modules"]
-o, --output output json to file [string]
--tree print tree to stdout [boolean] [default: true]
--circular print circular to stdout [boolean] [default: true]
--warning print warning to stdout [boolean] [default: true]
--tsconfig the tsconfig path, which is used for resolve path alias,
default is tsconfig.json if it exists in context directory
[string]
-T, --transform transform typescript modules to javascript before analyze, it
allows you to omit types dependency in typescript
[boolean] [default: false]
--exit-code exit with specified code, the value format is CASE:CODE,
`circular` is the only supported CASE, CODE should be a integer
between 0 and 128. For example: `dpdm --exit-code circular:1`
the program will exit with code 1 if circular dependency found.
[string]
--progress show progress bar [boolean] [default: true]
-h, --help Show help [boolean]
--version Show version number [boolean]
--context the context directory to shorten path, default is current
directory [string]
--extensions, --ext comma separated extensions to resolve
[string] [default: ".ts,.tsx,.mjs,.js,.jsx,.json"]
--js comma separated extensions indicate the file is js like
[string] [default: ".ts,.tsx,.mjs,.js,.jsx"]
--include included filenames regexp in string, default includes all files
[string] [default: ".*"]
--exclude excluded filenames regexp in string, set as empty string to
include all files [string] [default: "node_modules"]
-o, --output output json to file [string]
--tree print tree to stdout [boolean] [default: true]
--circular print circular to stdout [boolean] [default: true]
--warning print warning to stdout [boolean] [default: true]
--tsconfig the tsconfig path, which is used for resolve path alias, default
is tsconfig.json if it exists in context directory [string]
-T, --transform transform typescript modules to javascript before analyze, it
allows you to omit types dependency in typescript
[boolean] [default: false]
--exit-code exit with specified code, the value format is CASE:CODE,
`circular` is the only supported CASE, CODE should be a integer
between 0 and 128. For example: `dpdm --exit-code circular:1` the
program will exit with code 1 if circular dependency found.
[string]
--progress show progress bar [boolean] [default: true]
--detect-unused-files-from this file is a glob, used for finding unused files [string]
-h, --help Show help [boolean]
```
### Example output
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dpdm",
"version": "3.11.0",
"version": "3.12.0",
"description": "Analyze circular dependencies in your JavaScript/TypeScript projects.",
"keywords": [
"circular",
Expand Down
26 changes: 26 additions & 0 deletions src/bin/dpdm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ async function main() {
desc: 'show progress bar',
default: process.stdout.isTTY && !process.env.CI,
})
.option('detect-unused-files-from', {
type: 'string',
desc: 'this file is a glob, used for finding unused files',
})
.alias('h', 'help')
.wrap(Math.min(yargs.terminalWidth(), 100)).argv;

Expand Down Expand Up @@ -211,6 +215,28 @@ async function main() {
console.log(prettyWarning(parseWarnings(tree)));
console.log('');
}
if (argv.detectUnusedFilesFrom) {
const allFiles = await glob(argv.detectUnusedFilesFrom);
const shortAllFiles = allFiles.map((v) => path.relative(context, v));
const unusedFiles = shortAllFiles.filter((v) => !(v in tree)).sort();
console.log(chalk.bold.cyan('• Unused files'));
if (unusedFiles.length === 0) {
console.log(
chalk.bold.green(
' ✅ Congratulations, no unused file was found in your project. (total: ' +
allFiles.length +
', used: ' +
Object.keys(tree).length +
')',
),
);
} else {
const len = unusedFiles.length.toString().length;
unusedFiles.forEach((f, i) => {
console.log('%s) %s', i.toString().padStart(len, '0'), f);
});
}
}
for (const [label, code] of exitCodes) {
switch (label) {
case 'circular':
Expand Down

0 comments on commit 579a35f

Please sign in to comment.