Skip to content

Commit a294cfd

Browse files
committed
feat: add pkg fix subcommand
1 parent 271b977 commit a294cfd

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

docs/lib/content/commands/npm-pkg.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ Returned values are always in **json** format.
135135
npm pkg delete scripts.build
136136
```
137137
138+
* `npm pkg fix`
139+
140+
Auto corrects common errors in your `package.json`. npm already
141+
does this during `publish`, which leads to subtle (mostly harmless)
142+
differences between the contents of your `package.json` file and the
143+
manifest that npm uses during installation.
144+
138145
### Workspaces support
139146
140147
You can set/get/delete items across your configured workspaces by using the

lib/commands/pkg.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Pkg extends BaseCommand {
1111
'delete <key> [<key> ...]',
1212
'set [<array>[<index>].<key>=<value> ...]',
1313
'set [<array>[].<key>=<value> ...]',
14+
'fix',
1415
]
1516

1617
static params = [
@@ -45,6 +46,8 @@ class Pkg extends BaseCommand {
4546
return this.set(_args)
4647
case 'delete':
4748
return this.delete(_args)
49+
case 'fix':
50+
return this.fix(_args)
4851
default:
4952
throw this.usageError()
5053
}
@@ -136,6 +139,11 @@ class Pkg extends BaseCommand {
136139
pkgJson.update(q.toJSON())
137140
await pkgJson.save()
138141
}
142+
143+
async fix () {
144+
const pkgJson = await PackageJson.fix(this.prefix)
145+
await pkgJson.save()
146+
}
139147
}
140148

141149
module.exports = Pkg

tap-snapshots/test/lib/docs.js.test.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,6 +3660,7 @@ npm pkg get [<key> [<key> ...]]
36603660
npm pkg delete <key> [<key> ...]
36613661
npm pkg set [<array>[<index>].<key>=<value> ...]
36623662
npm pkg set [<array>[].<key>=<value> ...]
3663+
npm pkg fix
36633664
36643665
Options:
36653666
[-f|--force] [--json]
@@ -3674,6 +3675,7 @@ npm pkg get [<key> [<key> ...]]
36743675
npm pkg delete <key> [<key> ...]
36753676
npm pkg set [<array>[<index>].<key>=<value> ...]
36763677
npm pkg set [<array>[].<key>=<value> ...]
3678+
npm pkg fix
36773679
\`\`\`
36783680
36793681
#### \`force\`

test/lib/commands/pkg.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,21 @@ t.test('workspaces', async t => {
617617
'should delete version field from workspace b'
618618
)
619619
})
620+
621+
t.test('fix', async t => {
622+
const { pkg, readPackageJson } = await mockNpm(t, {
623+
prefixDir: {
624+
'package.json': JSON.stringify({
625+
name: 'foo ',
626+
version: 'v1.1.1',
627+
}),
628+
},
629+
})
630+
631+
await pkg('fix')
632+
t.strictSame(
633+
readPackageJson(),
634+
{ name: 'foo', version: '1.1.1' },
635+
'fixes package.json issues'
636+
)
637+
})

0 commit comments

Comments
 (0)