feat(init): add init-package-manager configuration (#9087)

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
btea
2025-02-14 07:23:51 +08:00
committed by GitHub
parent 7b9a9af38f
commit 49cc1672ff
8 changed files with 39 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-init": minor
"pnpm": minor
---
Add the `init-package-manager` configuration. When the value is set to `false`, `pnpm init` won't add the `packageManager` field to the initialized `package.json` file [#9087](https://github.com/pnpm/pnpm/pull/9087).

View File

@@ -214,6 +214,7 @@ export interface Config {
strictStorePkgContentCheck: boolean strictStorePkgContentCheck: boolean
managePackageManagerVersions: boolean managePackageManagerVersions: boolean
strictDepBuilds: boolean strictDepBuilds: boolean
initPackageManager: boolean
} }
export interface ConfigWithDeprecatedSettings extends Config { export interface ConfigWithDeprecatedSettings extends Config {

View File

@@ -148,6 +148,7 @@ export async function getConfig (opts: {
'ignore-workspace-cycles': false, 'ignore-workspace-cycles': false,
'ignore-workspace-root-check': false, 'ignore-workspace-root-check': false,
'optimistic-repeat-install': false, 'optimistic-repeat-install': false,
'init-package-manager': true,
'inject-workspace-packages': false, 'inject-workspace-packages': false,
'link-workspace-packages': false, 'link-workspace-packages': false,
'lockfile-include-tarball-url': false, 'lockfile-include-tarball-url': false,

View File

@@ -41,6 +41,7 @@
"@pnpm/cli-utils": "workspace:*", "@pnpm/cli-utils": "workspace:*",
"@pnpm/config": "workspace:*", "@pnpm/config": "workspace:*",
"@pnpm/error": "workspace:*", "@pnpm/error": "workspace:*",
"@pnpm/types": "workspace:*",
"@pnpm/write-project-manifest": "workspace:*", "@pnpm/write-project-manifest": "workspace:*",
"camelcase-keys": "catalog:", "camelcase-keys": "catalog:",
"render-help": "catalog:" "render-help": "catalog:"

View File

@@ -2,8 +2,9 @@ import fs from 'fs'
import path from 'path' import path from 'path'
import { docsUrl } from '@pnpm/cli-utils' import { docsUrl } from '@pnpm/cli-utils'
import { packageManager } from '@pnpm/cli-meta' import { packageManager } from '@pnpm/cli-meta'
import { type CliOptions, type UniversalOptions } from '@pnpm/config' import { type Config, type UniversalOptions } from '@pnpm/config'
import { PnpmError } from '@pnpm/error' import { PnpmError } from '@pnpm/error'
import { type ProjectManifest } from '@pnpm/types'
import { writeProjectManifest } from '@pnpm/write-project-manifest' import { writeProjectManifest } from '@pnpm/write-project-manifest'
import renderHelp from 'render-help' import renderHelp from 'render-help'
import { parseRawConfig } from './utils' import { parseRawConfig } from './utils'
@@ -26,7 +27,7 @@ export function help (): string {
} }
export async function handler ( export async function handler (
opts: Pick<UniversalOptions, 'rawConfig'> & { cliOptions: CliOptions }, opts: Pick<UniversalOptions, 'rawConfig'> & Pick<Config, 'cliOptions'> & Partial<Pick<Config, 'initPackageManager'>>,
params?: string[] params?: string[]
): Promise<string> { ): Promise<string> {
if (params?.length) { if (params?.length) {
@@ -41,12 +42,11 @@ export async function handler (
if (fs.existsSync(manifestPath)) { if (fs.existsSync(manifestPath)) {
throw new PnpmError('PACKAGE_JSON_EXISTS', 'package.json already exists') throw new PnpmError('PACKAGE_JSON_EXISTS', 'package.json already exists')
} }
const manifest = { const manifest: ProjectManifest = {
name: path.basename(process.cwd()), name: path.basename(process.cwd()),
version: '1.0.0', version: '1.0.0',
description: '', description: '',
main: 'index.js', main: 'index.js',
packageManager: `pnpm@${packageManager.version}`,
scripts: { scripts: {
test: 'echo "Error: no test specified" && exit 1', test: 'echo "Error: no test specified" && exit 1',
}, },
@@ -56,6 +56,9 @@ export async function handler (
} }
const config = await parseRawConfig(opts.rawConfig) const config = await parseRawConfig(opts.rawConfig)
const packageJson = { ...manifest, ...config } const packageJson = { ...manifest, ...config }
if (opts.initPackageManager) {
packageJson.packageManager = `pnpm@${packageManager.version}`
}
await writeProjectManifest(manifestPath, packageJson, { await writeProjectManifest(manifestPath, packageJson, {
indent: 2, indent: 2,
}) })

View File

@@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs' import fs from 'fs'
import { init } from '@pnpm/plugin-commands-init' import { init } from '@pnpm/plugin-commands-init'
import { prepare, prepareEmpty } from '@pnpm/prepare' import { prepare, prepareEmpty } from '@pnpm/prepare'
import { type ProjectManifest } from '@pnpm/types'
import { sync as loadJsonFile } from 'load-json-file' import { sync as loadJsonFile } from 'load-json-file'
test('init a new package.json', async () => { test('init a new package.json', async () => {
@@ -67,3 +68,19 @@ test('init a new package.json if a package.json exists in the current directory
const manifest = loadJsonFile(path.resolve('empty-dir2/package.json')) const manifest = loadJsonFile(path.resolve('empty-dir2/package.json'))
expect(manifest).toBeTruthy() expect(manifest).toBeTruthy()
}) })
test('init a new package.json with init-package-manager=true', async () => {
prepareEmpty()
await init.handler({ rawConfig: {}, cliOptions: {}, initPackageManager: true })
const manifest = loadJsonFile<ProjectManifest>(path.resolve('package.json'))
expect(manifest).toBeTruthy()
expect(manifest.packageManager).toBeTruthy()
})
test('init a new package.json with init-package-manager=false', async () => {
prepareEmpty()
await init.handler({ rawConfig: {}, cliOptions: {}, initPackageManager: false })
const manifest = loadJsonFile<ProjectManifest>(path.resolve('package.json'))
expect(manifest).toBeTruthy()
expect(manifest.packageManager).toBeFalsy()
})

View File

@@ -29,6 +29,9 @@
}, },
{ {
"path": "../error" "path": "../error"
},
{
"path": "../types"
} }
] ]
} }

3
pnpm-lock.yaml generated
View File

@@ -3952,6 +3952,9 @@ importers:
'@pnpm/error': '@pnpm/error':
specifier: workspace:* specifier: workspace:*
version: link:../error version: link:../error
'@pnpm/types':
specifier: workspace:*
version: link:../types
'@pnpm/write-project-manifest': '@pnpm/write-project-manifest':
specifier: workspace:* specifier: workspace:*
version: link:../../pkg-manifest/write-project-manifest version: link:../../pkg-manifest/write-project-manifest