Skip to content

Commit

Permalink
feat: Keep the "pre" when sorting certain scripts (#50)
Browse files Browse the repository at this point in the history
* feat: Keep the "pre" when sorting certain scripts

Before, "prepare" was being sorted as if it was named "pare" and
"prettier" was being sorted as if it was named "ttier". These scripts
are now sorted as is. This is done via a blacklist, so other similar
scripts could be added later.

* feat: Only omit "pre" or "post" based on a whitelist

The whitelist contains the default scripts defined in
https://docs.npmjs.com/misc/scripts plus any scripts in the package.json
that appear as a suffix of another script in the same package.json.
  • Loading branch information
Shingyx authored and keithamus committed Dec 3, 2018
1 parent ccbc536 commit c7d6900
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
39 changes: 36 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ var sortOrder = [
'preferGlobal',
'publishConfig',
];
// See https://docs.npmjs.com/misc/scripts
var defaultNpmScripts = [
'install',
'pack',
'prepare',
'publish',
'restart',
'shrinkwrap',
'start',
'stop',
'test',
'uninstall',
'version',
];

function sortPackageJson(packageJson) {
var wasString = false;
Expand All @@ -69,6 +83,18 @@ function sortPackageJson(packageJson) {
}
packageJson = JSON.parse(packageJson);
}

var prefixedScriptRegex = /^(pre|post)(.)/;
var prefixableScripts = defaultNpmScripts.slice();
if (typeof packageJson.scripts === 'object') {
Object.keys(packageJson.scripts).forEach(function (script) {
var prefixOmitted = script.replace(prefixedScriptRegex, '$2');
if (packageJson.scripts[prefixOmitted] && prefixableScripts.indexOf(prefixOmitted) < 0) {
prefixableScripts.push(prefixOmitted);
}
});
}

function sortSubKey(key, sortList, unique) {
if (Array.isArray(packageJson[key])) {
packageJson[key] = packageJson[key].sort();
Expand All @@ -81,6 +107,13 @@ function sortPackageJson(packageJson) {
packageJson[key] = sortObjectKeys(packageJson[key], sortList);
}
}
function toSortKey(script) {
var prefixOmitted = script.replace(prefixedScriptRegex, '$2');
if (prefixableScripts.indexOf(prefixOmitted) >= 0) {
return prefixOmitted;
}
return script;
}
/* b
* pre | * | post
* pre 0 | - | -
Expand All @@ -89,12 +122,12 @@ function sortPackageJson(packageJson) {
*/
function compareScriptKeys(a, b) {
if (a === b) return 0;
var aScript = a.replace(/^(pre|post)([^-])/, '$2');
var bScript = b.replace(/^(pre|post)([^-])/, '$2');
var aScript = toSortKey(a);
var bScript = toSortKey(b);
if (aScript === bScript) {
// pre* is always smaller; post* is always bigger
// Covers: pre* vs. *; pre* vs. post*; * vs. post*
if (a.indexOf('pre') === 0 || b.indexOf('post') === 0) return -1;
if (a === 'pre' + aScript || b === 'post' + bScript) return -1;
// The rest is bigger: * vs. *pre; *post vs. *pre; *post vs. *
return 1;
}
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,30 @@ require('fs').readFile('./package.json', 'utf8', function (error, contents) {
test: 'node test.js',
multiply: '2 * 3', // between p(ostinstall) and install
watch: 'watch things',
prewatch: 'echo "about to watch"',
postinstall: 'echo "Installed"',
start: 'node server.js',
posttest: 'abc',
pretest: 'xyz',
postprettier: 'echo "so pretty"',
preprettier: 'echo "not pretty"',
prettier: 'prettier -l "**/*.js"',
prepare: 'npm run build',
'pre-fetch-info': 'foo'
}
}).scripts), [
'postinstall',
'multiply',
'pre-fetch-info',
'prepare',
'preprettier',
'prettier',
'postprettier',
'start',
'pretest',
'test',
'posttest',
'prewatch',
'watch',
]);

Expand Down

0 comments on commit c7d6900

Please sign in to comment.