Skip to content

Commit 38351c9

Browse files
committed
fix: warn on autocorrected package.json entries during publish
1 parent a0763d3 commit 38351c9

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

lib/commands/publish.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Publish extends BaseCommand {
8989
// The purpose of re-reading the manifest is in case it changed,
9090
// so that we send the latest and greatest thing to the registry
9191
// note that publishConfig might have changed as well!
92-
manifest = await this.getManifest(spec, opts)
92+
manifest = await this.getManifest(spec, opts, true)
9393

9494
// JSON already has the package contents
9595
if (!json) {
@@ -196,11 +196,18 @@ class Publish extends BaseCommand {
196196
// if it's a directory, read it from the file system
197197
// otherwise, get the full metadata from whatever it is
198198
// XXX can't pacote read the manifest from a directory?
199-
async getManifest (spec, opts) {
199+
async getManifest (spec, opts, logWarnings = false) {
200200
let manifest
201201
if (spec.type === 'directory') {
202+
const changes = []
203+
const pkg = await pkgJson.fix(spec.fetchSpec, { changes })
204+
if (changes.length && logWarnings) {
205+
/* eslint-disable-next-line max-len */
206+
log.warn('publish', 'npm auto-corrected some errors in your package.json when publishing. Please run "npm pkg fix" to address these errors.')
207+
log.warn('publish', `errors corrected:\n${changes.join('\n')}`)
208+
}
202209
// Prepare is the special function for publishing, different than normalize
203-
const { content } = await pkgJson.prepare(spec.fetchSpec)
210+
const { content } = await pkg.prepare()
204211
manifest = content
205212
} else {
206213
manifest = await pacote.manifest(spec, {

tap-snapshots/test/lib/commands/publish.js.test.cjs

+105
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ exports[`test/lib/commands/publish.js TAP no auth dry-run > must match snapshot
245245

246246
exports[`test/lib/commands/publish.js TAP no auth dry-run > warns about auth being needed 1`] = `
247247
Array [
248+
Array [
249+
"publish",
250+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
251+
],
252+
Array [
253+
"publish",
254+
String(
255+
errors corrected:
256+
Removed invalid "scripts"
257+
),
258+
],
248259
Array [
249260
"",
250261
"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
416427

417428
exports[`test/lib/commands/publish.js TAP workspaces all workspaces - color > warns about skipped private workspace in color 1`] = `
418429
Array [
430+
Array [
431+
"publish",
432+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
433+
],
434+
Array [
435+
"publish",
436+
String(
437+
errors corrected:
438+
Removed invalid "scripts"
439+
"repository" was changed from a string to an object
440+
),
441+
],
442+
Array [
443+
"publish",
444+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
445+
],
446+
Array [
447+
"publish",
448+
String(
449+
errors corrected:
450+
Removed invalid "scripts"
451+
"repository" was changed from a string to an object
452+
"repository.url" was normalized to "git+https://github.com/npm/workspace-b.git"
453+
),
454+
],
455+
Array [
456+
"publish",
457+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
458+
],
459+
Array [
460+
"publish",
461+
String(
462+
errors corrected:
463+
Removed invalid "scripts"
464+
),
465+
],
466+
Array [
467+
"publish",
468+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
469+
],
470+
Array [
471+
"publish",
472+
String(
473+
errors corrected:
474+
Removed invalid "scripts"
475+
),
476+
],
419477
Array [
420478
"publish",
421479
"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 >
431489

432490
exports[`test/lib/commands/publish.js TAP workspaces all workspaces - no color > warns about skipped private workspace 1`] = `
433491
Array [
492+
Array [
493+
"publish",
494+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
495+
],
496+
Array [
497+
"publish",
498+
String(
499+
errors corrected:
500+
Removed invalid "scripts"
501+
"repository" was changed from a string to an object
502+
),
503+
],
504+
Array [
505+
"publish",
506+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
507+
],
508+
Array [
509+
"publish",
510+
String(
511+
errors corrected:
512+
Removed invalid "scripts"
513+
"repository" was changed from a string to an object
514+
"repository.url" was normalized to "git+https://github.com/npm/workspace-b.git"
515+
),
516+
],
517+
Array [
518+
"publish",
519+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
520+
],
521+
Array [
522+
"publish",
523+
String(
524+
errors corrected:
525+
Removed invalid "scripts"
526+
),
527+
],
528+
Array [
529+
"publish",
530+
"npm auto-corrected some errors in your package.json when publishing. Please run \\"npm pkg fix\\" to address these errors.",
531+
],
532+
Array [
533+
"publish",
534+
String(
535+
errors corrected:
536+
Removed invalid "scripts"
537+
),
538+
],
434539
Array [
435540
"publish",
436541
"Skipping workspace workspace-p, marked as private",

0 commit comments

Comments
 (0)