Skip to content

Commit

Permalink
feat: revert sort pre/post scripts with colon together (keithamus#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfalke-rtl committed Jan 21, 2025
1 parent 703badf commit 82a8e41
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 54 deletions.
21 changes: 3 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,24 +248,9 @@ const sortScripts = onObject((scripts, packageJson) => {
keys.sort()
}

const scriptsKeyMap = new Map()

keys
.flatMap((key) =>
prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key],
)
.forEach((key) => {
const [prefix] = key.split(':')
const keySet = scriptsKeyMap.has(prefix)
? scriptsKeyMap.get(prefix)
: new Set()
scriptsKeyMap.set(prefix, keySet.add(key))
})

const order = [...scriptsKeyMap.values()].flat().reduce((keys, keySet) => {
keys.push(...keySet)
return keys
}, [])
const order = keys.flatMap((key) =>
prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key],
)

return sortObjectKeys(scripts, order)
})
Expand Down
105 changes: 69 additions & 36 deletions tests/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,44 +87,77 @@ for (const field of ['scripts', 'betterScripts']) {
}

for (const field of ['scripts', 'betterScripts']) {
test(`${field} sort pre/post scripts with colon together`, macro.sortObject, {
value: {
[field]: {
prebuild: 'run-s prebuild:*',
build: 'run-s build:*',
postbuild: 'run-s prebuild:*',
'build:bar': 'node bar.js',
'build:baz': 'node baz.js',
'build:foo': 'node foo.js',
'd-unrelated': '..',
'e-unrelated': '..',
'f-unrelated': '..',
'postbuild:1': 'node prebuild.js 1',
'postbuild:2': 'node prebuild.js 2',
'postbuild:3': 'node prebuild.js 3',
'prebuild:1': 'node prebuild.js 1',
'prebuild:2': 'node prebuild.js 2',
'prebuild:3': 'node prebuild.js 3',
test(
`${field} does not sort pre/post scripts with colon together`,
macro.sortObject,
{
value: {
[field]: {
prebuild: 'run-s prebuild:*',
'prebuild:1': 'node prebuild.js 1',
'prebuild:2': 'node prebuild.js 2',
'prebuild:3': 'node prebuild.js 3',
build: 'run-s build:*',
'build:bar': 'node bar.js',
'build:baz': 'node baz.js',
'build:foo': 'node foo.js',
postbuild: 'run-s prebuild:*',
'postbuild:1': 'node prebuild.js 1',
'postbuild:2': 'node prebuild.js 2',
'postbuild:3': 'node prebuild.js 3',
'd-unrelated': '..',
'e-unrelated': '..',
'f-unrelated': '..',
},
},
expect: {
[field]: {
prebuild: 'run-s prebuild:*',
build: 'run-s build:*',
postbuild: 'run-s prebuild:*',
'build:bar': 'node bar.js',
'build:baz': 'node baz.js',
'build:foo': 'node foo.js',
'd-unrelated': '..',
'e-unrelated': '..',
'f-unrelated': '..',
'postbuild:1': 'node prebuild.js 1',
'postbuild:2': 'node prebuild.js 2',
'postbuild:3': 'node prebuild.js 3',
'prebuild:1': 'node prebuild.js 1',
'prebuild:2': 'node prebuild.js 2',
'prebuild:3': 'node prebuild.js 3',
},
},
},
expect: {
[field]: {
prebuild: 'run-s prebuild:*',
'prebuild:1': 'node prebuild.js 1',
'prebuild:2': 'node prebuild.js 2',
'prebuild:3': 'node prebuild.js 3',
build: 'run-s build:*',
'build:bar': 'node bar.js',
'build:baz': 'node baz.js',
'build:foo': 'node foo.js',
postbuild: 'run-s prebuild:*',
'postbuild:1': 'node prebuild.js 1',
'postbuild:2': 'node prebuild.js 2',
'postbuild:3': 'node prebuild.js 3',
'd-unrelated': '..',
'e-unrelated': '..',
'f-unrelated': '..',
)
}

for (const field of ['scripts', 'betterScripts']) {
test(
`${field} sort pre/post scripts together with base script independent of colon in name`,
macro.sortObject,
{
value: {
[field]: {
'pretest:es-check': 'echo',
'posttest:es-check': 'echo',
test: 'echo',
'test:coverage': 'echo',
'test:es-check': 'echo',
'test:types': 'echo',
},
},
expect: {
[field]: {
test: 'echo',
'test:coverage': 'echo',
'pretest:es-check': 'echo',
'test:es-check': 'echo',
'posttest:es-check': 'echo',
'test:types': 'echo',
},
},
},
})
)
}

0 comments on commit 82a8e41

Please sign in to comment.