-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfind-unused-exports.js
executable file
·80 lines (68 loc) · 2.3 KB
/
find-unused-exports.js
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
#!/usr/bin/env node
'use strict';
const { relative } = require('path');
const arg = require('arg');
const kleur = require('kleur');
const CliError = require('../private/CliError');
const errorConsole = require('../private/errorConsole');
const reportCliError = require('../private/reportCliError');
const findUnusedExports = require('../public/findUnusedExports');
/**
* 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,
});
const countUnusedExportsModules = Object.keys(unusedExports).length;
let countUnusedExports = 0;
if (countUnusedExportsModules) {
for (const [path, exports] of Object.entries(unusedExports)) {
countUnusedExports += exports.size;
errorConsole.group(
`\n${kleur.underline().red(relative(process.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();