fix: sort keys in the lockfile

This commit is contained in:
Zoltan Kochan
2024-03-05 12:00:26 +01:00
parent 9802ee6c08
commit c98cfec292

View File

@@ -1,6 +1,6 @@
import { lexCompare } from '@pnpm/util.lex-comparator'
import sortKeys from 'sort-keys'
import { type LockfileFile } from '@pnpm/lockfile-types'
import { type LockfileFileV7, type LockfileFile } from '@pnpm/lockfile-types'
const ORDERED_KEYS = {
resolution: 1,
@@ -57,7 +57,7 @@ function compareWithPriority (priority: Record<string, number>, left: string, ri
return lexCompare(left, right)
}
export function sortLockfileKeys (lockfile: LockfileFile) {
export function sortLockfileKeys (lockfile: LockfileFileV7) {
const compareRootKeys = compareWithPriority.bind(null, ROOT_KEYS_ORDER)
if (lockfile.importers != null) {
lockfile.importers = sortKeys(lockfile.importers)
@@ -77,6 +77,15 @@ export function sortLockfileKeys (lockfile: LockfileFile) {
})
}
}
if (lockfile.snapshots != null) {
lockfile.snapshots = sortKeys(lockfile.snapshots)
for (const [pkgId, pkg] of Object.entries(lockfile.snapshots)) {
lockfile.snapshots[pkgId] = sortKeys(pkg, {
compare: compareWithPriority.bind(null, ORDERED_KEYS),
deep: true,
})
}
}
for (const key of ['dependencies', 'devDependencies', 'optionalDependencies', 'time', 'patchedDependencies'] as const) {
if (!lockfile[key]) continue
lockfile[key] = sortKeys<any>(lockfile[key]) // eslint-disable-line @typescript-eslint/no-explicit-any