@noahnu/unused-files 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +12 -0
- package/lib/api/index.js +9 -6
- package/lib/api/walkDependencyTree.d.ts +2 -1
- package/lib/api/walkDependencyTree.js +8 -1
- package/lib/command.js +17 -7
- package/package.json +4 -2
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,18 @@
|
|
2
2
|
|
3
3
|
<!-- MONOWEAVE:BELOW -->
|
4
4
|
|
5
|
+
## @noahnu/unused-files (v0.2.2) <a name="0.2.2"></a>
|
6
|
+
|
7
|
+
Filter out files based on source directories.
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
## @noahnu/unused-files (v0.2.1) <a name="0.2.1"></a>
|
12
|
+
|
13
|
+
Respect ignorePatterns for visiting files.
|
14
|
+
|
15
|
+
|
16
|
+
|
5
17
|
## @noahnu/unused-files (v0.2.0) <a name="0.2.0"></a>
|
6
18
|
|
7
19
|
Support custom resolvers.
|
package/lib/api/index.js
CHANGED
@@ -32,6 +32,7 @@ async function findUnusedFiles({ entryFiles, ignorePatterns = ['**/node_modules'
|
|
32
32
|
resolvers,
|
33
33
|
depth,
|
34
34
|
visited: visitedFiles,
|
35
|
+
ignorePatterns,
|
35
36
|
})) {
|
36
37
|
let resolvedDependency = dependency;
|
37
38
|
if (files.has(dependency)) {
|
@@ -49,12 +50,14 @@ async function findUnusedFiles({ entryFiles, ignorePatterns = ['**/node_modules'
|
|
49
50
|
unvisitedFiles.delete(resolvedDependency);
|
50
51
|
}
|
51
52
|
}
|
53
|
+
const unused = Array.from(unvisitedFiles.intersection(files))
|
54
|
+
.map((abspath) => node_path_1.default.relative(cwd, abspath))
|
55
|
+
.sort();
|
56
|
+
const used = Array.from(visitedFiles.intersection(files))
|
57
|
+
.map((abspath) => node_path_1.default.relative(cwd, abspath))
|
58
|
+
.sort();
|
52
59
|
return {
|
53
|
-
unused
|
54
|
-
|
55
|
-
.sort(),
|
56
|
-
used: Array.from(visitedFiles)
|
57
|
-
.map((abspath) => node_path_1.default.relative(cwd, abspath))
|
58
|
-
.sort(),
|
60
|
+
unused,
|
61
|
+
used,
|
59
62
|
};
|
60
63
|
}
|
@@ -1,8 +1,9 @@
|
|
1
1
|
import { type Resolver } from './types';
|
2
|
-
export declare function walkDependencyTree(source: string, { resolvers, visited, depth, }?: {
|
2
|
+
export declare function walkDependencyTree(source: string, { resolvers, visited, depth, ignorePatterns, }?: {
|
3
3
|
resolvers?: Resolver[];
|
4
4
|
visited?: Set<string>;
|
5
5
|
depth?: number;
|
6
|
+
ignorePatterns?: string[];
|
6
7
|
}): AsyncGenerator<{
|
7
8
|
source: string;
|
8
9
|
dependency: string;
|
@@ -8,15 +8,18 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
9
9
|
const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
|
10
10
|
const debug_1 = __importDefault(require("debug"));
|
11
|
+
const micromatch_1 = __importDefault(require("micromatch"));
|
11
12
|
const debug = (0, debug_1.default)('unused-files:parse');
|
12
13
|
const DEFAULT_DEPTH_LIMIT = -1; // no depth limit
|
13
14
|
const VALID_EXTENSIONS = new Set(['ts', 'tsx', 'mts', 'cts', 'js', 'jsx', 'mjs', 'cjs']);
|
14
|
-
async function* walkDependencyTree(source, { resolvers, visited, depth = DEFAULT_DEPTH_LIMIT, } = {}) {
|
15
|
+
async function* walkDependencyTree(source, { resolvers, visited, depth = DEFAULT_DEPTH_LIMIT, ignorePatterns, } = {}) {
|
15
16
|
const ext = node_path_1.default.extname(source).substring(1);
|
16
17
|
if (!VALID_EXTENSIONS.has(ext)) {
|
17
18
|
debug(`${source}: Unknown file extension '${ext}' [skipping]`);
|
18
19
|
return;
|
19
20
|
}
|
21
|
+
// Convert to realpath if possible
|
22
|
+
source = await node_fs_1.default.promises.realpath(source).catch(() => source);
|
20
23
|
const visitedSet = visited ?? new Set();
|
21
24
|
if (visitedSet.has(source))
|
22
25
|
return;
|
@@ -89,12 +92,16 @@ async function* walkDependencyTree(source, { resolvers, visited, depth = DEFAULT
|
|
89
92
|
for (const importFrom of Array.from(importFroms)) {
|
90
93
|
const absPath = await resolveToAbsPath(importFrom);
|
91
94
|
if (absPath) {
|
95
|
+
if (ignorePatterns && micromatch_1.default.isMatch(absPath, ignorePatterns)) {
|
96
|
+
continue;
|
97
|
+
}
|
92
98
|
yield { dependency: absPath, source };
|
93
99
|
if (depth === -1 || depth > 0) {
|
94
100
|
yield* walkDependencyTree(absPath, {
|
95
101
|
resolvers,
|
96
102
|
visited: visitedSet,
|
97
103
|
depth: depth === -1 ? depth : depth - 1,
|
104
|
+
ignorePatterns,
|
98
105
|
});
|
99
106
|
}
|
100
107
|
}
|
package/lib/command.js
CHANGED
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
15
15
|
}) : function(o, v) {
|
16
16
|
o["default"] = v;
|
17
17
|
});
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
};
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
19
|
+
var ownKeys = function(o) {
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
21
|
+
var ar = [];
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
23
|
+
return ar;
|
24
|
+
};
|
25
|
+
return ownKeys(o);
|
26
|
+
};
|
27
|
+
return function (mod) {
|
28
|
+
if (mod && mod.__esModule) return mod;
|
29
|
+
var result = {};
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
31
|
+
__setModuleDefault(result, mod);
|
32
|
+
return result;
|
33
|
+
};
|
34
|
+
})();
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
36
|
exports.BaseCommand = void 0;
|
27
37
|
const clipanion_1 = require("clipanion");
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@noahnu/unused-files",
|
3
|
-
"version": "0.2.
|
3
|
+
"version": "0.2.2",
|
4
4
|
"repository": {
|
5
5
|
"type": "git",
|
6
6
|
"url": "https://github.com/noahnu/nodejs-tools.git",
|
@@ -35,13 +35,15 @@
|
|
35
35
|
"clipanion": "^4.0.0-rc.4",
|
36
36
|
"debug": "^4.3.7",
|
37
37
|
"fast-glob": "^3.3.2",
|
38
|
+
"micromatch": "^4.0.8",
|
38
39
|
"typanion": "^3.14.0"
|
39
40
|
},
|
40
41
|
"devDependencies": {
|
41
42
|
"@jest/globals": "^29.7.0",
|
42
43
|
"@noahnu/internal-test-utils": "0.0.0",
|
44
|
+
"@types/micromatch": "^4.0.9",
|
43
45
|
"@types/node": "^22.9.0",
|
44
|
-
"typescript": "^5.
|
46
|
+
"typescript": "^5.7.2"
|
45
47
|
},
|
46
48
|
"types": "./lib/api/index.d.ts"
|
47
49
|
}
|