Skip to content

Commit

Permalink
fix: allow sort for run-s
Browse files Browse the repository at this point in the history
  • Loading branch information
Brody Dingel authored and MichaelDeBoey committed Nov 22, 2022
1 parent 937e783 commit 70d68f8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
28 changes: 26 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ const hasDevDependency = (dependency, packageJson) => {
)
}

const runSValues = [new RegExp('run-s')]

const hasRunS = (packageJson) => {
if (hasDevDependency('npm-run-all', packageJson)) {
const scripts = packageJson.scripts
const betterScripts = packageJson.betterScripts

if (scripts) {
return Object.values(scripts).some((script) =>
runSValues.some((runSValue) => runSValue.test(script)),
)
}

if (betterScripts) {
return Object.values(betterScripts).some((script) =>
runSValues.some((runSValue) => runSValue.test(script)),
)
}
}

return false
}

const sortScripts = onObject((scripts, packageJson) => {
const names = Object.keys(scripts)
const prefixable = new Set()
Expand All @@ -161,7 +184,7 @@ const sortScripts = onObject((scripts, packageJson) => {
return name
})

if (!hasDevDependency('npm-run-all', packageJson)) {
if (!hasRunS(packageJson)) {
keys.sort()
}

Expand All @@ -172,8 +195,9 @@ const sortScripts = onObject((scripts, packageJson) => {
),
[],
)
const toReturn = sortObjectKeys(scripts, order)

return sortObjectKeys(scripts, order)
return toReturn
})

// fields marked `vscode` are for `Visual Studio Code extension manifest` only
Expand Down
60 changes: 49 additions & 11 deletions tests/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ const fixture = {
'pre-fetch-info': 'foo',
}

const fixtureWithRunS = {
test: 'node test.js',
multiply: '2 * 3', // between p(ostinstall) and install
watch: 'watch things',
prewatch: 'echo "about to watch"',
postinstall: 'echo "Installed"',
preinstall: 'echo "Installing"',
start: 'node server.js',
posttest: 'run-s abc def',
pretest: 'xyz',
postprettier: 'echo "so pretty"',
preprettier: 'echo "not pretty"',
prettier: 'prettier -l "**/*.js"',
prepare: 'npm run build',
'pre-fetch-info': 'foo',
}

const expectAllSorted = {
preinstall: 'echo "Installing"',
postinstall: 'echo "Installed"',
Expand All @@ -38,7 +55,7 @@ const expectAllSorted = {
const expectPreAndPostSorted = {
pretest: 'xyz',
test: 'node test.js',
posttest: 'abc',
posttest: 'run-s abc def',
multiply: '2 * 3',
prewatch: 'echo "about to watch"',
watch: 'watch things',
Expand All @@ -53,18 +70,39 @@ const expectPreAndPostSorted = {
}

for (const field of ['scripts', 'betterScripts']) {
test(`${field} when npm-run-all is not a dev dependency`, macro.sortObject, {
test(`${field} when npm-run-all is NOT a dev dependency`, macro.sortObject, {
value: { [field]: fixture },
expect: { [field]: expectAllSorted },
})
test(`${field} when npm-run-all is a dev dependency`, macro.sortObject, {
value: {
[field]: fixture,
devDependencies: { 'npm-run-all': '^1.0.0' },
},
expect: {
[field]: expectPreAndPostSorted,
devDependencies: { 'npm-run-all': '^1.0.0' },

for (const type of ['run-s']) {
test(
`${field} when npm-run-all IS a dev dependency, and IS used in scripts in form of ${type}`,
macro.sortObject,
{
value: {
[field]: fixtureWithRunS,
devDependencies: { 'npm-run-all': '^1.0.0' },
},
expect: {
[field]: expectPreAndPostSorted,
devDependencies: { 'npm-run-all': '^1.0.0' },
},
},
)
}
test(
`${field} when npm-run-all IS a dev dependency, but is NOT used in scripts`,
macro.sortObject,
{
value: {
[field]: fixture,
devDependencies: { 'npm-run-all': '^1.0.0' },
},
expect: {
[field]: expectAllSorted,
devDependencies: { 'npm-run-all': '^1.0.0' },
},
},
})
)
}

0 comments on commit 70d68f8

Please sign in to comment.