From 49cc1672ff70c0d859dcc785766a02ba97a87523 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Fri, 14 Feb 2025 07:23:51 +0800 Subject: [PATCH] feat(init): add `init-package-manager` configuration (#9087) --------- Co-authored-by: Zoltan Kochan --- .changeset/flat-pillows-attend.md | 6 ++++++ config/config/src/Config.ts | 1 + config/config/src/index.ts | 1 + packages/plugin-commands-init/package.json | 1 + packages/plugin-commands-init/src/init.ts | 11 +++++++---- packages/plugin-commands-init/test/init.test.ts | 17 +++++++++++++++++ packages/plugin-commands-init/tsconfig.json | 3 +++ pnpm-lock.yaml | 3 +++ 8 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 .changeset/flat-pillows-attend.md diff --git a/.changeset/flat-pillows-attend.md b/.changeset/flat-pillows-attend.md new file mode 100644 index 0000000000..70c2c3a66e --- /dev/null +++ b/.changeset/flat-pillows-attend.md @@ -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). diff --git a/config/config/src/Config.ts b/config/config/src/Config.ts index bce55324ab..4d39e3681c 100644 --- a/config/config/src/Config.ts +++ b/config/config/src/Config.ts @@ -214,6 +214,7 @@ export interface Config { strictStorePkgContentCheck: boolean managePackageManagerVersions: boolean strictDepBuilds: boolean + initPackageManager: boolean } export interface ConfigWithDeprecatedSettings extends Config { diff --git a/config/config/src/index.ts b/config/config/src/index.ts index f442f29b9a..7e6cec83cd 100644 --- a/config/config/src/index.ts +++ b/config/config/src/index.ts @@ -148,6 +148,7 @@ export async function getConfig (opts: { 'ignore-workspace-cycles': false, 'ignore-workspace-root-check': false, 'optimistic-repeat-install': false, + 'init-package-manager': true, 'inject-workspace-packages': false, 'link-workspace-packages': false, 'lockfile-include-tarball-url': false, diff --git a/packages/plugin-commands-init/package.json b/packages/plugin-commands-init/package.json index 7af5550570..0058fe7966 100644 --- a/packages/plugin-commands-init/package.json +++ b/packages/plugin-commands-init/package.json @@ -41,6 +41,7 @@ "@pnpm/cli-utils": "workspace:*", "@pnpm/config": "workspace:*", "@pnpm/error": "workspace:*", + "@pnpm/types": "workspace:*", "@pnpm/write-project-manifest": "workspace:*", "camelcase-keys": "catalog:", "render-help": "catalog:" diff --git a/packages/plugin-commands-init/src/init.ts b/packages/plugin-commands-init/src/init.ts index 5d2fc76bd4..b1fce23ac2 100644 --- a/packages/plugin-commands-init/src/init.ts +++ b/packages/plugin-commands-init/src/init.ts @@ -2,8 +2,9 @@ import fs from 'fs' import path from 'path' import { docsUrl } from '@pnpm/cli-utils' 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 { type ProjectManifest } from '@pnpm/types' import { writeProjectManifest } from '@pnpm/write-project-manifest' import renderHelp from 'render-help' import { parseRawConfig } from './utils' @@ -26,7 +27,7 @@ export function help (): string { } export async function handler ( - opts: Pick & { cliOptions: CliOptions }, + opts: Pick & Pick & Partial>, params?: string[] ): Promise { if (params?.length) { @@ -41,12 +42,11 @@ export async function handler ( if (fs.existsSync(manifestPath)) { throw new PnpmError('PACKAGE_JSON_EXISTS', 'package.json already exists') } - const manifest = { + const manifest: ProjectManifest = { name: path.basename(process.cwd()), version: '1.0.0', description: '', main: 'index.js', - packageManager: `pnpm@${packageManager.version}`, scripts: { test: 'echo "Error: no test specified" && exit 1', }, @@ -56,6 +56,9 @@ export async function handler ( } const config = await parseRawConfig(opts.rawConfig) const packageJson = { ...manifest, ...config } + if (opts.initPackageManager) { + packageJson.packageManager = `pnpm@${packageManager.version}` + } await writeProjectManifest(manifestPath, packageJson, { indent: 2, }) diff --git a/packages/plugin-commands-init/test/init.test.ts b/packages/plugin-commands-init/test/init.test.ts index f710130701..3e4817181e 100644 --- a/packages/plugin-commands-init/test/init.test.ts +++ b/packages/plugin-commands-init/test/init.test.ts @@ -2,6 +2,7 @@ import path from 'path' import fs from 'fs' import { init } from '@pnpm/plugin-commands-init' import { prepare, prepareEmpty } from '@pnpm/prepare' +import { type ProjectManifest } from '@pnpm/types' import { sync as loadJsonFile } from 'load-json-file' 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')) 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(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(path.resolve('package.json')) + expect(manifest).toBeTruthy() + expect(manifest.packageManager).toBeFalsy() +}) diff --git a/packages/plugin-commands-init/tsconfig.json b/packages/plugin-commands-init/tsconfig.json index 7fb987fe16..ae76662897 100644 --- a/packages/plugin-commands-init/tsconfig.json +++ b/packages/plugin-commands-init/tsconfig.json @@ -29,6 +29,9 @@ }, { "path": "../error" + }, + { + "path": "../types" } ] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index efe01368a4..d80c743dba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3952,6 +3952,9 @@ importers: '@pnpm/error': specifier: workspace:* version: link:../error + '@pnpm/types': + specifier: workspace:* + version: link:../types '@pnpm/write-project-manifest': specifier: workspace:* version: link:../../pkg-manifest/write-project-manifest