@noahnu/unused-files 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +6 -0
- package/lib/api/index.js +4 -3
- package/lib/api/utils.d.ts +1 -0
- package/lib/api/utils.js +19 -0
- package/lib/api/walkDependencyTree.js +10 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/lib/api/index.js
CHANGED
@@ -8,6 +8,7 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
9
9
|
const debug_1 = __importDefault(require("debug"));
|
10
10
|
const fast_glob_1 = __importDefault(require("fast-glob"));
|
11
|
+
const utils_1 = require("./utils");
|
11
12
|
const walkDependencyTree_1 = require("./walkDependencyTree");
|
12
13
|
const debug = (0, debug_1.default)('unused-files');
|
13
14
|
async function findUnusedFiles({ entryFiles, ignorePatterns = ['**/node_modules'], sourceDirectories = [], resolvers, depth, cwd = process.cwd(), }) {
|
@@ -19,14 +20,14 @@ async function findUnusedFiles({ entryFiles, ignorePatterns = ['**/node_modules'
|
|
19
20
|
absolute: true,
|
20
21
|
cwd,
|
21
22
|
});
|
22
|
-
return Promise.all(files.map(async (file) => await
|
23
|
+
return Promise.all(files.map(async (file) => await (0, utils_1.resolveRealpath)(file)));
|
23
24
|
};
|
24
25
|
const sourceDirs = sourceDirectories.length > 0 ? sourceDirectories : [cwd];
|
25
26
|
const files = new Set([].concat(...(await Promise.all(sourceDirs.map((source) => globFromSource(source))))));
|
26
27
|
const unvisitedFiles = new Set(files);
|
27
28
|
const visitedFiles = new Set();
|
28
29
|
for (const entryFile of entryFiles) {
|
29
|
-
const entry = await
|
30
|
+
const entry = await (0, utils_1.resolveRealpath)(node_path_1.default.resolve(cwd, entryFile));
|
30
31
|
unvisitedFiles.delete(entry);
|
31
32
|
for await (const { source, dependency } of (0, walkDependencyTree_1.walkDependencyTree)(entry, {
|
32
33
|
resolvers,
|
@@ -39,7 +40,7 @@ async function findUnusedFiles({ entryFiles, ignorePatterns = ['**/node_modules'
|
|
39
40
|
debug(`${source}: ${dependency} [dependency]`);
|
40
41
|
}
|
41
42
|
else {
|
42
|
-
const realpath = await
|
43
|
+
const realpath = await (0, utils_1.resolveRealpath)(dependency);
|
43
44
|
if (files.has(realpath)) {
|
44
45
|
resolvedDependency = realpath;
|
45
46
|
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function resolveRealpath(request: string): Promise<string>;
|
package/lib/api/utils.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.resolveRealpath = resolveRealpath;
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
8
|
+
const node_module_1 = require("node:module");
|
9
|
+
async function resolveRealpath(request) {
|
10
|
+
if ((0, node_module_1.isBuiltin)(request)) {
|
11
|
+
return request;
|
12
|
+
}
|
13
|
+
try {
|
14
|
+
return await node_fs_1.default.promises.realpath(request);
|
15
|
+
}
|
16
|
+
catch {
|
17
|
+
return request;
|
18
|
+
}
|
19
|
+
}
|
@@ -5,10 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.walkDependencyTree = walkDependencyTree;
|
7
7
|
const node_fs_1 = __importDefault(require("node:fs"));
|
8
|
+
const node_module_1 = require("node:module");
|
8
9
|
const node_path_1 = __importDefault(require("node:path"));
|
9
10
|
const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
|
10
11
|
const debug_1 = __importDefault(require("debug"));
|
11
12
|
const micromatch_1 = __importDefault(require("micromatch"));
|
13
|
+
const utils_1 = require("./utils");
|
12
14
|
const debug = (0, debug_1.default)('unused-files:parse');
|
13
15
|
const DEFAULT_DEPTH_LIMIT = -1; // no depth limit
|
14
16
|
const VALID_EXTENSIONS = new Set(['ts', 'tsx', 'mts', 'cts', 'js', 'jsx', 'mjs', 'cjs']);
|
@@ -19,7 +21,7 @@ async function* walkDependencyTree(source, { resolvers, visited, depth = DEFAULT
|
|
19
21
|
return;
|
20
22
|
}
|
21
23
|
// Convert to realpath if possible
|
22
|
-
source = await
|
24
|
+
source = await (0, utils_1.resolveRealpath)(source);
|
23
25
|
const visitedSet = visited ?? new Set();
|
24
26
|
if (visitedSet.has(source))
|
25
27
|
return;
|
@@ -90,6 +92,13 @@ async function* walkDependencyTree(source, { resolvers, visited, depth = DEFAULT
|
|
90
92
|
return undefined;
|
91
93
|
};
|
92
94
|
for (const importFrom of Array.from(importFroms)) {
|
95
|
+
if ((0, node_module_1.isBuiltin)(importFrom))
|
96
|
+
continue;
|
97
|
+
if (ignorePatterns && micromatch_1.default.isMatch(importFrom, ignorePatterns)) {
|
98
|
+
// ignorePatterns is used on the initial request so it doesn't get to the resolvers
|
99
|
+
// as well as on the returned result
|
100
|
+
continue;
|
101
|
+
}
|
93
102
|
const absPath = await resolveToAbsPath(importFrom);
|
94
103
|
if (absPath) {
|
95
104
|
if (ignorePatterns && micromatch_1.default.isMatch(absPath, ignorePatterns)) {
|