feat(lockfile): resolution should be the first key (#3194)

This commit is contained in:
Zoltan Kochan
2021-02-25 21:25:02 +02:00
committed by GitHub
parent 6871d74b2b
commit 155e705971
7 changed files with 48 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/lockfile-file": minor
---
The "resolution" field should always be the first key. This will reduce the number of issues during lockfile merges.

View File

@@ -53,6 +53,7 @@
"js-yaml": "^4.0.0",
"normalize-path": "^3.0.0",
"ramda": "^0.27.1",
"sort-keys": "^4.2.0",
"strip-bom": "^4.0.0",
"write-file-atomic": "^3.0.3"
},

View File

@@ -6,6 +6,7 @@ import { WANTED_LOCKFILE } from '@pnpm/constants'
import rimraf from '@zkochan/rimraf'
import yaml from 'js-yaml'
import * as R from 'ramda'
import sortKeys from 'sort-keys'
import writeFileAtomicCB from 'write-file-atomic'
import logger from './logger'
@@ -17,7 +18,7 @@ const LOCKFILE_YAML_FORMAT = {
lineWidth: 1000,
noCompatMode: true,
noRefs: true,
sortKeys: true,
sortKeys: false,
}
export async function writeWantedLockfile (
@@ -62,7 +63,15 @@ async function writeLockfile (
}
function yamlStringify (lockfile: Lockfile, forceSharedFormat: boolean) {
const normalizedLockfile = normalizeLockfile(lockfile, forceSharedFormat)
let normalizedLockfile = normalizeLockfile(lockfile, forceSharedFormat)
normalizedLockfile = sortKeys(normalizedLockfile, {
compare: (left, right) => {
if (left === 'resolution') return -1
if (right === 'resolution') return 1
return left.localeCompare(right)
},
deep: true,
})
return yaml.dump(normalizedLockfile, LOCKFILE_YAML_FORMAT)
}

View File

@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`writeLockfiles() 1`] = `
"dependencies:
is-negative: 1.0.0
is-positive: 1.0.0
lockfileVersion: 5.2
packages:
/is-negative/1.0.0:
resolution:
integrity: sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=
dependencies:
is-positive: 2.0.0
/is-positive/1.0.0:
resolution:
integrity: sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=
/is-positive/2.0.0:
resolution:
integrity: sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=
specifiers:
is-negative: ^1.0.0
is-positive: ^1.0.0
"
`;

View File

@@ -54,6 +54,9 @@ test('writeLockfiles()', async () => {
})
expect(await readCurrentLockfile(projectPath, { ignoreIncompatible: false })).toEqual(wantedLockfile)
expect(await readWantedLockfile(projectPath, { ignoreIncompatible: false })).toEqual(wantedLockfile)
// Verifying the formatting of the lockfile
expect(fs.readFileSync(path.join(projectPath, WANTED_LOCKFILE), 'utf8')).toMatchSnapshot()
})
test('writeLockfiles() when no specifiers but dependencies present', async () => {

View File

@@ -38,7 +38,7 @@
"json5": "^2.1.3",
"parse-json": "^5.1.0",
"read-yaml-file": "^2.1.0",
"sort-keys": "^4.1.0",
"sort-keys": "^4.2.0",
"strip-bom": "^4.0.0"
},
"devDependencies": {

4
pnpm-lock.yaml generated
View File

@@ -945,6 +945,7 @@ importers:
js-yaml: 4.0.0
normalize-path: 3.0.0
ramda: 0.27.1
sort-keys: 4.2.0
strip-bom: 4.0.0
write-file-atomic: 3.0.3
devDependencies:
@@ -973,6 +974,7 @@ importers:
js-yaml: ^4.0.0
normalize-path: ^3.0.0
ramda: ^0.27.1
sort-keys: ^4.2.0
strip-bom: ^4.0.0
tempy: ^1.0.0
write-file-atomic: ^3.0.3
@@ -2440,7 +2442,7 @@ importers:
json5: ^2.1.3
parse-json: ^5.1.0
read-yaml-file: ^2.1.0
sort-keys: ^4.1.0
sort-keys: ^4.2.0
strip-bom: ^4.0.0
tempy: ^1.0.0
packages/read-projects-context: