Skip to content

Commit

Permalink
fix: do not sort scripts except pre/post
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorley committed Jan 29, 2021
1 parent 6ed5d07 commit 44ba8ff
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
43 changes: 41 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,45 @@ const sortPrettierConfig = onObject(
]),
)

// See https://docs.npmjs.com/misc/scripts
const defaultNpmScripts = new Set([
'install',
'pack',
'prepare',
'publish',
'restart',
'shrinkwrap',
'start',
'stop',
'test',
'uninstall',
'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
})

const order = keys.reduce(
(order, key) =>
order.concat(
prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key],
),
[],
)

return sortObjectKeys(scripts, order)
})

// fields marked `vscode` are for `Visual Studio Code extension manifest` only
// https://code.visualstudio.com/api/references/extension-manifest
// Supported fields:
Expand Down Expand Up @@ -183,8 +222,8 @@ const fields = [
'host',
]),
},
{ key: 'scripts' },
{ key: 'betterScripts' },
{ key: 'scripts', over: sortScripts },
{ key: 'betterScripts', over: sortScripts },
/* vscode */ { key: 'contributes', over: sortObject },
/* vscode */ { key: 'activationEvents', over: uniq },
{ key: 'husky', over: overProperty('hooks', sortGitHooks) },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"lint": "eslint .",
"semantic-release": "semantic-release",
"sort-package-json": "node cli.js package.json --check",
"test": "ava && dtslint --localTs node_modules/typescript/lib ",
"test": "ava && dtslint --localTs node_modules/typescript/lib",
"test-coverage": "nyc ava",
"update-snapshot": "ava -u && node cli.js package.json"
},
Expand Down
20 changes: 19 additions & 1 deletion tests/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fixture = {
watch: 'watch things',
prewatch: 'echo "about to watch"',
postinstall: 'echo "Installed"',
preinstall: 'echo "Installing"',
start: 'node server.js',
posttest: 'abc',
pretest: 'xyz',
Expand All @@ -17,10 +18,27 @@ const fixture = {
'pre-fetch-info': 'foo',
}

const expect = {
pretest: 'xyz',
test: 'node test.js',
posttest: 'abc',
multiply: '2 * 3',
prewatch: 'echo "about to watch"',
watch: 'watch things',
preinstall: 'echo "Installing"',
postinstall: 'echo "Installed"',
start: 'node server.js',
preprettier: 'echo "not pretty"',
prettier: 'prettier -l "**/*.js"',
postprettier: 'echo "so pretty"',
prepare: 'npm run build',
'pre-fetch-info': 'foo',
}

for (const field of ['scripts', 'betterScripts']) {
test(field, macro.sortObject, {
path: field,
value: fixture,
expect: fixture,
expect,
})
}

0 comments on commit 44ba8ff

Please sign in to comment.