@virmator/publish 14.30.6 → 14.31.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.
@@ -0,0 +1,15 @@
1
+ /** JSON-serializable results from npm's publish-time package.json checks. */
2
+ export type NpmPublishPackageJsonCheckResult = {
3
+ /** The corrections npm would report as publish warnings. */
4
+ warnings: string[];
5
+ /** Errors that prevent npm from checking the package.json. */
6
+ errors: string[];
7
+ };
8
+ /**
9
+ * Runs npm's package.json normalization used during publishing without changing the source file.
10
+ *
11
+ * @category Internal
12
+ */
13
+ export declare function checkPackageJsonHealth({ packageDirPath, }: Readonly<{
14
+ packageDirPath: string;
15
+ }>): Promise<NpmPublishPackageJsonCheckResult>;
@@ -0,0 +1,28 @@
1
+ import { check } from '@augment-vir/assert';
2
+ import NpmPackageJson from '@npmcli/package-json';
3
+ /**
4
+ * Runs npm's package.json normalization used during publishing without changing the source file.
5
+ *
6
+ * @category Internal
7
+ */
8
+ export async function checkPackageJsonHealth({ packageDirPath, }) {
9
+ const warnings = [];
10
+ try {
11
+ const packageJson = await NpmPackageJson.fix(packageDirPath, {
12
+ changes: warnings,
13
+ });
14
+ await packageJson.prepare();
15
+ return {
16
+ warnings,
17
+ errors: [],
18
+ };
19
+ }
20
+ catch (error) {
21
+ return {
22
+ warnings,
23
+ errors: [
24
+ check.isError(error) ? error.message : String(error),
25
+ ],
26
+ };
27
+ }
28
+ }
package/dist/publish.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { type PackageJson } from '@augment-vir/common';
2
+ import { VirmatorNoTraceError } from '@virmator/core';
2
3
  import { type SemVer } from 'semver';
4
+ export * from './npm-publish-package-json-check.js';
3
5
  /** A virmator plugin for publishing a package to npm. */
4
6
  export declare const virmatorPublishPlugin: Readonly<Readonly<{
5
7
  name: string;
@@ -34,6 +36,12 @@ export declare const virmatorPublishPlugin: Readonly<Readonly<{
34
36
  };
35
37
  }>>;
36
38
  }>;
39
+ /** Creates the error that prevents publishing an unhealthy package.json. */
40
+ export declare function createPackageJsonHealthError({ packageDirPath, warnings, errors, }: Readonly<{
41
+ packageDirPath: string;
42
+ warnings: ReadonlyArray<string>;
43
+ errors: ReadonlyArray<string>;
44
+ }>): VirmatorNoTraceError | undefined;
37
45
  /**
38
46
  * Asserts that a package's license field is valid if the package is going to be published.
39
47
  *
@@ -66,3 +74,13 @@ export declare function determineNextVersion({ latestVersion, changeMarkers, }:
66
74
  latestVersion: SemVer | undefined;
67
75
  changeMarkers: Readonly<Record<ChangeMarker, number>>;
68
76
  }): string | undefined;
77
+ /**
78
+ * Updates a package's version and every dependency map that references the given package.
79
+ *
80
+ * @category Internal
81
+ */
82
+ export declare function updatePackageJsonVersions({ packageJsonContents, packageName, version, }: Readonly<{
83
+ packageJsonContents: string;
84
+ packageName: string;
85
+ version: string;
86
+ }>): string;
package/dist/publish.js CHANGED
@@ -10,6 +10,8 @@ import semver from 'semver';
10
10
  import { simpleGit } from 'simple-git';
11
11
  import { isValidSpdxExpression } from 'spdx-vir';
12
12
  import { buildUrl } from 'url-vir';
13
+ import { checkPackageJsonHealth } from './npm-publish-package-json-check.js';
14
+ export * from './npm-publish-package-json-check.js';
13
15
  const inVirmatorEnvKey = 'IN_VIRMATOR';
14
16
  /**
15
17
  * When true, publish through npm's staged publishing (`npm stage publish`), which requires later
@@ -188,6 +190,7 @@ export const virmatorPublishPlugin = defineVirmatorPlugin(import.meta.dirname, {
188
190
  return undefined;
189
191
  }
190
192
  await alterPackageEntryPoints(packageCwd);
193
+ await assertPackageJsonHealth(packageCwd);
191
194
  return publishCommand;
192
195
  });
193
196
  }
@@ -198,6 +201,7 @@ export const virmatorPublishPlugin = defineVirmatorPlugin(import.meta.dirname, {
198
201
  return;
199
202
  }
200
203
  await alterPackageEntryPoints(monoRepoRootPath);
204
+ await assertPackageJsonHealth(monoRepoRootPath);
201
205
  await runShellCommand(publishCommand);
202
206
  }
203
207
  await Promise.all(alteredPackageJsonFiles.map(async (alteredFile) => {
@@ -227,6 +231,31 @@ export const virmatorPublishPlugin = defineVirmatorPlugin(import.meta.dirname, {
227
231
  }));
228
232
  }
229
233
  });
234
+ async function assertPackageJsonHealth(packageDirPath) {
235
+ const packageJsonHealth = await checkPackageJsonHealth({
236
+ packageDirPath,
237
+ });
238
+ const packageJsonHealthError = createPackageJsonHealthError({
239
+ packageDirPath,
240
+ ...packageJsonHealth,
241
+ });
242
+ if (packageJsonHealthError) {
243
+ throw packageJsonHealthError;
244
+ }
245
+ }
246
+ /** Creates the error that prevents publishing an unhealthy package.json. */
247
+ export function createPackageJsonHealthError({ packageDirPath, warnings, errors, }) {
248
+ const healthProblems = [
249
+ ...errors.map((error) => `Error: ${error}`),
250
+ ...warnings.map((warning) => `Warning: ${warning}`),
251
+ ];
252
+ return healthProblems.length
253
+ ? new VirmatorNoTraceError([
254
+ `npm publish package.json health check failed in '${packageDirPath}'.`,
255
+ ...healthProblems,
256
+ ].join('\n'))
257
+ : undefined;
258
+ }
230
259
  async function updateGit(packageDirPath) {
231
260
  const newVersion = (await readPackageJson(packageDirPath)).version;
232
261
  assert.isDefined(newVersion);
@@ -423,10 +452,34 @@ async function updateVersion({ version, packagePath, monoPackages, }) {
423
452
  if (!packageName) {
424
453
  throw new Error(`Missing package name in '${packagePath}'.`);
425
454
  }
426
- await writeFile(packageJsonPath, packageJsonContents.replace(/"version": "[^"]+"/, `"version": "${version}"`));
455
+ await writeFile(packageJsonPath, updatePackageJsonVersions({
456
+ packageJsonContents,
457
+ packageName,
458
+ version,
459
+ }));
427
460
  await awaitedBlockingMap(monoPackages, async (monoPackage) => {
428
461
  const monoPackageJsonPath = join(monoPackage.fullPath, 'package.json');
429
462
  const monoPackageJsonContents = (await readFile(monoPackageJsonPath)).toString();
430
- await writeFile(monoPackageJsonPath, monoPackageJsonContents.replace(new RegExp(`"${packageName}": "[^"]+"`), `"${packageName}": "^${version}"`));
463
+ await writeFile(monoPackageJsonPath, updatePackageJsonVersions({
464
+ packageJsonContents: monoPackageJsonContents,
465
+ packageName,
466
+ version,
467
+ }));
468
+ });
469
+ }
470
+ /**
471
+ * Updates a package's version and every dependency map that references the given package.
472
+ *
473
+ * @category Internal
474
+ */
475
+ export function updatePackageJsonVersions({ packageJsonContents, packageName, version, }) {
476
+ const packageNameMatcher = new RegExp(String.raw `("${escapeRegExp(packageName)}"\s*:\s*)"[^"]+"`);
477
+ return packageJsonContents
478
+ .replace(/"version"\s*:\s*"[^"]+"/, `"version": "${version}"`)
479
+ .replace(/"[^"]*dependencies"\s*:\s*\{[^{}]*}/gi, (dependencyPropertyContents) => {
480
+ return dependencyPropertyContents.replace(packageNameMatcher, `$1"^${version}"`);
431
481
  });
432
482
  }
483
+ function escapeRegExp(value) {
484
+ return value.replace(/[.*+?^${}()|[\]\\]/g, String.raw `\$&`);
485
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/publish",
3
- "version": "14.30.6",
3
+ "version": "14.31.0",
4
4
  "description": "Default publish plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -34,19 +34,21 @@
34
34
  "test:update": "npm test"
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",
40
- "@virmator/core": "^14.30.6",
37
+ "@augment-vir/assert": "^32.2.2",
38
+ "@augment-vir/common": "^32.2.2",
39
+ "@augment-vir/node": "^32.2.2",
40
+ "@npmcli/package-json": "^8.0.0",
41
+ "@virmator/core": "^14.31.0",
41
42
  "mri": "^1.2.0",
42
43
  "semver": "^7.8.5",
43
44
  "simple-git": "^3.36.0",
44
45
  "spdx-vir": "^1.0.0",
45
- "url-vir": "^2.1.10"
46
+ "url-vir": "^2.2.1"
46
47
  },
47
48
  "devDependencies": {
48
- "@augment-vir/test": "^32.2.1",
49
- "@types/node": "^26.1.2",
49
+ "@augment-vir/test": "^32.2.2",
50
+ "@types/node": "^26.2.0",
51
+ "@types/npmcli__package-json": "^4.0.4",
50
52
  "@types/semver": "^7.8.0",
51
53
  "markdown-code-example-inserter": "^3.0.6",
52
54
  "typedoc": "^0.28.20"