Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds pkg sort command #7954

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/commands/pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Pkg extends BaseCommand {
'set [<array>[<index>].<key>=<value> ...]',
'set [<array>[].<key>=<value> ...]',
'fix',
'sort',
]

static params = [
Expand Down Expand Up @@ -43,6 +44,8 @@ class Pkg extends BaseCommand {
return this.delete(_args, { path, workspace }).then(p => p.save())
case 'fix':
return PackageJson.fix(path).then(p => p.save())
case 'sort':
return PackageJson.load(path).then(p => p.save({ sort: true }))
default:
throw this.usageError()
}
Expand Down
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 @@ -3727,6 +3727,7 @@ npm pkg delete <key> [<key> ...]
npm pkg set [<array>[<index>].<key>=<value> ...]
npm pkg set [<array>[].<key>=<value> ...]
npm pkg fix
npm pkg sort

Options:
[-f|--force] [--json]
Expand All @@ -3742,6 +3743,7 @@ npm pkg delete <key> [<key> ...]
npm pkg set [<array>[<index>].<key>=<value> ...]
npm pkg set [<array>[].<key>=<value> ...]
npm pkg fix
npm pkg sort
\`\`\`

#### \`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 @@ -759,3 +759,21 @@ t.test('fix', async t => {
'fixes package.json issues'
)
})

t.test('sort', async t => {
const { pkg, readPackageJson } = await mockNpm(t, {
prefixDir: {
'package.json': JSON.stringify({
version: '1.1.1',
name: 'foo',
}),
},
})

await pkg('sort')
t.strictSame(
readPackageJson(),
{ name: 'foo', version: '1.1.1' },
'sorts package.json fields'
)
})
Loading