@noahnu/unused-files 0.2.1 → 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,18 @@
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
+
11
+ ## @noahnu/unused-files (v0.2.2) <a name="0.2.2"></a>
12
+
13
+ Filter out files based on source directories.
14
+
15
+
16
+
5
17
  ## @noahnu/unused-files (v0.2.1) <a name="0.2.1"></a>
6
18
 
7
19
  Respect ignorePatterns for visiting files.
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
  }
@@ -50,12 +51,14 @@ async function findUnusedFiles({ entryFiles, ignorePatterns = ['**/node_modules'
50
51
  unvisitedFiles.delete(resolvedDependency);
51
52
  }
52
53
  }
54
+ const unused = Array.from(unvisitedFiles.intersection(files))
55
+ .map((abspath) => node_path_1.default.relative(cwd, abspath))
56
+ .sort();
57
+ const used = Array.from(visitedFiles.intersection(files))
58
+ .map((abspath) => node_path_1.default.relative(cwd, abspath))
59
+ .sort();
53
60
  return {
54
- unused: Array.from(unvisitedFiles)
55
- .map((abspath) => node_path_1.default.relative(cwd, abspath))
56
- .sort(),
57
- used: Array.from(visitedFiles)
58
- .map((abspath) => node_path_1.default.relative(cwd, abspath))
59
- .sort(),
61
+ unused,
62
+ used,
60
63
  };
61
64
  }
@@ -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']);
@@ -18,6 +20,8 @@ async function* walkDependencyTree(source, { resolvers, visited, depth = DEFAULT
18
20
  debug(`${source}: Unknown file extension '${ext}' [skipping]`);
19
21
  return;
20
22
  }
23
+ // Convert to realpath if possible
24
+ source = await (0, utils_1.resolveRealpath)(source);
21
25
  const visitedSet = visited ?? new Set();
22
26
  if (visitedSet.has(source))
23
27
  return;
@@ -88,6 +92,13 @@ async function* walkDependencyTree(source, { resolvers, visited, depth = DEFAULT
88
92
  return undefined;
89
93
  };
90
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
+ }
91
102
  const absPath = await resolveToAbsPath(importFrom);
92
103
  if (absPath) {
93
104
  if (ignorePatterns && micromatch_1.default.isMatch(absPath, ignorePatterns)) {
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 (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
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.1",
3
+ "version": "0.2.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/noahnu/nodejs-tools.git",
@@ -43,7 +43,7 @@
43
43
  "@noahnu/internal-test-utils": "0.0.0",
44
44
  "@types/micromatch": "^4.0.9",
45
45
  "@types/node": "^22.9.0",
46
- "typescript": "^5.6.3"
46
+ "typescript": "^5.7.2"
47
47
  },
48
48
  "types": "./lib/api/index.d.ts"
49
49
  }