Skip to content

Commit

Permalink
feat!: more changes to the new lockfile format
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jan 7, 2023
1 parent 3c6620a commit cdc4c51
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 4 deletions.
@@ -1,3 +1,4 @@
import * as dp from '@pnpm/dependency-path'
import type { Lockfile, ProjectSnapshot, ResolvedDependencies } from '@pnpm/lockfile-types'
import {
INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX,
Expand All @@ -14,13 +15,69 @@ export function isExperimentalInlineSpecifiersFormat (
}

export function convertToInlineSpecifiersFormat (lockfile: Lockfile): InlineSpecifiersLockfile {
const convertedImporters = Object.fromEntries(
Object.entries(lockfile.importers ?? {})
.map(([importerId, pkgSnapshot]: [string, ProjectSnapshot]) => {
const newSnapshot = { ...pkgSnapshot }
if (newSnapshot.dependencies != null) {
newSnapshot.dependencies = mapValues(newSnapshot.dependencies, convertOldRefToNewRef)
}
if (newSnapshot.optionalDependencies != null) {
newSnapshot.optionalDependencies = mapValues(newSnapshot.optionalDependencies, convertOldRefToNewRef)
}
if (newSnapshot.devDependencies != null) {
newSnapshot.devDependencies = mapValues(newSnapshot.devDependencies, convertOldRefToNewRef)
}
return [importerId, newSnapshot]
})
)
return {
...lockfile,
packages: Object.fromEntries(
Object.entries(lockfile.packages ?? {})
.map(([depPath, pkgSnapshot]) => {
const newSnapshot = { ...pkgSnapshot }
if (newSnapshot.dependencies != null) {
newSnapshot.dependencies = mapValues(newSnapshot.dependencies, convertOldRefToNewRef)
}
if (newSnapshot.optionalDependencies != null) {
newSnapshot.optionalDependencies = mapValues(newSnapshot.optionalDependencies, convertOldRefToNewRef)
}
return [convertOldDepPathToNewDepPath(depPath), newSnapshot]
})
),
lockfileVersion: `${lockfile.lockfileVersion}${INLINE_SPECIFIERS_FORMAT_LOCKFILE_VERSION_SUFFIX}`,
importers: mapValues(lockfile.importers, convertProjectSnapshotToInlineSpecifiersFormat),
importers: mapValues(convertedImporters, convertProjectSnapshotToInlineSpecifiersFormat),
}
}

function convertOldDepPathToNewDepPath (oldDepPath: string) {
const parsedDepPath = dp.parse(oldDepPath)
if (!parsedDepPath.name || !parsedDepPath.version) return oldDepPath
let newDepPath = `/${parsedDepPath.name}@${parsedDepPath.version}`
if (parsedDepPath.peersSuffix) {
if (parsedDepPath.peersSuffix.startsWith('(')) {
newDepPath += parsedDepPath.peersSuffix
} else {
newDepPath += `_${parsedDepPath.peersSuffix}`
}
}
if (parsedDepPath.host) {
newDepPath = `${parsedDepPath.host}${newDepPath}`
}
return newDepPath
}

function convertOldRefToNewRef (oldRef: string) {
if (oldRef.startsWith('link:') || oldRef.startsWith('file:')) {
return oldRef
}
if (oldRef.includes('/')) {
return convertOldDepPathToNewDepPath(oldRef)
}
return oldRef
}

export function revertFromInlineSpecifiersFormatIfNecessary (lockfile: Lockfile | InlineSpecifiersLockfile): Lockfile {
return isExperimentalInlineSpecifiersFormat(lockfile)
? revertFromInlineSpecifiersFormat(lockfile)
Expand All @@ -36,11 +93,57 @@ export function revertFromInlineSpecifiersFormat (lockfile: InlineSpecifiersLock
throw new Error(`Unable to revert lockfile from inline specifiers format. Invalid version parsed: ${originalVersionStr}`)
}

const revertedImporters = mapValues(importers, revertProjectSnapshot)
const convertedImporters = Object.fromEntries(
Object.entries(revertedImporters ?? {})
.map(([importerId, pkgSnapshot]: [string, ProjectSnapshot]) => {
const newSnapshot = { ...pkgSnapshot }
if (newSnapshot.dependencies != null) {
newSnapshot.dependencies = mapValues(newSnapshot.dependencies, convertNewRefToOldRef)
}
if (newSnapshot.optionalDependencies != null) {
newSnapshot.optionalDependencies = mapValues(newSnapshot.optionalDependencies, convertNewRefToOldRef)
}
if (newSnapshot.devDependencies != null) {
newSnapshot.devDependencies = mapValues(newSnapshot.devDependencies, convertNewRefToOldRef)
}
return [importerId, newSnapshot]
})
)
return {
...rest,
packages: Object.fromEntries(
Object.entries(lockfile.packages ?? {})
.map(([depPath, pkgSnapshot]) => {
const newSnapshot = { ...pkgSnapshot }
if (newSnapshot.dependencies != null) {
newSnapshot.dependencies = mapValues(newSnapshot.dependencies, convertNewRefToOldRef)
}
if (newSnapshot.optionalDependencies != null) {
newSnapshot.optionalDependencies = mapValues(newSnapshot.optionalDependencies, convertNewRefToOldRef)
}
return [convertNewDepPathToOldDepPath(depPath), newSnapshot]
})
),
lockfileVersion: originalVersion,
importers: mapValues(importers, revertProjectSnapshot),
importers: convertedImporters,
}
}

function convertNewDepPathToOldDepPath (oldDepPath: string) {
if (!oldDepPath.includes('@', 1)) return oldDepPath
const index = oldDepPath.indexOf('@', oldDepPath.indexOf('/@') + 2)
return `${oldDepPath.substring(0, index)}/${oldDepPath.substring(index + 1)}`
}

function convertNewRefToOldRef (oldRef: string) {
if (oldRef.startsWith('link:') || oldRef.startsWith('file:')) {
return oldRef
}
if (oldRef.includes('@')) {
return convertNewDepPathToOldDepPath(oldRef)
}
return oldRef
}

function convertProjectSnapshotToInlineSpecifiersFormat (
Expand Down
Expand Up @@ -40,7 +40,7 @@ dependencies:
packages:
/foo/1.0.0:
/foo@1.0.0:
resolution: {integrity: sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=}
"
`;
86 changes: 86 additions & 0 deletions lockfile/lockfile-file/test/lockfileV6Converters.test.ts
@@ -0,0 +1,86 @@
import { convertToInlineSpecifiersFormat, revertFromInlineSpecifiersFormat } from '../lib/experiments/inlineSpecifiersLockfileConverters'

test('convertToInlineSpecifiersFormat()', () => {
const lockfileV5 = {
lockfileVersion: 5.0,
importers: {
project1: {
specifiers: {
foo: '^1.0.0',
bar: '^1.0.0',
qar: '^1.0.0',
tarball: '^1.0.0',
},
dependencies: {
foo: '1.0.0',
tarball: '@registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
},
devDependencies: {
bar: '/@bar/bar/1.0.0_@babel+core@2.0.0',
},
optionalDependencies: {
qar: 'reg.com/qar/1.0.0',
},
},
},
packages: {
'/foo/1.0.0': {
resolution: { integrity: '' },
},
'/@bar/bar/1.0.0_@babel+core@2.0.0': {
resolution: { integrity: '' },
},
'reg.com/qar/1.0.0': {
resolution: { integrity: '' },
},
'@registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz': {
resolution: { integrity: '' },
},
},
}
const lockfileV6 = {
lockfileVersion: '5-inlineSpecifiers',
importers: {
project1: {
dependencies: {
foo: {
specifier: '^1.0.0',
version: '1.0.0',
},
tarball: {
specifier: '^1.0.0',
version: '@registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
},
},
devDependencies: {
bar: {
specifier: '^1.0.0',
version: '/@bar/bar@1.0.0_@babel+core@2.0.0',
},
},
optionalDependencies: {
qar: {
specifier: '^1.0.0',
version: 'reg.com/qar@1.0.0',
},
},
},
},
packages: {
'/foo@1.0.0': {
resolution: { integrity: '' },
},
'/@bar/bar@1.0.0_@babel+core@2.0.0': {
resolution: { integrity: '' },
},
'reg.com/qar@1.0.0': {
resolution: { integrity: '' },
},
'@registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz': {
resolution: { integrity: '' },
},
},
}
expect(convertToInlineSpecifiersFormat(lockfileV5)).toEqual(lockfileV6)
expect(revertFromInlineSpecifiersFormat(lockfileV6)).toEqual(lockfileV5)
})
2 changes: 1 addition & 1 deletion packages/dependency-path/src/index.ts
Expand Up @@ -155,7 +155,7 @@ function depPathToFilenameUnescaped (depPath: string) {
if (depPath.startsWith('/')) {
depPath = depPath.substring(1)
}
const index = depPath.lastIndexOf('/')
const index = depPath.lastIndexOf('/', depPath.lastIndexOf('(') - 1)
return `${depPath.substring(0, index)}@${depPath.slice(index + 1)}`
}
return depPath.replace(':', '+')
Expand Down

0 comments on commit cdc4c51

Please sign in to comment.