Skip to content

Commit 359bb42

Browse files
committed
fix(scripts): properly sort pre scripts before base scripts beyond categorized scripts
1 parent 0940c58 commit 359bb42

3 files changed

Lines changed: 21 additions & 27 deletions

File tree

src/package/scripts/script-comparator.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ function isPostScriptFor(a, b) {
1010
return isRelatedScript(a, b, 'post');
1111
}
1212

13-
function getBaseScriptOf(script) {
13+
function stripPrefix(script) {
1414
if (script.startsWith('pre')) return script.slice(3);
1515
if (script.startsWith('post')) return script.slice(4);
1616
return script;
1717
}
1818

19-
function getCategoryOrder(base) {
19+
function getCategoryOrder(script) {
20+
const base = stripPrefix(script);
2021
if ('test' === base) return 0;
2122
if (base.startsWith('lint:')) return 1;
2223
if (base.startsWith('test:unit')) return 2;
@@ -32,14 +33,14 @@ export default function compareScriptNames(a, b) {
3233
if (isPostScriptFor(a, b)) return 1;
3334
if (isPostScriptFor(b, a)) return -1;
3435

35-
const aBase = getBaseScriptOf(a);
36-
const bBase = getBaseScriptOf(b);
37-
38-
const categoryDiff = getCategoryOrder(aBase) - getCategoryOrder(bBase);
36+
const categoryDiff = getCategoryOrder(a) - getCategoryOrder(b);
3937
if (0 !== categoryDiff) return 0 > categoryDiff ? -1 : 1;
4038

41-
const aKey = bBase === aBase || b === aBase || aBase.startsWith(`${b}:`) ? aBase : a;
42-
const bKey = aBase === bBase || a === bBase || bBase.startsWith(`${a}:`) ? bBase : b;
39+
const aStripped = stripPrefix(a);
40+
const bStripped = stripPrefix(b);
41+
42+
const aKey = aStripped.startsWith(`${b}:`) ? aStripped : a;
43+
const bKey = bStripped.startsWith(`${a}:`) ? bStripped : b;
4344

4445
return aKey.localeCompare(bKey);
4546
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ describe('script name comparator', () => {
8989
it('should sort `test:unit` sub-scripts after `test:unit`', async () => {
9090
expect(compareScriptNames('test:unit', 'test:unit:base')).toEqual(A_BEFORE_B);
9191
expect(compareScriptNames('test:unit:base', 'test:unit')).toEqual(A_AFTER_B);
92+
93+
expect(compareScriptNames('test:unit', 'pretest:unit:base')).toEqual(A_BEFORE_B);
94+
expect(compareScriptNames('pretest:unit:base', 'test:unit')).toEqual(A_AFTER_B);
9295
});
9396

9497
it('should sort `test:integration` sub-scripts after `test:integration`', async () => {

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {Before, Given, Then} from '@cucumber/cucumber';
55

66
Before(function () {
77
this.existingScripts = {
8-
...any.simpleObject(),
8+
aaa: any.string(),
9+
zzz: any.string(),
910
test: any.string(),
1011
'lint:md': any.string(),
1112
prepare: any.string(),
@@ -14,8 +15,10 @@ Before(function () {
1415
'test:integration:base': any.string(),
1516
'test:integration:debug': any.string(),
1617
'test:integration:focus': any.string(),
18+
build: any.string(),
1719
'test:integration:focus:debug': any.string(),
1820
'test:integration:wip': any.string(),
21+
prebuild: any.string(),
1922
'test:integration:wip:debug': any.string(),
2023
'test:unit': any.string(),
2124
pretest: any.string(),
@@ -83,23 +86,6 @@ Then('the updated test script includes build', async function () {
8386

8487
Then('the scripts are ordered correctly', async function () {
8588
const {scripts} = JSON.parse(await fs.readFile(`${process.cwd()}/package.json`, 'utf8'));
86-
const {
87-
pretest,
88-
test,
89-
'test:unit': testUnit,
90-
'test:integration': testIntegration,
91-
'pretest:integration:base': pretestIntegrationBase,
92-
'test:integration:base': testIntegrationBase,
93-
'test:integration:debug': testIntegrationDebug,
94-
'test:integration:focus': testIntegrationFocus,
95-
'test:unit:base': testUnitBase,
96-
'test:integration:focus:debug': testIntegrationFocusDebug,
97-
'test:integration:wip': testIntegrationWip,
98-
'test:integration:wip:debug': testIntegrationWipDebug,
99-
'prelint:publish': prelintPublish,
100-
'lint:md': lintMd,
101-
...otherExistingScripts
102-
} = this.existingScripts;
10389

10490
assert.deepEqual(
10591
Object.keys(scripts),
@@ -120,7 +106,11 @@ Then('the scripts are ordered correctly', async function () {
120106
'test:integration:focus:debug',
121107
'test:integration:wip',
122108
'test:integration:wip:debug',
123-
...Object.keys(otherExistingScripts).sort((a, b) => a.localeCompare(b))
109+
'aaa',
110+
'prebuild',
111+
'build',
112+
'prepare',
113+
'zzz'
124114
]
125115
);
126116
});

0 commit comments

Comments
 (0)