fix: pnpm link should keep the indentations in the updated manifest

This commit is contained in:
Zoltan Kochan
2025-01-07 02:48:02 +01:00
parent c0c63ef5b5
commit e050221384
6 changed files with 36 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/read-project-manifest": patch
---
Export `WriteProjectManifest`.

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-installation": patch
"pnpm": patch
---
`pnpm link` should keep the indentation in the `package.json` file that it updates.

View File

@@ -0,0 +1,16 @@
import path from 'path'
import util from 'util'
import { readProjectManifest, type WriteProjectManifest } from '@pnpm/read-project-manifest'
import { writeProjectManifest } from '@pnpm/write-project-manifest'
export async function createProjectManifestWriter (projectDir: string): Promise<WriteProjectManifest> {
try {
const { writeProjectManifest } = await readProjectManifest(projectDir)
return writeProjectManifest
} catch (err) {
if (util.types.isNativeError(err) && 'code' in err && err.code === 'ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND') {
return writeProjectManifest.bind(null, path.join(projectDir, 'package.json')) as WriteProjectManifest
}
throw err
}
}

View File

@@ -10,7 +10,6 @@ import { DEPENDENCIES_FIELDS, type ProjectManifest, type Project } from '@pnpm/t
import { PnpmError } from '@pnpm/error'
import { arrayOfWorkspacePackagesToMap } from '@pnpm/get-context'
import { findWorkspacePackages } from '@pnpm/workspace.find-packages'
import { writeProjectManifest } from '@pnpm/write-project-manifest'
import {
type WorkspacePackages,
} from '@pnpm/core'
@@ -18,6 +17,7 @@ import { logger } from '@pnpm/logger'
import pick from 'ramda/src/pick'
import partition from 'ramda/src/partition'
import renderHelp from 'render-help'
import { createProjectManifestWriter } from './createProjectManifestWriter'
import { getSaveType } from './getSaveType'
import * as install from './install'
@@ -128,6 +128,8 @@ export async function handler (
})
}
const writeProjectManifest = await createProjectManifestWriter(opts.rootProjectManifestDir)
// pnpm link
if ((params == null) || (params.length === 0)) {
const cwd = process.cwd()
@@ -139,7 +141,7 @@ export async function handler (
const newManifest = opts.rootProjectManifest ?? {}
await addLinkToManifest(opts, newManifest, cwd, linkOpts.dir)
await writeProjectManifest(path.join(opts.rootProjectManifestDir, 'package.json'), newManifest)
await writeProjectManifest(newManifest)
await install.handler({
...linkOpts,
frozenLockfileIfExists: false,
@@ -160,7 +162,7 @@ export async function handler (
})
)
await writeProjectManifest(path.join(opts.rootProjectManifestDir, 'package.json'), newManifest)
await writeProjectManifest(newManifest)
await install.handler({
...linkOpts,
frozenLockfileIfExists: false,

View File

@@ -1,8 +1,7 @@
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { writeProjectManifest } from '@pnpm/write-project-manifest'
import renderHelp from 'render-help'
import { createProjectManifestWriter } from './createProjectManifestWriter'
import * as install from './install'
export const cliOptionsTypes = install.cliOptionsTypes
@@ -58,7 +57,8 @@ export async function handler (
}
}
}
await writeProjectManifest(path.join(opts.rootProjectManifestDir, 'package.json'), opts.rootProjectManifest)
const writeProjectManifest = await createProjectManifestWriter(opts.rootProjectManifestDir)
await writeProjectManifest(opts.rootProjectManifest)
await install.handler(opts)
return undefined
}

View File

@@ -14,7 +14,7 @@ import {
readJsonFile,
} from './readFile'
type WriteProjectManifest = (manifest: ProjectManifest, force?: boolean) => Promise<void>
export type WriteProjectManifest = (manifest: ProjectManifest, force?: boolean) => Promise<void>
export async function safeReadProjectManifestOnly (projectDir: string): Promise<ProjectManifest | null> {
try {