Skip to content

Commit 0b2130b

Browse files
committed
fix(scripts): sort scripts alphabetically, including prefix when no related base script exists
1 parent fdf75e9 commit 0b2130b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/package/scripts/script-comparator.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ function isPostScriptFor(a, b) {
1111
}
1212

1313
function getBaseScriptOf(script) {
14-
return script.replace(/^(?:pre|post)/, '');
14+
if (script.startsWith('pre')) return script.slice(3);
15+
if (script.startsWith('post')) return script.slice(4);
16+
return script;
1517
}
1618

1719
function getCategoryOrder(base) {
@@ -36,8 +38,8 @@ export default function compareScriptNames(a, b) {
3638
const categoryDiff = getCategoryOrder(aBase) - getCategoryOrder(bBase);
3739
if (0 !== categoryDiff) return 0 > categoryDiff ? -1 : 1;
3840

39-
const baseCompare = aBase.localeCompare(bBase);
40-
if (0 !== baseCompare) return 0 > baseCompare ? -1 : 1;
41+
const aKey = bBase === aBase || b === aBase || aBase.startsWith(`${b}:`) ? aBase : a;
42+
const bKey = aBase === bBase || a === bBase || bBase.startsWith(`${a}:`) ? bBase : b;
4143

42-
return a.localeCompare(b);
44+
return aKey.localeCompare(bKey);
4345
}

src/package/scripts/script-comparator.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ describe('script name comparator', () => {
1414

1515
expect(compareScriptNames(a, b)).toEqual(A_BEFORE_B);
1616
expect(compareScriptNames(b, a)).toEqual(A_AFTER_B);
17+
18+
expect(compareScriptNames('pebfifu', 'prepare')).toEqual(A_BEFORE_B);
19+
expect(compareScriptNames('prepare', 'pebfifu')).toEqual(A_AFTER_B);
1720
});
1821

1922
it('should sort `pre` scripts ahead of their related scripts', async () => {
@@ -33,6 +36,9 @@ describe('script name comparator', () => {
3336

3437
expect(compareScriptNames('posttest', 'test')).toEqual(A_AFTER_B);
3538
expect(compareScriptNames('test', 'posttest')).toEqual(A_BEFORE_B);
39+
40+
expect(compareScriptNames(`postlint:${any.word()}`, `test:${any.word()}`)).toEqual(A_BEFORE_B);
41+
expect(compareScriptNames(`test:${any.word()}`, `postlint:${any.word()}`)).toEqual(A_AFTER_B);
3642
});
3743

3844
it('should sort the `test` script ahead of any sub-test scripts', async () => {

0 commit comments

Comments
 (0)