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: improve pnpm sorting #314

Merged
merged 4 commits into from
Mar 30, 2024
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
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
Loading