diff --git a/index.js b/index.js index fca5fe8e..c2a362f0 100755 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const detectIndent = require('detect-indent') const detectNewline = require('detect-newline').graceful const gitHooks = require('git-hooks-list') const isPlainObject = require('is-plain-obj') +const sortPackageJsonScripts = require('sort-package-json-scripts').default const hasOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property) @@ -131,31 +132,7 @@ const defaultNpmScripts = new Set([ 'version', ]) -const sortScripts = onObject(scripts => { - const names = Object.keys(scripts) - const prefixable = new Set() - - const keys = names - .map(name => { - const omitted = name.replace(/^(?:pre|post)/, '') - if (defaultNpmScripts.has(omitted) || names.includes(omitted)) { - prefixable.add(omitted) - return omitted - } - return name - }) - .sort() - - const order = keys.reduce( - (order, key) => - order.concat( - prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key], - ), - [], - ) - - return sortObjectKeys(scripts, order) -}) +const sortScripts = onObject(sortPackageJsonScripts) // fields marked `vscode` are for `Visual Studio Code extension manifest` only // https://code.visualstudio.com/api/references/extension-manifest diff --git a/package.json b/package.json index 89df0e08..164a1fe6 100644 --- a/package.json +++ b/package.json @@ -27,10 +27,10 @@ ], "scripts": { "lint": "eslint .", - "semantic-release": "semantic-release", - "sort-package-json": "node cli.js package.json --check", "test": "ava", "test-coverage": "nyc ava", + "semantic-release": "semantic-release", + "sort-package-json": "node cli.js package.json --check", "update-snapshot": "ava -u && node cli.js package.json" }, "husky": { @@ -76,7 +76,8 @@ "git-hooks-list": "1.0.3", "globby": "11.0.0", "is-plain-obj": "2.1.0", - "sort-object-keys": "^1.1.3" + "sort-object-keys": "^1.1.3", + "sort-package-json-scripts": "^1.0.3" }, "devDependencies": { "@commitlint/cli": "8.3.5", diff --git a/tests/scripts.js b/tests/scripts.js index 0acde8b5..98db5ebe 100644 --- a/tests/scripts.js +++ b/tests/scripts.js @@ -18,19 +18,19 @@ const fixture = { } const expect = { + start: 'node server.js', + pretest: 'xyz', + test: 'node test.js', + posttest: 'abc', postinstall: 'echo "Installed"', - multiply: '2 * 3', - 'pre-fetch-info': 'foo', prepare: 'npm run build', + multiply: '2 * 3', preprettier: 'echo "not pretty"', prettier: 'prettier -l "**/*.js"', postprettier: 'echo "so pretty"', - start: 'node server.js', - pretest: 'xyz', - test: 'node test.js', - posttest: 'abc', + 'pre-fetch-info': 'foo', prewatch: 'echo "about to watch"', - watch: 'watch things', + watch: 'watch things' } for (const field of ['scripts', 'betterScripts']) { diff --git a/tests/snapshots/cli.js.snap b/tests/snapshots/cli.js.snap index 3efb5c67..14d79691 100644 Binary files a/tests/snapshots/cli.js.snap and b/tests/snapshots/cli.js.snap differ