diff --git a/lib/commands/pkg.js b/lib/commands/pkg.js index 5a236f6e62270..5422fa9d0236d 100644 --- a/lib/commands/pkg.js +++ b/lib/commands/pkg.js @@ -13,6 +13,7 @@ class Pkg extends BaseCommand { 'set [[].= ...]', 'set [[].= ...]', 'fix', + 'sort', ] static params = [ @@ -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() } diff --git a/tap-snapshots/test/lib/docs.js.test.cjs b/tap-snapshots/test/lib/docs.js.test.cjs index 0ebee0d7ae896..a4004b40e8ce2 100644 --- a/tap-snapshots/test/lib/docs.js.test.cjs +++ b/tap-snapshots/test/lib/docs.js.test.cjs @@ -3727,6 +3727,7 @@ npm pkg delete [ ...] npm pkg set [[].= ...] npm pkg set [[].= ...] npm pkg fix +npm pkg sort Options: [-f|--force] [--json] @@ -3742,6 +3743,7 @@ npm pkg delete [ ...] npm pkg set [[].= ...] npm pkg set [[].= ...] npm pkg fix +npm pkg sort \`\`\` #### \`force\` diff --git a/test/lib/commands/pkg.js b/test/lib/commands/pkg.js index f4d0278b04d9b..a74f4148cd8cb 100644 --- a/test/lib/commands/pkg.js +++ b/test/lib/commands/pkg.js @@ -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' + ) +})