From 38351c954040ddea56dc5db453f7f7d10964e5cd Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 17 Jul 2023 13:51:02 -0700 Subject: [PATCH] fix: warn on autocorrected package.json entries during publish --- lib/commands/publish.js | 13 ++- .../test/lib/commands/publish.js.test.cjs | 105 ++++++++++++++++++ 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/lib/commands/publish.js b/lib/commands/publish.js index 8d2aa9e0e47f6..7b3e930922eca 100644 --- a/lib/commands/publish.js +++ b/lib/commands/publish.js @@ -89,7 +89,7 @@ class Publish extends BaseCommand { // The purpose of re-reading the manifest is in case it changed, // so that we send the latest and greatest thing to the registry // note that publishConfig might have changed as well! - manifest = await this.getManifest(spec, opts) + manifest = await this.getManifest(spec, opts, true) // JSON already has the package contents if (!json) { @@ -196,11 +196,18 @@ class Publish extends BaseCommand { // if it's a directory, read it from the file system // otherwise, get the full metadata from whatever it is // XXX can't pacote read the manifest from a directory? - async getManifest (spec, opts) { + async getManifest (spec, opts, logWarnings = false) { let manifest if (spec.type === 'directory') { + const changes = [] + const pkg = await pkgJson.fix(spec.fetchSpec, { changes }) + if (changes.length && logWarnings) { + /* eslint-disable-next-line max-len */ + log.warn('publish', 'npm auto-corrected some errors in your package.json when publishing. Please run "npm pkg fix" to address these errors.') + log.warn('publish', `errors corrected:\n${changes.join('\n')}`) + } // Prepare is the special function for publishing, different than normalize - const { content } = await pkgJson.prepare(spec.fetchSpec) + const { content } = await pkg.prepare() manifest = content } else { manifest = await pacote.manifest(spec, { diff --git a/tap-snapshots/test/lib/commands/publish.js.test.cjs b/tap-snapshots/test/lib/commands/publish.js.test.cjs index f421a2dbd8add..8392f32bbaafa 100644 --- a/tap-snapshots/test/lib/commands/publish.js.test.cjs +++ b/tap-snapshots/test/lib/commands/publish.js.test.cjs @@ -245,6 +245,17 @@ exports[`test/lib/commands/publish.js TAP no auth dry-run > must match snapshot exports[`test/lib/commands/publish.js TAP no auth dry-run > warns about auth being needed 1`] = ` Array [ + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + ), + ], Array [ "", "This command requires you to be logged in to https://registry.npmjs.org/ (dry-run)", @@ -416,6 +427,53 @@ exports[`test/lib/commands/publish.js TAP workspaces all workspaces - color > al exports[`test/lib/commands/publish.js TAP workspaces all workspaces - color > warns about skipped private workspace in color 1`] = ` Array [ + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + "repository" was changed from a string to an object + ), + ], + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + "repository" was changed from a string to an object + "repository.url" was normalized to "git+https://github.com/npm/workspace-b.git" + ), + ], + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + ), + ], + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + ), + ], Array [ "publish", "Skipping workspace \\u001b[32mworkspace-p\\u001b[39m, marked as \\u001b[1mprivate\\u001b[22m", @@ -431,6 +489,53 @@ exports[`test/lib/commands/publish.js TAP workspaces all workspaces - no color > exports[`test/lib/commands/publish.js TAP workspaces all workspaces - no color > warns about skipped private workspace 1`] = ` Array [ + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + "repository" was changed from a string to an object + ), + ], + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + "repository" was changed from a string to an object + "repository.url" was normalized to "git+https://github.com/npm/workspace-b.git" + ), + ], + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + ), + ], + Array [ + "publish", + "npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.", + ], + Array [ + "publish", + String( + errors corrected: + Removed invalid "scripts" + ), + ], Array [ "publish", "Skipping workspace workspace-p, marked as private",