Skip to content

Commit

Permalink
fix: disable sorting scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorley committed Jan 27, 2021
1 parent b70c4f9 commit 6ed5d07
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 128 deletions.
67 changes: 3 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const sortDirectories = sortObjectBy([
])
const overProperty = (property, over) => (object) =>
hasOwnProperty(object, property)
? Object.assign(object, { [property]: over(object[property], object) })
? Object.assign(object, { [property]: over(object[property]) })
: object
const sortGitHooks = sortObjectBy(gitHooks)

Expand Down Expand Up @@ -116,67 +116,6 @@ const sortPrettierConfig = onObject(
]),
)

// See https://docs.npmjs.com/misc/scripts
const defaultNpmScripts = new Set([
'install',
'pack',
'prepare',
'publish',
'restart',
'shrinkwrap',
'start',
'stop',
'test',
'uninstall',
'version',
])

const sortScripts = (scripts, parent) => {
if (!isPlainObject(scripts)) return scripts

// npm-run-all provides commands that are sensitive to script-ordering.
// If we detect its usage, do not re-order scripts.
if (hasOwnProperty(parent, 'devDependencies')) {
if (Object.keys(parent.devDependencies).includes('npm-run-all')) {
return scripts
}
}

// These are the commands that npm-run-all provides that are sensitive to script-ordering
// Explicitly check for them in case npm-run-all is installed globally
if (Object.values(scripts).some((script) => script.includes('npm-run-all'))) {
return scripts
}

if (Object.values(scripts).some((script) => script.includes('run-s'))) {
return scripts
}

const names = Object.keys(scripts)
const prefixable = new Set()

const keys = names
.map((name) => {
const omitted = name.replace(/^(?:pre|post)/, '')
if (defaultNpmScripts.has(omitted) || names.includes(omitted)) {
prefixable.add(omitted)
return omitted
}
return name
})
.sort()

const order = keys.reduce(
(order, key) =>
order.concat(
prefixable.has(key) ? [`pre${key}`, key, `post${key}`] : [key],
),
[],
)

return sortObjectKeys(scripts, order)
}

// fields marked `vscode` are for `Visual Studio Code extension manifest` only
// https://code.visualstudio.com/api/references/extension-manifest
// Supported fields:
Expand Down Expand Up @@ -244,8 +183,8 @@ const fields = [
'host',
]),
},
{ key: 'scripts', over: sortScripts },
{ key: 'betterScripts', over: sortScripts },
{ key: 'scripts' },
{ key: 'betterScripts' },
/* vscode */ { key: 'contributes', over: sortObject },
/* vscode */ { key: 'activationEvents', over: uniq },
{ key: 'husky', over: overProperty('hooks', sortGitHooks) },
Expand Down
65 changes: 1 addition & 64 deletions tests/scripts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const test = require('ava')
const sortPackageJson = require('..')
const { macro } = require('./_helpers')

const fixture = {
Expand All @@ -18,72 +17,10 @@ const fixture = {
'pre-fetch-info': 'foo',
}

const expect = {
postinstall: 'echo "Installed"',
multiply: '2 * 3',
'pre-fetch-info': 'foo',
prepare: 'npm run build',
preprettier: 'echo "not pretty"',
prettier: 'prettier -l "**/*.js"',
postprettier: 'echo "so pretty"',
start: 'node server.js',
pretest: 'xyz',
test: 'node test.js',
posttest: 'abc',
prewatch: 'echo "about to watch"',
watch: 'watch things',
}

for (const field of ['scripts', 'betterScripts']) {
test(field, macro.sortObject, {
path: field,
value: fixture,
expect: expect,
})

test(`${field} with npm-run-all devDependency`, (t) => {
const packageJson = {
[field]: fixture,
devDependencies: {
'npm-run-all': '*',
},
}

t.is(
sortPackageJson(JSON.stringify(packageJson)),
JSON.stringify(packageJson),
)
})

test(`${field} with npm-run-all usage`, (t) => {
const newFixture = {
...fixture,
test: 'npm-run-all foo:*',
}

const packageJson = {
[field]: newFixture,
}

t.is(
sortPackageJson(JSON.stringify(packageJson)),
JSON.stringify(packageJson),
)
})

test(`${field} with run-s usage`, (t) => {
const newFixture = {
...fixture,
test: 'run-s foo:*',
}

const packageJson = {
[field]: newFixture,
}

t.is(
sortPackageJson(JSON.stringify(packageJson)),
JSON.stringify(packageJson),
)
expect: fixture,
})
}

0 comments on commit 6ed5d07

Please sign in to comment.