Skip to content

Commit

Permalink
fix: warn on autocorrected package.json entries during publish
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jul 18, 2023
1 parent a0763d3 commit 38351c9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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, {
Expand Down
105 changes: 105 additions & 0 deletions tap-snapshots/test/lib/commands/publish.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 38351c9

Please sign in to comment.