Skip to content

Commit

Permalink
feat: add pkg fix subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jul 5, 2023
1 parent c61e037 commit 67459e7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/lib/content/commands/npm-pkg.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/commands/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Pkg extends BaseCommand {
'delete <key> [<key> ...]',
'set [<array>[<index>].<key>=<value> ...]',
'set [<array>[].<key>=<value> ...]',
'fix',
]

static params = [
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions tap-snapshots/test/lib/docs.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3660,6 +3660,7 @@ npm pkg get [<key> [<key> ...]]
npm pkg delete <key> [<key> ...]
npm pkg set [<array>[<index>].<key>=<value> ...]
npm pkg set [<array>[].<key>=<value> ...]
npm pkg fix
Options:
[-f|--force] [--json]
Expand All @@ -3674,6 +3675,7 @@ npm pkg get [<key> [<key> ...]]
npm pkg delete <key> [<key> ...]
npm pkg set [<array>[<index>].<key>=<value> ...]
npm pkg set [<array>[].<key>=<value> ...]
npm pkg fix
\`\`\`
#### \`force\`
Expand Down
18 changes: 18 additions & 0 deletions test/lib/commands/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
})

0 comments on commit 67459e7

Please sign in to comment.