diff --git a/lockfile/fs/src/envLockfile.ts b/lockfile/fs/src/envLockfile.ts index fa650f65d1..aa506e2c0a 100644 --- a/lockfile/fs/src/envLockfile.ts +++ b/lockfile/fs/src/envLockfile.ts @@ -4,10 +4,10 @@ import util from 'node:util' import { LOCKFILE_VERSION, WANTED_LOCKFILE } from '@pnpm/constants' import type { EnvLockfile } from '@pnpm/lockfile.types' -import { sortDirectKeys } from '@pnpm/object.key-sorting' import yaml from 'js-yaml' import writeFileAtomic from 'write-file-atomic' +import { sortLockfileKeys } from './sortLockfileKeys.js' import { lockfileYamlDump } from './write.js' import { extractMainDocument, streamReadFirstYamlDocument } from './yamlDocuments.js' @@ -58,7 +58,7 @@ export async function readEnvLockfile (rootDir: string): Promise { const lockfilePath = path.join(rootDir, WANTED_LOCKFILE) - const sorted = sortEnvLockfile(lockfile) + const sorted = sortLockfileKeys(lockfile) const envYaml = lockfileYamlDump(sorted) // Read existing main lockfile document to preserve it @@ -75,20 +75,3 @@ export async function writeEnvLockfile (rootDir: string, lockfile: EnvLockfile): const combined = `---\n${envYaml}\n---\n${mainDoc}` return writeFileAtomic(lockfilePath, combined) } - -function sortEnvLockfile (lockfile: EnvLockfile): EnvLockfile { - const importer: EnvLockfile['importers']['.'] = { - configDependencies: sortDirectKeys(lockfile.importers['.']?.configDependencies ?? {}), - } - if (lockfile.importers['.'].packageManagerDependencies && Object.keys(lockfile.importers['.'].packageManagerDependencies).length > 0) { - importer.packageManagerDependencies = sortDirectKeys(lockfile.importers['.'].packageManagerDependencies) - } - return { - lockfileVersion: lockfile.lockfileVersion, - importers: { - '.': importer, - }, - packages: sortDirectKeys(lockfile.packages), - snapshots: sortDirectKeys(lockfile.snapshots), - } -} diff --git a/lockfile/fs/src/sortLockfileKeys.ts b/lockfile/fs/src/sortLockfileKeys.ts index b2847a9cc1..30044e47e2 100644 --- a/lockfile/fs/src/sortLockfileKeys.ts +++ b/lockfile/fs/src/sortLockfileKeys.ts @@ -1,4 +1,4 @@ -import type { LockfileFile } from '@pnpm/lockfile.types' +import type { EnvLockfile, LockfileFile } from '@pnpm/lockfile.types' import { sortDeepKeys, sortDirectKeys, sortKeysByPriority } from '@pnpm/object.key-sorting' const ORDERED_KEYS = { @@ -44,7 +44,9 @@ const ROOT_KEYS: readonly RootKey[] = [ ] const ROOT_KEYS_ORDER = Object.fromEntries(ROOT_KEYS.map((key, index) => [key, index])) -export function sortLockfileKeys (lockfile: LockfileFile): LockfileFile { +export function sortLockfileKeys (lockfile: LockfileFile): LockfileFile +export function sortLockfileKeys (lockfile: EnvLockfile): EnvLockfile +export function sortLockfileKeys (lockfile: LockfileFile | EnvLockfile): LockfileFile | EnvLockfile { if (lockfile.importers != null) { lockfile.importers = sortDirectKeys(lockfile.importers) for (const [importerId, importer] of Object.entries(lockfile.importers)) { @@ -72,15 +74,17 @@ export function sortLockfileKeys (lockfile: LockfileFile): LockfileFile { }, pkg) } } - if (lockfile.catalogs != null) { + if ('catalogs' in lockfile && lockfile.catalogs != null) { lockfile.catalogs = sortDirectKeys(lockfile.catalogs) for (const [catalogName, catalog] of Object.entries(lockfile.catalogs)) { lockfile.catalogs[catalogName] = sortDeepKeys(catalog) } } - for (const key of ['time', 'patchedDependencies'] as const) { - if (!lockfile[key]) continue - lockfile[key] = sortDirectKeys(lockfile[key]) // eslint-disable-line @typescript-eslint/no-explicit-any + if ('time' in lockfile && lockfile.time != null) { + lockfile.time = sortDirectKeys(lockfile.time) + } + if ('patchedDependencies' in lockfile && lockfile.patchedDependencies != null) { + lockfile.patchedDependencies = sortDirectKeys(lockfile.patchedDependencies) } return sortKeysByPriority({ priority: ROOT_KEYS_ORDER }, lockfile) } diff --git a/lockfile/types/src/index.ts b/lockfile/types/src/index.ts index 64f2740567..f19bbe6929 100644 --- a/lockfile/types/src/index.ts +++ b/lockfile/types/src/index.ts @@ -165,13 +165,15 @@ export type PackageBin = string | { [name: string]: string } */ export type ResolvedDependencies = Record +export interface EnvImporterSnapshot { + configDependencies: Record + packageManagerDependencies?: Record +} + export interface EnvLockfile { lockfileVersion: string - importers: { - '.': { - configDependencies: Record - packageManagerDependencies?: Record - } + importers: Record & { + '.': EnvImporterSnapshot } packages: Record snapshots: Record