feat(init): --bare (#10228)

* feat(init): fields preset

* feat: replace `init-preset` with `init-bare`

* feat: remove init-bare

close #10226

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
Khải
2025-11-29 20:55:02 +07:00
committed by GitHub
parent 7730a7f25c
commit 3aa50c8365
3 changed files with 56 additions and 17 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-init": minor
"pnpm": minor
---
Added a new flag called `--bare` to `pnpm init` for creating a package.json with the bare minimum of required fields [#10226](https://github.com/pnpm/pnpm/issues/10226).

View File

@@ -14,7 +14,13 @@ import { parseRawConfig } from './utils.js'
export const rcOptionsTypes = cliOptionsTypes
export function cliOptionsTypes (): Record<string, unknown> {
return pick(['init-type', 'init-package-manager'], allTypes)
return {
...pick([
'init-package-manager',
'init-type',
], allTypes),
bare: Boolean,
}
}
export const commandNames = ['init']
@@ -34,6 +40,10 @@ export function help (): string {
description: 'Pin the project to the current pnpm version by adding a "packageManager" field to package.json',
name: '--init-package-manager',
},
{
description: 'Create a package.json file with the bare minimum of required fields',
name: '--bare',
},
],
},
],
@@ -42,10 +52,17 @@ export function help (): string {
})
}
export async function handler (
opts: Pick<UniversalOptions, 'rawConfig'> & Pick<Config, 'cliOptions'> & Partial<Pick<Config, 'initPackageManager' | 'initType'>>,
params?: string[]
): Promise<string> {
export type InitOptions =
& Pick<UniversalOptions, 'rawConfig'>
& Pick<Config, 'cliOptions'>
& Partial<Pick<Config,
| 'initPackageManager'
| 'initType'
>> & {
bare?: boolean
}
export async function handler (opts: InitOptions, params?: string[]): Promise<string> {
if (params?.length) {
throw new PnpmError('INIT_ARG', 'init command does not accept any arguments', {
hint: `Maybe you wanted to run "pnpm create ${params.join(' ')}"`,
@@ -58,18 +75,20 @@ export async function handler (
if (fs.existsSync(manifestPath)) {
throw new PnpmError('PACKAGE_JSON_EXISTS', 'package.json already exists')
}
const manifest: ProjectManifest = {
name: path.basename(process.cwd()),
version: '1.0.0',
description: '',
main: 'index.js',
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
keywords: [],
author: '',
license: 'ISC',
}
const manifest: ProjectManifest = opts.bare
? {}
: {
name: path.basename(process.cwd()),
version: '1.0.0',
description: '',
main: 'index.js',
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
keywords: [],
author: '',
license: 'ISC',
}
if (opts.initType === 'module') {
manifest.type = opts.initType
@@ -83,6 +102,7 @@ export async function handler (
const priority = Object.fromEntries([
'name',
'version',
'private',
'description',
'main',
'scripts',

View File

@@ -91,3 +91,16 @@ test('init a new package.json with init-type=module', async () => {
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest.type).toBe('module')
})
test('init a new package.json with --bare', async () => {
prepareEmpty()
await init.handler({ rawConfig: {}, cliOptions: {}, bare: true })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest).not.toHaveProperty(['name'])
expect(manifest).not.toHaveProperty(['version'])
expect(manifest).not.toHaveProperty(['description'])
expect(manifest).not.toHaveProperty(['main'])
expect(manifest).not.toHaveProperty(['keywords'])
expect(manifest).not.toHaveProperty(['author'])
expect(manifest).not.toHaveProperty(['license'])
})