mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-23 23:29:17 -05:00
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:
6
.changeset/brave-ties-move.md
Normal file
6
.changeset/brave-ties-move.md
Normal 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).
|
||||
@@ -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',
|
||||
|
||||
@@ -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'])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user