feat(plugin-commands-init): add support for --init-type flag in pnpm init (#9463)

* Added .npmrc support for init-type

* feat: init-type setting

close #9416

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
cezary-klauza
2025-05-04 03:38:38 +02:00
committed by GitHub
parent df8df8a0c1
commit b282bd1538
8 changed files with 34 additions and 3 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/plugin-commands-init": minor
"@pnpm/config": minor
"pnpm": minor
---
A new setting added for `pnpm init` to create a `package.json` with `type=module`, when `init-type` is `module`. Works as a flag for the init command too [#9463](https://github.com/pnpm/pnpm/pull/9463).

View File

@@ -218,6 +218,7 @@ export interface Config extends OptionsFromRootManifest {
strictDepBuilds: boolean
syncInjectedDepsAfterScripts?: string[]
initPackageManager: boolean
initType: 'commonjs' | 'module'
dangerouslyAllowAllBuilds: boolean
}

View File

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

View File

@@ -46,6 +46,7 @@ export const types = Object.assign({
'optimistic-repeat-install': Boolean,
'include-workspace-root': Boolean,
'init-package-manager': Boolean,
'init-type': ['commonjs', 'module'],
'inject-workspace-packages': Boolean,
'legacy-dir-filtering': Boolean,
'link-workspace-packages': [Boolean, 'deep'],

View File

@@ -40,12 +40,14 @@
"@pnpm/types": "workspace:*",
"@pnpm/write-project-manifest": "workspace:*",
"camelcase-keys": "catalog:",
"ramda": "catalog:",
"render-help": "catalog:"
},
"devDependencies": {
"@pnpm/plugin-commands-init": "workspace:*",
"@pnpm/prepare": "workspace:*",
"@pnpm/test-fixtures": "workspace:*",
"@types/ramda": "catalog:",
"load-json-file": "catalog:"
},
"engines": {

View File

@@ -2,18 +2,19 @@ import fs from 'fs'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { packageManager } from '@pnpm/cli-meta'
import { type Config, type UniversalOptions } from '@pnpm/config'
import { types as allTypes, type Config, type UniversalOptions } from '@pnpm/config'
import { PnpmError } from '@pnpm/error'
import { sortKeysByPriority } from '@pnpm/object.key-sorting'
import { type ProjectManifest } from '@pnpm/types'
import { writeProjectManifest } from '@pnpm/write-project-manifest'
import pick from 'ramda/src/pick'
import renderHelp from 'render-help'
import { parseRawConfig } from './utils'
export const rcOptionsTypes = cliOptionsTypes
export function cliOptionsTypes (): Record<string, unknown> {
return {}
return pick(['init-type', 'init-package-manager'], allTypes)
}
export const commandNames = ['init']
@@ -28,7 +29,7 @@ export function help (): string {
}
export async function handler (
opts: Pick<UniversalOptions, 'rawConfig'> & Pick<Config, 'cliOptions'> & Partial<Pick<Config, 'initPackageManager'>>,
opts: Pick<UniversalOptions, 'rawConfig'> & Pick<Config, 'cliOptions'> & Partial<Pick<Config, 'initPackageManager' | 'initType'>>,
params?: string[]
): Promise<string> {
if (params?.length) {
@@ -55,6 +56,11 @@ export async function handler (
author: '',
license: 'ISC',
}
if (opts.initType === 'module') {
manifest.type = opts.initType
}
const config = await parseRawConfig(opts.rawConfig)
const packageJson = { ...manifest, ...config }
if (opts.initPackageManager) {

View File

@@ -84,3 +84,10 @@ test('init a new package.json with init-package-manager=false', async () => {
expect(manifest).toBeTruthy()
expect(manifest).not.toHaveProperty('packageManager')
})
test('init a new package.json with init-type=module', async () => {
prepareEmpty()
await init.handler({ rawConfig: { 'init-type': 'module' }, cliOptions: {}, initType: 'module' })
const manifest = loadJsonFile<ProjectManifest>(path.resolve('package.json'))
expect(manifest.type).toEqual('module')
})

6
pnpm-lock.yaml generated
View File

@@ -4162,6 +4162,9 @@ importers:
camelcase-keys:
specifier: 'catalog:'
version: 6.2.2
ramda:
specifier: 'catalog:'
version: '@pnpm/ramda@0.28.1'
render-help:
specifier: 'catalog:'
version: 1.0.3
@@ -4175,6 +4178,9 @@ importers:
'@pnpm/test-fixtures':
specifier: workspace:*
version: link:../../__utils__/test-fixtures
'@types/ramda':
specifier: 'catalog:'
version: 0.29.12
load-json-file:
specifier: 'catalog:'
version: 6.2.0