Skip to content

Commit

Permalink
Merge pull request #3 from jkrems/jk-sort-scripts
Browse files Browse the repository at this point in the history
Group scripts w/ pre/post
  • Loading branch information
keithamus committed Jan 22, 2016
2 parents 5f3c650 + bb4f843 commit a040500
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ function sortPackageJson(packageJson) {
packageJson[key] = sortObjectKeys(packageJson[key], sortList);
}
}
/* b
* pre | * | post
* pre 0 | - | -
* a * + | 0 | -
* post + | + | 0
*/
function compareScriptKeys(a, b) {
if (a === b) return 0;
var aScript = a.replace(/^(pre|post)(.)/, '$2');
var bScript = b.replace(/^(pre|post)(.)/, '$2');
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;
// The rest is bigger: * vs. *pre; *post vs. *pre; *post vs. *
return 1;
}
return aScript < bScript ? -1 : 1;
}
sortSubKey('keywords');
sortSubKey('homepage');
sortSubKey('bugs', [ 'url', 'email' ]);
Expand All @@ -24,7 +43,7 @@ function sortPackageJson(packageJson) {
sortSubKey('man');
sortSubKey('directories', [ 'lib', 'bin', 'man', 'doc', 'example' ]);
sortSubKey('repository', [ 'type', 'url' ]);
sortSubKey('scripts');
sortSubKey('scripts', compareScriptKeys);
sortSubKey('config');
sortSubKey('browser');
sortSubKey('browserify');
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,24 @@ require('fs').readFile('./package.json', 'utf8', function (error, contents) {
keywords: ['thing'],
name: 'foo',
}), null, 2), '{\n "name": "foo",\n "version": "1.0.0",\n "keywords": [\n "thing"\n ],\n "dependencies": {}\n}');

assert.deepEqual(Object.keys(sortPackageJson({
scripts: {
test: 'node test.js',
multiply: '2 * 3', // between p(ostinstall) and install
watch: 'watch things',
postinstall: 'echo "Installed"',
start: 'node server.js',
posttest: 'abc',
pretest: 'xyz',
}
}).scripts), [
'postinstall',
'multiply',
'start',
'pretest',
'test',
'posttest',
'watch',
])
});

0 comments on commit a040500

Please sign in to comment.