diff --git a/docs/lib/content/commands/npm-pkg.md b/docs/lib/content/commands/npm-pkg.md index dbb2f27e5c9aa..a32bddd5f6583 100644 --- a/docs/lib/content/commands/npm-pkg.md +++ b/docs/lib/content/commands/npm-pkg.md @@ -135,6 +135,13 @@ Returned values are always in **json** format. npm pkg delete scripts.build ``` +* `npm pkg fix` + + Auto corrects common errors in your `package.json`. npm already + does this during `publish`, which leads to subtle (mostly harmless) + differences between the contents of your `package.json` file and the + manifest that npm uses during installation. + ### Workspaces support You can set/get/delete items across your configured workspaces by using the diff --git a/lib/commands/pkg.js b/lib/commands/pkg.js index 5cdcd207887c9..29bd4e89cc3c1 100644 --- a/lib/commands/pkg.js +++ b/lib/commands/pkg.js @@ -11,6 +11,7 @@ class Pkg extends BaseCommand { 'delete [ ...]', 'set [[].= ...]', 'set [[].= ...]', + 'fix', ] static params = [ @@ -45,6 +46,8 @@ class Pkg extends BaseCommand { return this.set(_args) case 'delete': return this.delete(_args) + case 'fix': + return this.fix(_args) default: throw this.usageError() } @@ -136,6 +139,11 @@ class Pkg extends BaseCommand { pkgJson.update(q.toJSON()) await pkgJson.save() } + + async fix () { + const pkgJson = await PackageJson.fix(this.prefix) + await pkgJson.save() + } } module.exports = Pkg diff --git a/tap-snapshots/test/lib/docs.js.test.cjs b/tap-snapshots/test/lib/docs.js.test.cjs index d89d5840b7825..99ddff6e150b3 100644 --- a/tap-snapshots/test/lib/docs.js.test.cjs +++ b/tap-snapshots/test/lib/docs.js.test.cjs @@ -3660,6 +3660,7 @@ npm pkg get [ [ ...]] npm pkg delete [ ...] npm pkg set [[].= ...] npm pkg set [[].= ...] +npm pkg fix Options: [-f|--force] [--json] @@ -3674,6 +3675,7 @@ npm pkg get [ [ ...]] npm pkg delete [ ...] npm pkg set [[].= ...] npm pkg set [[].= ...] +npm pkg fix \`\`\` #### \`force\` diff --git a/test/lib/commands/pkg.js b/test/lib/commands/pkg.js index ef38d537308a5..e915ef942410f 100644 --- a/test/lib/commands/pkg.js +++ b/test/lib/commands/pkg.js @@ -617,3 +617,21 @@ t.test('workspaces', async t => { 'should delete version field from workspace b' ) }) + +t.test('fix', async t => { + const { pkg, readPackageJson } = await mockNpm(t, { + prefixDir: { + 'package.json': JSON.stringify({ + name: 'foo ', + version: 'v1.1.1', + }), + }, + }) + + await pkg('fix') + t.strictSame( + readPackageJson(), + { name: 'foo', version: '1.1.1' }, + 'fixes package.json issues' + ) +})