Skip to content

Commit dc9aedc

Browse files
committed
fix(scripts): another tweak to improve relating pre scripts with their base script
1 parent 336a4a6 commit dc9aedc

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/package/scripts/script-comparator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export default function compareScriptNames(a, b) {
3939
const aStripped = stripPrefix(a);
4040
const bStripped = stripPrefix(b);
4141

42-
const aKey = aStripped.startsWith(`${b}:`) ? aStripped : a;
43-
const bKey = bStripped.startsWith(`${a}:`) ? bStripped : b;
42+
const aKey = aStripped !== a ? aStripped : a;
43+
const bKey = bStripped !== b ? bStripped : b;
4444

4545
return aKey.localeCompare(bKey);
4646
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ 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);
2017
});
2118

2219
it('should sort `pre` scripts ahead of their related scripts', async () => {
@@ -30,6 +27,14 @@ describe('script name comparator', () => {
3027
expect(compareScriptNames('lint:publish', 'prelint:publish')).toEqual(A_AFTER_B);
3128
});
3229

30+
it('should sort a `pre` script directly before its base script relative to unrelated scripts', async () => {
31+
const baseName = `b${any.word()}`;
32+
const scriptBetween = `c${any.word()}`;
33+
34+
expect(compareScriptNames(`pre${baseName}`, scriptBetween)).toEqual(A_BEFORE_B);
35+
expect(compareScriptNames(scriptBetween, `pre${baseName}`)).toEqual(A_AFTER_B);
36+
});
37+
3338
it('should sort `post` scripts after of their related scripts', async () => {
3439
expect(compareScriptNames(`post${baseScriptName}`, baseScriptName)).toEqual(A_AFTER_B);
3540
expect(compareScriptNames(baseScriptName, `post${baseScriptName}`)).toEqual(A_BEFORE_B);

test/integration/features/step_definitions/scripts-steps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {Before, Given, Then} from '@cucumber/cucumber';
66
Before(function () {
77
this.existingScripts = {
88
aaa: any.string(),
9+
ccc: any.string(),
910
zzz: any.string(),
1011
test: any.string(),
1112
'lint:md': any.string(),
@@ -109,6 +110,7 @@ Then('the scripts are ordered correctly', async function () {
109110
'aaa',
110111
'prebuild',
111112
'build',
113+
'ccc',
112114
'prepare',
113115
'zzz'
114116
]

0 commit comments

Comments
 (0)