Skip to content

Commit 9f1101b

Browse files
MichaelDeBoeyBrody Dingelfisker
authored
feat: allow sort scripts without run-s (#277)
* fix: allow sort for run-s * chore: fix remarks * Fix check logic * Update tests * One more test * Fix * Use string literal * Minor tweak * test: fix tests --------- Co-authored-by: Brody Dingel <bdingel@hy-vee.com> Co-authored-by: fisker <lionkay@gmail.com>
1 parent 819cd97 commit 9f1101b

File tree

2 files changed

+85
-31
lines changed

2 files changed

+85
-31
lines changed

index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ const hasDevDependency = (dependency, packageJson) => {
218218
)
219219
}
220220

221+
const runSRegExp =
222+
/(?<=^|[\s&;<>|(])(?:run-s|npm-run-all .*(?:--sequential|--serial|-s))(?=$|[\s&;<>|)])/
223+
224+
const isSequentialScript = (command) =>
225+
command.includes('*') && runSRegExp.test(command)
226+
227+
const hasSequentialScript = (packageJson) => {
228+
if (!hasDevDependency('npm-run-all', packageJson)) {
229+
return false
230+
}
231+
const scripts = ['scripts', 'betterScripts'].flatMap((field) =>
232+
packageJson[field] ? Object.values(packageJson[field]) : [],
233+
)
234+
return scripts.some((script) => isSequentialScript(script))
235+
}
236+
221237
const sortScripts = onObject((scripts, packageJson) => {
222238
const names = Object.keys(scripts)
223239
const prefixable = new Set()
@@ -231,10 +247,7 @@ const sortScripts = onObject((scripts, packageJson) => {
231247
return name
232248
})
233249

234-
if (
235-
!hasDevDependency('npm-run-all', packageJson) &&
236-
!hasDevDependency('npm-run-all2', packageJson)
237-
) {
250+
if (!hasSequentialScript(packageJson)) {
238251
keys.sort()
239252
}
240253

tests/scripts.js

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from 'ava'
2+
import sortPackageJson from '../index.js'
23
import { macro } from './_helpers.js'
34

45
const fixture = {
@@ -35,37 +36,77 @@ const expectAllSorted = {
3536
watch: 'watch things',
3637
}
3738

38-
const expectPreAndPostSorted = {
39-
pretest: 'xyz',
40-
test: 'node test.js',
41-
posttest: 'abc',
42-
multiply: '2 * 3',
43-
prewatch: 'echo "about to watch"',
44-
watch: 'watch things',
45-
preinstall: 'echo "Installing"',
46-
postinstall: 'echo "Installed"',
47-
start: 'node server.js',
48-
preprettier: 'echo "not pretty"',
49-
prettier: 'prettier -l "**/*.js"',
50-
postprettier: 'echo "so pretty"',
51-
prepare: 'npm run build',
52-
'pre-fetch-info': 'foo',
53-
}
54-
5539
for (const field of ['scripts', 'betterScripts']) {
56-
test(`${field} when npm-run-all is not a dev dependency`, macro.sortObject, {
40+
test(`${field} when npm-run-all is NOT a dev dependency`, macro.sortObject, {
5741
value: { [field]: fixture },
5842
expect: { [field]: expectAllSorted },
5943
})
60-
test(`${field} when npm-run-all is a dev dependency`, macro.sortObject, {
61-
value: {
62-
[field]: fixture,
63-
devDependencies: { 'npm-run-all': '^1.0.0' },
64-
},
65-
expect: {
66-
[field]: expectPreAndPostSorted,
67-
devDependencies: { 'npm-run-all': '^1.0.0' },
44+
45+
test(
46+
`${field} when npm-run-all IS a dev dependency, but is NOT used in scripts`,
47+
macro.sortObject,
48+
{
49+
value: {
50+
[field]: { z: 'z', a: 'a' },
51+
devDependencies: { 'npm-run-all': '^1.0.0' },
52+
},
53+
expect: {
54+
[field]: { a: 'a', z: 'z' },
55+
devDependencies: { 'npm-run-all': '^1.0.0' },
56+
},
6857
},
58+
)
59+
}
60+
61+
// `run-s` command
62+
function sortScriptsWithNpmRunAll(script) {
63+
const packageJson = {
64+
scripts: { z: 'z', a: 'a', maybeRunS: script },
65+
devDependencies: { 'npm-run-all': '^1.0.0' },
66+
}
67+
68+
return Object.keys(sortPackageJson(packageJson).scripts)
69+
}
70+
const sortedScripts = ['a', 'maybeRunS', 'z']
71+
const unsortedScripts = ['z', 'a', 'maybeRunS']
72+
for (const { script, expected } of [
73+
// Should NOT sort
74+
{ script: 'run-s "lint:*"', expected: unsortedScripts },
75+
{ script: 'npm-run-all -s "lint:*"', expected: unsortedScripts },
76+
{ script: 'npm-run-all --sequential "lint:*"', expected: unsortedScripts },
77+
{ script: 'npm-run-all --serial "lint:*"', expected: unsortedScripts },
78+
{ script: 'npm-run-all "lint:*" --sequential', expected: unsortedScripts },
79+
{ script: 'foo&&npm-run-all --serial "lint:*"', expected: unsortedScripts },
80+
{ script: 'foo||npm-run-all --serial "lint:*"', expected: unsortedScripts },
81+
{ script: 'foo|npm-run-all --serial "lint:*"', expected: unsortedScripts },
82+
{ script: 'foo>npm-run-all --serial "lint:*"', expected: unsortedScripts },
83+
{ script: 'foo<npm-run-all --serial "lint:*"', expected: unsortedScripts },
84+
{
85+
script: 'cross-env FOO=1 npm-run-all --serial "lint:*"',
86+
expected: unsortedScripts,
87+
},
88+
{ script: 'npm-run-all "lint:*" --serial&&foo', expected: unsortedScripts },
89+
{ script: 'npm-run-all "lint:*" --serial|foo', expected: unsortedScripts },
90+
{ script: 'npm-run-all "lint:*" --serial||foo', expected: unsortedScripts },
91+
{ script: 'npm-run-all "lint:*" --serial>foo', expected: unsortedScripts },
92+
{ script: 'npm-run-all "lint:*" --serial<foo', expected: unsortedScripts },
93+
{ script: 'npm-run-all --serial "lint:*"&&foo', expected: unsortedScripts },
94+
{ script: 'npm-run-all "lint:*" --serial;foo', expected: unsortedScripts },
95+
{ script: '(npm-run-all "lint:*" --serial)|foo', expected: unsortedScripts },
96+
97+
// Should sort
98+
{ script: 'run-s lint:a lint:b', expected: sortedScripts },
99+
{ script: 'not-run-s *', expected: sortedScripts },
100+
{ script: 'npm-run-all * --serial!', expected: sortedScripts },
101+
{ script: 'looks like && run-s-but-its-not *', expected: sortedScripts },
102+
{ script: 'npm-run-all *', expected: sortedScripts },
103+
{ script: 'npm-run-all --parallel watch:*', expected: sortedScripts },
104+
105+
// False positive
106+
{ script: 'rm -rf dist/* && run-s lint:a lint:b', expected: unsortedScripts },
107+
]) {
108+
test(`command: '${script}'`, (t) => {
109+
t.deepEqual(sortScriptsWithNpmRunAll(script), expected)
69110
})
70111
}
71112

@@ -80,7 +121,7 @@ for (const field of ['scripts', 'betterScripts']) {
80121
devDependencies: { 'npm-run-all2': '^1.0.0' },
81122
},
82123
expect: {
83-
[field]: expectPreAndPostSorted,
124+
[field]: expectAllSorted,
84125
devDependencies: { 'npm-run-all2': '^1.0.0' },
85126
},
86127
})

0 commit comments

Comments
 (0)