@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 CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  <!-- MONOWEAVE:BELOW -->
4
4
 
5
+ ## @noahnu/unused-files (v0.2.3) <a name="0.2.3"></a>
6
+
7
+ Ignore builtins.
8
+
9
+
10
+
5
11
  ## @noahnu/unused-files (v0.2.2) <a name="0.2.2"></a>
6
12
 
7
13
  Filter out files based on source directories.
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 node_fs_1.default.promises.realpath(file).catch(() => file)));
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 node_fs_1.default.promises.realpath(node_path_1.default.resolve(cwd, entryFile));
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 node_fs_1.default.promises.realpath(dependency);
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>;
@@ -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 node_fs_1.default.promises.realpath(source).catch(() => source);
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)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noahnu/unused-files",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/noahnu/nodejs-tools.git",