Skip to content

Commit 3aa50c8

Browse files
KSXGitHubzkochan
andauthored
feat(init): --bare (pnpm#10228)
* feat(init): fields preset * feat: replace `init-preset` with `init-bare` * feat: remove init-bare close pnpm#10226 --------- Co-authored-by: Zoltan Kochan <[email protected]>
1 parent 7730a7f commit 3aa50c8

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

.changeset/brave-ties-move.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pnpm/plugin-commands-init": minor
3+
"pnpm": minor
4+
---
5+
6+
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).

packages/plugin-commands-init/src/init.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import { parseRawConfig } from './utils.js'
1414
export const rcOptionsTypes = cliOptionsTypes
1515

1616
export function cliOptionsTypes (): Record<string, unknown> {
17-
return pick(['init-type', 'init-package-manager'], allTypes)
17+
return {
18+
...pick([
19+
'init-package-manager',
20+
'init-type',
21+
], allTypes),
22+
bare: Boolean,
23+
}
1824
}
1925

2026
export const commandNames = ['init']
@@ -34,6 +40,10 @@ export function help (): string {
3440
description: 'Pin the project to the current pnpm version by adding a "packageManager" field to package.json',
3541
name: '--init-package-manager',
3642
},
43+
{
44+
description: 'Create a package.json file with the bare minimum of required fields',
45+
name: '--bare',
46+
},
3747
],
3848
},
3949
],
@@ -42,10 +52,17 @@ export function help (): string {
4252
})
4353
}
4454

45-
export async function handler (
46-
opts: Pick<UniversalOptions, 'rawConfig'> & Pick<Config, 'cliOptions'> & Partial<Pick<Config, 'initPackageManager' | 'initType'>>,
47-
params?: string[]
48-
): Promise<string> {
55+
export type InitOptions =
56+
& Pick<UniversalOptions, 'rawConfig'>
57+
& Pick<Config, 'cliOptions'>
58+
& Partial<Pick<Config,
59+
| 'initPackageManager'
60+
| 'initType'
61+
>> & {
62+
bare?: boolean
63+
}
64+
65+
export async function handler (opts: InitOptions, params?: string[]): Promise<string> {
4966
if (params?.length) {
5067
throw new PnpmError('INIT_ARG', 'init command does not accept any arguments', {
5168
hint: `Maybe you wanted to run "pnpm create ${params.join(' ')}"`,
@@ -58,18 +75,20 @@ export async function handler (
5875
if (fs.existsSync(manifestPath)) {
5976
throw new PnpmError('PACKAGE_JSON_EXISTS', 'package.json already exists')
6077
}
61-
const manifest: ProjectManifest = {
62-
name: path.basename(process.cwd()),
63-
version: '1.0.0',
64-
description: '',
65-
main: 'index.js',
66-
scripts: {
67-
test: 'echo "Error: no test specified" && exit 1',
68-
},
69-
keywords: [],
70-
author: '',
71-
license: 'ISC',
72-
}
78+
const manifest: ProjectManifest = opts.bare
79+
? {}
80+
: {
81+
name: path.basename(process.cwd()),
82+
version: '1.0.0',
83+
description: '',
84+
main: 'index.js',
85+
scripts: {
86+
test: 'echo "Error: no test specified" && exit 1',
87+
},
88+
keywords: [],
89+
author: '',
90+
license: 'ISC',
91+
}
7392

7493
if (opts.initType === 'module') {
7594
manifest.type = opts.initType
@@ -83,6 +102,7 @@ export async function handler (
83102
const priority = Object.fromEntries([
84103
'name',
85104
'version',
105+
'private',
86106
'description',
87107
'main',
88108
'scripts',

packages/plugin-commands-init/test/init.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,16 @@ test('init a new package.json with init-type=module', async () => {
9191
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
9292
expect(manifest.type).toBe('module')
9393
})
94+
95+
test('init a new package.json with --bare', async () => {
96+
prepareEmpty()
97+
await init.handler({ rawConfig: {}, cliOptions: {}, bare: true })
98+
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
99+
expect(manifest).not.toHaveProperty(['name'])
100+
expect(manifest).not.toHaveProperty(['version'])
101+
expect(manifest).not.toHaveProperty(['description'])
102+
expect(manifest).not.toHaveProperty(['main'])
103+
expect(manifest).not.toHaveProperty(['keywords'])
104+
expect(manifest).not.toHaveProperty(['author'])
105+
expect(manifest).not.toHaveProperty(['license'])
106+
})

0 commit comments

Comments
 (0)