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: better scripts order for follow npm run order #171

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 2 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions tests/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.