@virmator/deps 14.16.0 → 14.17.1

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
@@ -30,8 +30,13 @@ A default plugin for [virmator](https://www.npmjs.com/package/virmator).
30
30
 
31
31
  Upgrades dependencies using npm-check-update. Does not automatically run 'npm i'. It is recommended to run 'virmator deps regen' instead.
32
32
 
33
+ If a package name or glob is passed as an argument, only the matching direct dependencies are upgraded via 'npm i <extra-args> <name>@latest'. Any flags or args supplied after the pattern are forwarded to npm verbatim (e.g. '--min-release-age 0'). In a mono-repo, this scans the root package.json as well as every workspace package.json, running an install in each one that has a match. Outside a mono-repo, it scans the current package only. The command errors out if no direct deps match in any package.json.
34
+
33
35
  - Examples
34
36
  - `virmator deps upgrade`
37
+ - upgrade a single package across the mono-repo: `virmator deps upgrade @augment-vir/common`
38
+ - upgrade all packages matching a glob: `virmator deps upgrade "@augment-vir/*"`
39
+ - forward npm flags (e.g. bypass min-release-age): `virmator deps upgrade "@augment-vir/*" --min-release-age 0`
35
40
  - Configs
36
41
  - configs/ncu.config.ts
37
42
  - Deps
package/dist/deps.d.ts CHANGED
@@ -68,9 +68,18 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
68
68
  };
69
69
  readonly upgrade: {
70
70
  readonly doc: {
71
- readonly sections: readonly ["\n Upgrades dependencies using npm-check-update.\n Does not automatically run 'npm i'.\n It is recommended to run 'virmator deps regen' instead.\n "];
71
+ readonly sections: readonly ["\n Upgrades dependencies using npm-check-update.\n Does not automatically run 'npm i'.\n It is recommended to run 'virmator deps regen' instead.\n ", "\n If a package name or glob is passed as an argument, only the\n matching direct dependencies are upgraded via\n 'npm i <extra-args> <name>@latest'. Any flags or args supplied\n after the pattern are forwarded to npm verbatim\n (e.g. '--min-release-age 0'). In a mono-repo, this scans the\n root package.json as well as every workspace package.json,\n running an install in each one that has a match. Outside a\n mono-repo, it scans the current package only. The command\n errors out if no direct deps match in any package.json.\n "];
72
72
  readonly examples: readonly [{
73
73
  readonly content: "virmator deps upgrade";
74
+ }, {
75
+ readonly title: "upgrade a single package across the mono-repo";
76
+ readonly content: "virmator deps upgrade @augment-vir/common";
77
+ }, {
78
+ readonly title: "upgrade all packages matching a glob";
79
+ readonly content: "virmator deps upgrade \"@augment-vir/*\"";
80
+ }, {
81
+ readonly title: "forward npm flags (e.g. bypass min-release-age)";
82
+ readonly content: "virmator deps upgrade \"@augment-vir/*\" --min-release-age 0";
74
83
  }];
75
84
  };
76
85
  readonly configFiles: {
@@ -85,7 +94,12 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
85
94
  readonly "mono-root": true;
86
95
  readonly package: true;
87
96
  };
88
- readonly required: true;
97
+ /**
98
+ * Only required when no dep filter argument is passed (the
99
+ * arg-based path bypasses ncu entirely). Copied on demand in the
100
+ * no-arg branch.
101
+ */
102
+ readonly required: false;
89
103
  readonly configFlags: ["--config"];
90
104
  };
91
105
  };
@@ -182,9 +196,18 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
182
196
  };
183
197
  readonly upgrade: {
184
198
  readonly doc: {
185
- readonly sections: readonly ["\n Upgrades dependencies using npm-check-update.\n Does not automatically run 'npm i'.\n It is recommended to run 'virmator deps regen' instead.\n "];
199
+ readonly sections: readonly ["\n Upgrades dependencies using npm-check-update.\n Does not automatically run 'npm i'.\n It is recommended to run 'virmator deps regen' instead.\n ", "\n If a package name or glob is passed as an argument, only the\n matching direct dependencies are upgraded via\n 'npm i <extra-args> <name>@latest'. Any flags or args supplied\n after the pattern are forwarded to npm verbatim\n (e.g. '--min-release-age 0'). In a mono-repo, this scans the\n root package.json as well as every workspace package.json,\n running an install in each one that has a match. Outside a\n mono-repo, it scans the current package only. The command\n errors out if no direct deps match in any package.json.\n "];
186
200
  readonly examples: readonly [{
187
201
  readonly content: "virmator deps upgrade";
202
+ }, {
203
+ readonly title: "upgrade a single package across the mono-repo";
204
+ readonly content: "virmator deps upgrade @augment-vir/common";
205
+ }, {
206
+ readonly title: "upgrade all packages matching a glob";
207
+ readonly content: "virmator deps upgrade \"@augment-vir/*\"";
208
+ }, {
209
+ readonly title: "forward npm flags (e.g. bypass min-release-age)";
210
+ readonly content: "virmator deps upgrade \"@augment-vir/*\" --min-release-age 0";
188
211
  }];
189
212
  };
190
213
  readonly configFiles: {
@@ -199,7 +222,12 @@ export declare const virmatorDepsPlugin: Readonly<Readonly<{
199
222
  readonly "mono-root": true;
200
223
  readonly package: true;
201
224
  };
202
- readonly required: true;
225
+ /**
226
+ * Only required when no dep filter argument is passed (the
227
+ * arg-based path bypasses ncu entirely). Copied on demand in the
228
+ * no-arg branch.
229
+ */
230
+ readonly required: false;
203
231
  readonly configFlags: ["--config"];
204
232
  };
205
233
  };
package/dist/deps.js CHANGED
@@ -1,10 +1,17 @@
1
1
  import { check } from '@augment-vir/assert';
2
- import { awaitedBlockingMap, RuntimeEnv } from '@augment-vir/common';
3
- import { toPosixPath } from '@augment-vir/node';
4
- import { defineVirmatorPlugin, JsModuleType, NpmDepType, PackageType, VirmatorNoTraceError, withCompiledTsFile, withImportedTsFile, } from '@virmator/core';
2
+ import { awaitedBlockingMap, getObjectTypedEntries, RuntimeEnv } from '@augment-vir/common';
3
+ import { listAllDirectNpmDeps, PackageJsonDependencyKey, toPosixPath } from '@augment-vir/node';
4
+ import { copyConfigFile, defineVirmatorPlugin, JsModuleType, NpmDepType, PackageType, VirmatorNoTraceError, withCompiledTsFile, withImportedTsFile, } from '@virmator/core';
5
5
  import mri from 'mri';
6
6
  import { rm } from 'node:fs/promises';
7
- import { join, relative } from 'node:path';
7
+ import { dirname, join, matchesGlob, relative } from 'node:path';
8
+ const installFlagsByDepKey = {
9
+ [PackageJsonDependencyKey.Dependencies]: '',
10
+ [PackageJsonDependencyKey.DevDependencies]: '-D',
11
+ [PackageJsonDependencyKey.PeerDependencies]: '--save-peer',
12
+ /** Not a real installable dep, just a resolution override. */
13
+ [PackageJsonDependencyKey.Overrides]: undefined,
14
+ };
8
15
  /** A virmator plugin for checking package TS dependencies. */
9
16
  export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
10
17
  name: 'Deps',
@@ -96,11 +103,34 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
96
103
  Does not automatically run 'npm i'.
97
104
  It is recommended to run 'virmator deps regen' instead.
98
105
  `,
106
+ `
107
+ If a package name or glob is passed as an argument, only the
108
+ matching direct dependencies are upgraded via
109
+ 'npm i <extra-args> <name>@latest'. Any flags or args supplied
110
+ after the pattern are forwarded to npm verbatim
111
+ (e.g. '--min-release-age 0'). In a mono-repo, this scans the
112
+ root package.json as well as every workspace package.json,
113
+ running an install in each one that has a match. Outside a
114
+ mono-repo, it scans the current package only. The command
115
+ errors out if no direct deps match in any package.json.
116
+ `,
99
117
  ],
100
118
  examples: [
101
119
  {
102
120
  content: 'virmator deps upgrade',
103
121
  },
122
+ {
123
+ title: 'upgrade a single package across the mono-repo',
124
+ content: 'virmator deps upgrade @augment-vir/common',
125
+ },
126
+ {
127
+ title: 'upgrade all packages matching a glob',
128
+ content: 'virmator deps upgrade "@augment-vir/*"',
129
+ },
130
+ {
131
+ title: 'forward npm flags (e.g. bypass min-release-age)',
132
+ content: 'virmator deps upgrade "@augment-vir/*" --min-release-age 0',
133
+ },
104
134
  ],
105
135
  },
106
136
  configFiles: {
@@ -115,7 +145,12 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
115
145
  [PackageType.MonoRoot]: true,
116
146
  [PackageType.TopPackage]: true,
117
147
  },
118
- required: true,
148
+ /**
149
+ * Only required when no dep filter argument is passed (the
150
+ * arg-based path bypasses ncu entirely). Copied on demand in the
151
+ * no-arg branch.
152
+ */
153
+ required: false,
119
154
  configFlags: ['--config'],
120
155
  },
121
156
  },
@@ -200,23 +235,88 @@ export const virmatorDepsPlugin = defineVirmatorPlugin(import.meta.dirname, {
200
235
  });
201
236
  }
202
237
  else if (usedCommands.deps?.subCommands.upgrade) {
203
- await withImportedTsFile({
204
- inputPath: join(monoRepoRootPath, configs.deps.subCommands.upgrade.configs.ncu.copyToPath),
205
- outputPath: join(cwdPackagePath, 'node_modules', '.virmator', 'dep-cruiser.config.mjs'),
206
- }, JsModuleType.Esm, async (configFile) => {
207
- const config = configFile.ncuConfig;
208
- /** C8 incorrectly thinks these imports are uncovered branches. */
209
- /* node:coverage ignore next */
210
- const ncu = await import('npm-check-updates');
211
- await ncu.run({
212
- ...config,
213
- cwd: monoRepoRootPath,
214
- workspaces: !!monoRepoPackages.length,
215
- format: [],
216
- }, {
217
- cli: true,
238
+ const upgradeArgs = mri(filteredArgs);
239
+ const depPattern = upgradeArgs._[0];
240
+ if (depPattern) {
241
+ const passthroughArgs = filteredArgs.toSpliced(filteredArgs.indexOf(depPattern), 1);
242
+ const allDirectDeps = await listAllDirectNpmDeps(monoRepoRootPath);
243
+ const matchedDepsByPackageAndKey = Object.entries(allDirectDeps).reduce((accum, [depName, usages,]) => {
244
+ if (!matchesGlob(depName, depPattern)) {
245
+ return accum;
246
+ }
247
+ return usages.reduce((innerAccumulator, usage) => {
248
+ if (usage.isWorkspace ||
249
+ installFlagsByDepKey[usage.dependencyKey] == undefined) {
250
+ return innerAccumulator;
251
+ }
252
+ const packageDir = dirname(usage.requiredBy);
253
+ const existingDepsByKey = innerAccumulator[packageDir] ?? {};
254
+ const existingDeps = existingDepsByKey[usage.dependencyKey] ?? [];
255
+ return {
256
+ ...innerAccumulator,
257
+ [packageDir]: {
258
+ ...existingDepsByKey,
259
+ [usage.dependencyKey]: [
260
+ ...existingDeps,
261
+ depName,
262
+ ],
263
+ },
264
+ };
265
+ }, accum);
266
+ }, {});
267
+ const matches = Object.entries(matchedDepsByPackageAndKey);
268
+ if (!matches.length) {
269
+ throw new VirmatorNoTraceError(`No direct dependencies matching '${depPattern}' found in any package.json.`);
270
+ }
271
+ await awaitedBlockingMap(matches, async ([packageDir, depsByKey,]) => {
272
+ await awaitedBlockingMap(getObjectTypedEntries(depsByKey), async ([dependencyKey, depNames,]) => {
273
+ const flag = installFlagsByDepKey[dependencyKey];
274
+ /**
275
+ * Defensive guard: upstream filter at 'matchedDepsByPackageAndKey'
276
+ * already drops entries whose dep key maps to an undefined flag, so
277
+ * this branch is unreachable in practice.
278
+ */
279
+ /* node:coverage ignore next 3 */
280
+ if (flag == undefined) {
281
+ return;
282
+ }
283
+ const command = [
284
+ 'npm',
285
+ 'i',
286
+ flag,
287
+ ...passthroughArgs,
288
+ ...depNames.map((depName) => `${depName}@latest`),
289
+ ]
290
+ .filter(check.isTruthy)
291
+ .join(' ');
292
+ await runShellCommand(command, {
293
+ cwd: packageDir,
294
+ }, {
295
+ logPrefix: relative(monoRepoRootPath, packageDir) || '.',
296
+ });
297
+ });
218
298
  });
219
- });
299
+ }
300
+ else {
301
+ await copyConfigFile(configs.deps.subCommands.upgrade.configs.ncu, log);
302
+ await withImportedTsFile({
303
+ inputPath: join(monoRepoRootPath, configs.deps.subCommands.upgrade.configs.ncu.copyToPath),
304
+ outputPath: join(cwdPackagePath, 'node_modules', '.virmator', 'dep-cruiser.config.mjs'),
305
+ }, JsModuleType.Esm, async (configFile) => {
306
+ const config = configFile.ncuConfig;
307
+ /** C8 incorrectly thinks these imports are uncovered branches. */
308
+ /* node:coverage ignore next */
309
+ const ncu = await import('npm-check-updates');
310
+ await ncu.run({
311
+ ...config,
312
+ cwd: monoRepoRootPath,
313
+ workspaces: !!monoRepoPackages.length,
314
+ format: [],
315
+ }, {
316
+ cli: true,
317
+ });
318
+ });
319
+ }
220
320
  }
221
321
  else if (usedCommands.deps?.subCommands.regen) {
222
322
  const allNodeModulesDirectories = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/deps",
3
- "version": "14.16.0",
3
+ "version": "14.17.1",
4
4
  "description": "Default TS deps plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -38,7 +38,7 @@
38
38
  "@augment-vir/common": "^31.69.0",
39
39
  "@augment-vir/node": "^31.69.0",
40
40
  "@npm/types": "^2.1.0",
41
- "@virmator/core": "^14.16.0",
41
+ "@virmator/core": "^14.17.1",
42
42
  "mri": "^1.2.0"
43
43
  },
44
44
  "devDependencies": {