-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
find-unused-exports.mjs
executable file
·84 lines (70 loc) · 2.51 KB
/
find-unused-exports.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env node
import { relative } from 'path';
import arg from 'arg';
import kleur from 'kleur';
import CliError from '../private/CliError.mjs';
import errorConsole from '../private/errorConsole.mjs';
import reportCliError from '../private/reportCliError.mjs';
import findUnusedExports from '../public/findUnusedExports.mjs';
/**
* Runs the `find-unused-exports` CLI.
* @kind function
* @name findUnusedExportsCli
* @returns {Promise<void>} Resolves once the operation is done.
* @ignore
*/
async function findUnusedExportsCli() {
try {
const {
'--module-glob': moduleGlob,
'--resolve-file-extensions': resolveFileExtensionsList,
'--resolve-index-files': resolveIndexFiles,
} = arg({
'--module-glob': String,
'--resolve-file-extensions': String,
'--resolve-index-files': Boolean,
});
if (resolveIndexFiles && !resolveFileExtensionsList)
throw new CliError(
'The `--resolve-index-files` flag can only be used with the `--resolve-file-extensions` argument.'
);
const unusedExports = await findUnusedExports({
moduleGlob,
resolveFileExtensions: resolveFileExtensionsList
? resolveFileExtensionsList.split(',')
: undefined,
resolveIndexFiles,
});
// Sort the list so that the results will be deterministic (important for
// snapshot tests) and tidy (for output readability).
const unusedExportsModulePaths = Object.keys(unusedExports).sort();
const countUnusedExportsModules = unusedExportsModulePaths.length;
let countUnusedExports = 0;
if (countUnusedExportsModules) {
const cwd = process.cwd();
for (const path of unusedExportsModulePaths) {
const exports = unusedExports[path];
countUnusedExports += exports.size;
errorConsole.group(`\n${kleur.underline().red(relative(cwd, path))}`);
errorConsole.error(kleur.dim().red(Array.from(exports).join(', ')));
errorConsole.groupEnd();
}
errorConsole.error(
`\n${kleur
.bold()
.red(
`${countUnusedExports} unused export${
countUnusedExports === 1 ? '' : 's'
} in ${countUnusedExportsModules} module${
countUnusedExportsModules === 1 ? '' : 's'
}.`
)}\n`
);
process.exitCode = 1;
} else console.info(`\n${kleur.bold().green(`0 unused exports.`)}\n`);
} catch (error) {
reportCliError('find-unused-exports', error);
process.exitCode = 1;
}
}
findUnusedExportsCli();