@virmator/deps 14.30.7 → 14.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,6 +11,7 @@ A default plugin for [virmator](https://www.npmjs.com/package/virmator).
11
11
  - Examples
12
12
  - check import dependencies: `virmator deps check`
13
13
  - upgrade npm dependencies: `virmator deps upgrade`
14
+ - list unused npm dependencies: `virmator deps unused`
14
15
  - regenerate npm dependencies: `virmator deps regen`
15
16
  - Sub Commands
16
17
 
@@ -26,6 +27,15 @@ A default plugin for [virmator](https://www.npmjs.com/package/virmator).
26
27
  - [dependency-cruiser](https://npmjs.com/package/dependency-cruiser)
27
28
  - [esbuild](https://npmjs.com/package/esbuild)
28
29
 
30
+ - **unused**
31
+
32
+ Lists direct dependencies declared in package.json that are not referenced by a static import in the package's code. Dependencies used only by npm scripts or dynamically generated module names may be reported as unused.
33
+
34
+ - Examples
35
+ - `virmator deps unused`
36
+ - Deps
37
+ - [dependency-cruiser](https://npmjs.com/package/dependency-cruiser)
38
+
29
39
  - **upgrade**
30
40
 
31
41
  Upgrades dependencies using npm-check-update. Does not automatically run 'npm i'. It is recommended to run 'virmator deps regen' instead.
package/dist/deps.d.ts CHANGED
@@ -1,4 +1,11 @@
1
- import { NpmDepType } from '@virmator/core';
1
+ import { type MonoRepoPackage, NpmDepType, PackageType } from '@virmator/core';
2
+ /** Lists package directories that should be checked for unused dependencies. */
3
+ export declare function getUnusedPackageDirPaths({ cwdPackagePath, monoRepoPackages, monoRepoRootPath, packageType, }: Readonly<{
4
+ cwdPackagePath: string;
5
+ monoRepoPackages: ReadonlyArray<Readonly<MonoRepoPackage>>;
6
+ monoRepoRootPath: string;
7
+ packageType: PackageType;
8
+ }>): string[];
2
9
  /** A virmator plugin for checking package TS dependencies. */
3
10
  export declare const virmatorDepsPlugin: Readonly<Readonly<{
4
11
  name: string;
@@ -12,6 +19,9 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
12
19
  }, {
13
20
  readonly title: "upgrade npm dependencies";
14
21
  readonly content: "virmator deps upgrade";
22
+ }, {
23
+ readonly title: "list unused npm dependencies";
24
+ readonly content: "virmator deps unused";
15
25
  }, {
16
26
  readonly title: "regenerate npm dependencies";
17
27
  readonly content: "virmator deps regen";
@@ -41,7 +51,8 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
41
51
  };
42
52
  };
43
53
  readonly npmDeps: {
44
- readonly 'dependency-cruiser': {
54
+ /** Needed to compile the TS dep-cruiser config file. */
55
+ readonly esbuild: {
45
56
  readonly type: NpmDepType.Dev;
46
57
  readonly env: {
47
58
  readonly node: true;
@@ -52,16 +63,36 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
52
63
  readonly "mono-root": true;
53
64
  };
54
65
  };
55
- /** Needed to compile the TS dep-cruiser config file. */
56
- readonly esbuild: {
57
- readonly type: NpmDepType.Dev;
58
- readonly env: {
59
- readonly node: true;
60
- readonly web: true;
66
+ readonly 'dependency-cruiser': {
67
+ type: NpmDepType;
68
+ env: {
69
+ node: boolean;
70
+ web: boolean;
61
71
  };
62
- readonly packageType: {
63
- readonly package: true;
64
- readonly "mono-root": true;
72
+ packageType: {
73
+ package: boolean;
74
+ "mono-root": boolean;
75
+ };
76
+ };
77
+ };
78
+ };
79
+ readonly unused: {
80
+ readonly doc: {
81
+ readonly sections: readonly ["\n Lists direct dependencies declared in package.json that are not\n referenced by a static import in the package's code. Dependencies\n used only by npm scripts or dynamically generated module names\n may be reported as unused.\n "];
82
+ readonly examples: readonly [{
83
+ readonly content: "virmator deps unused";
84
+ }];
85
+ };
86
+ readonly npmDeps: {
87
+ 'dependency-cruiser': {
88
+ type: NpmDepType;
89
+ env: {
90
+ node: boolean;
91
+ web: boolean;
92
+ };
93
+ packageType: {
94
+ package: boolean;
95
+ "mono-root": boolean;
65
96
  };
66
97
  };
67
98
  };
@@ -143,6 +174,9 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
143
174
  }, {
144
175
  readonly title: "upgrade npm dependencies";
145
176
  readonly content: "virmator deps upgrade";
177
+ }, {
178
+ readonly title: "list unused npm dependencies";
179
+ readonly content: "virmator deps unused";
146
180
  }, {
147
181
  readonly title: "regenerate npm dependencies";
148
182
  readonly content: "virmator deps regen";
@@ -172,7 +206,8 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
172
206
  };
173
207
  };
174
208
  readonly npmDeps: {
175
- readonly 'dependency-cruiser': {
209
+ /** Needed to compile the TS dep-cruiser config file. */
210
+ readonly esbuild: {
176
211
  readonly type: NpmDepType.Dev;
177
212
  readonly env: {
178
213
  readonly node: true;
@@ -183,16 +218,36 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
183
218
  readonly "mono-root": true;
184
219
  };
185
220
  };
186
- /** Needed to compile the TS dep-cruiser config file. */
187
- readonly esbuild: {
188
- readonly type: NpmDepType.Dev;
189
- readonly env: {
190
- readonly node: true;
191
- readonly web: true;
221
+ readonly 'dependency-cruiser': {
222
+ type: NpmDepType;
223
+ env: {
224
+ node: boolean;
225
+ web: boolean;
192
226
  };
193
- readonly packageType: {
194
- readonly package: true;
195
- readonly "mono-root": true;
227
+ packageType: {
228
+ package: boolean;
229
+ "mono-root": boolean;
230
+ };
231
+ };
232
+ };
233
+ };
234
+ readonly unused: {
235
+ readonly doc: {
236
+ readonly sections: readonly ["\n Lists direct dependencies declared in package.json that are not\n referenced by a static import in the package's code. Dependencies\n used only by npm scripts or dynamically generated module names\n may be reported as unused.\n "];
237
+ readonly examples: readonly [{
238
+ readonly content: "virmator deps unused";
239
+ }];
240
+ };
241
+ readonly npmDeps: {
242
+ 'dependency-cruiser': {
243
+ type: NpmDepType;
244
+ env: {
245
+ node: boolean;
246
+ web: boolean;
247
+ };
248
+ packageType: {
249
+ package: boolean;
250
+ "mono-root": boolean;
196
251
  };
197
252
  };
198
253
  };
package/dist/deps.js CHANGED
@@ -5,8 +5,33 @@ import { copyConfigFile, defineVirmatorPlugin, JsModuleType, NpmDepType, Package
5
5
  import mri from 'mri';
6
6
  import { rm } from 'node:fs/promises';
7
7
  import { join, relative } from 'node:path';
8
+ import { findUnusedPackageDependencies } from './find-unused-package-dependencies.js';
8
9
  import { listRegenNodeModulesDirs } from './regen-node-modules.js';
9
10
  import { runArgBasedUpgrade } from './upgrade-deps.js';
11
+ const dependencyCruiserNpmDeps = {
12
+ 'dependency-cruiser': {
13
+ type: NpmDepType.Dev,
14
+ env: {
15
+ [RuntimeEnv.Node]: true,
16
+ [RuntimeEnv.Web]: true,
17
+ },
18
+ packageType: {
19
+ [PackageType.TopPackage]: true,
20
+ [PackageType.MonoRoot]: true,
21
+ },
22
+ },
23
+ };
24
+ /** Lists package directories that should be checked for unused dependencies. */
25
+ export function getUnusedPackageDirPaths({ cwdPackagePath, monoRepoPackages, monoRepoRootPath, packageType, }) {
26
+ return packageType === PackageType.MonoRoot
27
+ ? [
28
+ monoRepoRootPath,
29
+ ...monoRepoPackages.map(({ fullPath }) => {
30
+ return fullPath;
31
+ }),
32
+ ]
33
+ : [cwdPackagePath];
34
+ }
10
35
  /** A virmator plugin for checking package TS dependencies. */
11
36
  export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
12
37
  name: 'Deps',
@@ -27,6 +52,10 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
27
52
  title: 'upgrade npm dependencies',
28
53
  content: 'virmator deps upgrade',
29
54
  },
55
+ {
56
+ title: 'list unused npm dependencies',
57
+ content: 'virmator deps unused',
58
+ },
30
59
  {
31
60
  title: 'regenerate npm dependencies',
32
61
  content: 'virmator deps regen',
@@ -65,17 +94,7 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
65
94
  },
66
95
  },
67
96
  npmDeps: {
68
- 'dependency-cruiser': {
69
- type: NpmDepType.Dev,
70
- env: {
71
- [RuntimeEnv.Node]: true,
72
- [RuntimeEnv.Web]: true,
73
- },
74
- packageType: {
75
- [PackageType.TopPackage]: true,
76
- [PackageType.MonoRoot]: true,
77
- },
78
- },
97
+ ...dependencyCruiserNpmDeps,
79
98
  /** Needed to compile the TS dep-cruiser config file. */
80
99
  esbuild: {
81
100
  type: NpmDepType.Dev,
@@ -90,6 +109,24 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
90
109
  },
91
110
  },
92
111
  },
112
+ unused: {
113
+ doc: {
114
+ sections: [
115
+ `
116
+ Lists direct dependencies declared in package.json that are not
117
+ referenced by a static import in the package's code. Dependencies
118
+ used only by npm scripts or dynamically generated module names
119
+ may be reported as unused.
120
+ `,
121
+ ],
122
+ examples: [
123
+ {
124
+ content: 'virmator deps unused',
125
+ },
126
+ ],
127
+ },
128
+ npmDeps: dependencyCruiserNpmDeps,
129
+ },
93
130
  upgrade: {
94
131
  doc: {
95
132
  sections: [
@@ -188,7 +225,7 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
188
225
  },
189
226
  },
190
227
  },
191
- }, async ({ cliInputs: { filteredArgs, usedCommands }, package: { monoRepoRootPath, packageType, cwdPackagePath, monoRepoPackages }, configs, log, runPerPackage, runShellCommand, cwd, }) => {
228
+ }, async ({ cliInputs: { filteredArgs, usedCommands }, package: { monoRepoRootPath, packageType, cwdPackagePath, monoRepoPackages }, configs, log, runShellCommand, cwd, }) => {
192
229
  if (usedCommands.deps?.subCommands.check) {
193
230
  const args = mri(filteredArgs);
194
231
  const pathToCheck = args._.length ? '' : 'src';
@@ -205,22 +242,19 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
205
242
  ];
206
243
  }
207
244
  if (packageType === PackageType.MonoRoot) {
208
- await runPerPackage(({ packageCwd }) => {
209
- const relativeToRoot = toPosixPath(relative(packageCwd, cwd));
210
- const packageRelPath = toPosixPath(relative(cwd, packageCwd));
211
- return [
212
- 'cd',
213
- relativeToRoot,
214
- '&&',
215
- 'npx',
216
- 'depcruise',
217
- ...buildConfigFlags(cwd),
218
- pathToCheck ? toPosixPath(join(packageRelPath, pathToCheck)) : '',
219
- ...filteredArgs,
220
- ]
221
- .filter(check.isTruthy)
222
- .join(' ');
223
- });
245
+ await runShellCommand([
246
+ 'npx',
247
+ 'depcruise',
248
+ ...buildConfigFlags(cwd),
249
+ ...monoRepoPackages.map(({ relativePath }) => {
250
+ return pathToCheck
251
+ ? toPosixPath(join(relativePath, pathToCheck))
252
+ : '';
253
+ }),
254
+ ...filteredArgs,
255
+ ]
256
+ .filter(check.isTruthy)
257
+ .join(' '));
224
258
  }
225
259
  else {
226
260
  await runShellCommand([
@@ -235,6 +269,38 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
235
269
  }
236
270
  });
237
271
  }
272
+ else if (usedCommands.deps?.subCommands.unused) {
273
+ const packageDirPaths = getUnusedPackageDirPaths({
274
+ cwdPackagePath,
275
+ monoRepoPackages,
276
+ monoRepoRootPath,
277
+ packageType,
278
+ });
279
+ const unusedPackageDependencyLines = (await awaitedBlockingMap(packageDirPaths, async (packageDirPath) => {
280
+ return {
281
+ packageDirPath,
282
+ unusedDependencies: await findUnusedPackageDependencies({
283
+ packageDirPath,
284
+ }),
285
+ };
286
+ })).flatMap(({ packageDirPath, unusedDependencies }) => {
287
+ if (!unusedDependencies.length) {
288
+ return [];
289
+ }
290
+ return [
291
+ `${toPosixPath(join(relative(cwd, packageDirPath), 'package.json'))}:`,
292
+ ...unusedDependencies.map((dependencyName) => {
293
+ return ` ${dependencyName}`;
294
+ }),
295
+ ];
296
+ });
297
+ if (unusedPackageDependencyLines.length) {
298
+ log.plain(unusedPackageDependencyLines.join('\n'));
299
+ }
300
+ else {
301
+ log.success('No unused package.json dependencies found.');
302
+ }
303
+ }
238
304
  else if (usedCommands.deps?.subCommands.upgrade) {
239
305
  const upgradeArgs = mri(filteredArgs);
240
306
  const depPattern = upgradeArgs._[0];
@@ -296,6 +362,6 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
296
362
  await runShellCommand(installCommand);
297
363
  }
298
364
  else {
299
- throw new VirmatorNoTraceError("deps sub-command needed: 'virmator deps check', 'virmator deps upgrade', or 'virmator deps regen'");
365
+ throw new VirmatorNoTraceError("deps sub-command needed: 'virmator deps check', 'virmator deps unused', 'virmator deps upgrade', or 'virmator deps regen'");
300
366
  }
301
367
  });
@@ -0,0 +1,4 @@
1
+ /** Finds direct package.json dependencies that have no static import in the package's code. */
2
+ export declare function findUnusedPackageDependencies({ packageDirPath, }: Readonly<{
3
+ packageDirPath: string;
4
+ }>): Promise<string[]>;
@@ -0,0 +1,58 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import { getObjectTypedEntries } from '@augment-vir/common';
3
+ import { readPackageJson } from '@augment-vir/node';
4
+ /** Finds direct package.json dependencies that have no static import in the package's code. */
5
+ export async function findUnusedPackageDependencies({ packageDirPath, }) {
6
+ /** The plugin installs dependency-cruiser in the consuming package immediately before this runs. */
7
+ /* c8 ignore next */
8
+ const dependencyCruiser = await import('dependency-cruiser');
9
+ const dependencyCruiseOutput = await dependencyCruiser.cruise(['.'], {
10
+ baseDir: packageDirPath,
11
+ doNotFollow: {
12
+ dependencyTypes: [
13
+ 'npm',
14
+ 'npm-bundled',
15
+ 'npm-dev',
16
+ 'npm-no-pkg',
17
+ 'npm-optional',
18
+ 'npm-peer',
19
+ 'npm-unknown',
20
+ ],
21
+ },
22
+ exclude: '^(?:dist|dist-[^/]+|coverage|test-files)(?:/|$)',
23
+ });
24
+ /** `cruise` only returns string output when explicitly given an output format, which we do not. */
25
+ /* c8 ignore next 3 */
26
+ if (check.isString(dependencyCruiseOutput.output)) {
27
+ throw new TypeError('Dependency cruiser did not return a dependency graph.');
28
+ }
29
+ const moduleSpecifiers = dependencyCruiseOutput.output.modules
30
+ .filter(({ source }) => {
31
+ return !/(?:^\.\.|node_modules\/)/.test(source);
32
+ })
33
+ .flatMap(({ dependencies }) => {
34
+ return dependencies.map(({ module }) => {
35
+ return module;
36
+ });
37
+ });
38
+ const packageJson = await readPackageJson(packageDirPath);
39
+ const dependencyNames = [
40
+ packageJson.dependencies,
41
+ packageJson.devDependencies,
42
+ packageJson.optionalDependencies,
43
+ packageJson.peerDependencies,
44
+ ].flatMap((dependencies) => {
45
+ return getObjectTypedEntries(dependencies || {}).map(([dependencyName,]) => {
46
+ return dependencyName;
47
+ });
48
+ });
49
+ return dependencyNames
50
+ .filter((dependencyName, index) => {
51
+ return (dependencyNames.indexOf(dependencyName) === index &&
52
+ !moduleSpecifiers.some((moduleSpecifier) => {
53
+ return (moduleSpecifier === dependencyName ||
54
+ moduleSpecifier.startsWith(`${dependencyName}/`));
55
+ }));
56
+ })
57
+ .toSorted();
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/deps",
3
- "version": "14.30.7",
3
+ "version": "14.32.0",
4
4
  "description": "Default TS deps plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -34,24 +34,25 @@
34
34
  "test:update": "npm test update"
35
35
  },
36
36
  "dependencies": {
37
- "@augment-vir/assert": "^32.2.1",
38
- "@augment-vir/common": "^32.2.1",
39
- "@augment-vir/node": "^32.2.1",
37
+ "@augment-vir/assert": "^32.2.2",
38
+ "@augment-vir/common": "^32.2.2",
39
+ "@augment-vir/node": "^32.2.2",
40
40
  "@npm/types": "^2.1.0",
41
- "@virmator/core": "^14.30.7",
42
- "date-vir": "^8.6.2",
41
+ "@virmator/core": "^14.32.0",
42
+ "date-vir": "^9.1.0",
43
43
  "mri": "^1.2.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@augment-vir/test": "^32.2.1",
47
- "@types/node": "^26.1.2",
46
+ "@augment-vir/test": "^32.2.2",
47
+ "@types/node": "^26.2.0",
48
48
  "c8": "^12.0.0",
49
- "dependency-cruiser": "^18.1.1",
50
- "esbuild": "^0.28.1",
49
+ "dependency-cruiser": "^18.2.0",
50
+ "esbuild": "^0.28.2",
51
51
  "istanbul-smart-text-reporter": "^1.1.5",
52
- "npm-check-updates": "^23.0.1"
52
+ "npm-check-updates": "^23.0.2"
53
53
  },
54
54
  "peerDependencies": {
55
+ "dependency-cruiser": ">=18",
55
56
  "npm-check-updates": ">=19"
56
57
  },
57
58
  "engines": {