Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sort pre/post scripts with colon together #330

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,24 @@ const sortScripts = onObject((scripts, packageJson) => {
keys.sort()
}

const order = keys.flatMap((key) =>
prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key],
)
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
}, [])

return sortObjectKeys(scripts, order)
})
Expand Down
43 changes: 43 additions & 0 deletions tests/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,46 @@ 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',
},
},
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': '..',
},
},
})
}
Loading