Skip to content

Commit

Permalink
feat: improve pnpm sorting (#314)
Browse files Browse the repository at this point in the history
* feat: improve sorting of pnpm.overrides

* fix: replace .at with .pop

* feat: handle parent/child overrides ('pkgA>pkgB')

* chore: simplify
  • Loading branch information
westrik authored Mar 30, 2024
1 parent af54e91 commit a55cbb7
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 224 deletions.
58 changes: 57 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import detectIndent from 'detect-indent'
import { detectNewlineGraceful as detectNewline } from 'detect-newline'
import gitHooks from 'git-hooks-list'
import isPlainObject from 'is-plain-obj'
import semver from 'semver'

const hasOwn =
Object.hasOwn ||
Expand Down Expand Up @@ -54,6 +55,38 @@ const overProperty =
: object
const sortGitHooks = sortObjectBy(gitHooks)

const sortObjectBySemver = sortObjectBy((a, b) => {
const parseNameAndVersionRange = (specifier) => {
// Ignore anything after > & rely on fallback alphanumeric sorting for that
const [nameAndVersion] = specifier.split('>')
const atMatches = [...nameAndVersion.matchAll('@')]
if (
!atMatches.length ||
(atMatches.length === 1 && atMatches[0].index === 0)
) {
return { name: specifier }
}
const splitIndex = atMatches.pop().index
return {
name: nameAndVersion.substring(0, splitIndex),
range: nameAndVersion.substring(splitIndex + 1),
}
}
const { name: aName, range: aRange } = parseNameAndVersionRange(a)
const { name: bName, range: bRange } = parseNameAndVersionRange(b)

if (aName !== bName) {
return aName.localeCompare(bName)
}
if (!aRange) {
return -1
}
if (!bRange) {
return 1
}
return semver.compare(semver.minVersion(aRange), semver.minVersion(bRange))
})

// https://github.com/eslint/eslint/blob/acc0e47572a9390292b4e313b4a4bf360d236358/conf/config-schema.js
const eslintBaseConfigProperties = [
// `files` and `excludedFiles` are only on `overrides[]`
Expand Down Expand Up @@ -128,6 +161,29 @@ const sortPrettierConfig = onObject(

const sortVolta = sortObjectBy(['node', 'npm', 'yarn'])

const pnpmBaseConfigProperties = [
'peerDependencyRules',
'neverBuiltDependencies',
'onlyBuiltDependencies',
'onlyBuiltDependenciesFile',
'allowedDeprecatedVersions',
'allowNonAppliedPatches',
'updateConfig',
'auditConfig',
'requiredScripts',
'supportedArchitectures',
'overrides',
'patchedDependencies',
'packageExtensions',
]

const sortPnpmConfig = onObject(
pipe([
sortObjectBy(pnpmBaseConfigProperties, true),
overProperty('overrides', sortObjectBySemver),
]),
)

// See https://docs.npmjs.com/misc/scripts
const defaultNpmScripts = new Set([
'install',
Expand Down Expand Up @@ -312,7 +368,7 @@ const fields = [
/* vscode */ { key: 'galleryBanner', over: sortObject },
/* vscode */ { key: 'preview' },
/* vscode */ { key: 'markdown' },
{ key: 'pnpm', over: sortObjectBy(undefined, true) },
{ key: 'pnpm', over: sortPnpmConfig },
]

const defaultSortOrder = fields.map(({ key }) => key)
Expand Down
Loading

0 comments on commit a55cbb7

Please sign in to comment.