diff --git a/.changeset/brave-ties-move.md b/.changeset/brave-ties-move.md new file mode 100644 index 0000000000..3034b59fe5 --- /dev/null +++ b/.changeset/brave-ties-move.md @@ -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). diff --git a/packages/plugin-commands-init/src/init.ts b/packages/plugin-commands-init/src/init.ts index 205a41d056..54f68df51d 100644 --- a/packages/plugin-commands-init/src/init.ts +++ b/packages/plugin-commands-init/src/init.ts @@ -14,7 +14,13 @@ import { parseRawConfig } from './utils.js' export const rcOptionsTypes = cliOptionsTypes export function cliOptionsTypes (): Record { - 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 & Pick & Partial>, - params?: string[] -): Promise { +export type InitOptions = + & Pick + & Pick + & Partial> & { + bare?: boolean + } + +export async function handler (opts: InitOptions, params?: string[]): Promise { 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', diff --git a/packages/plugin-commands-init/test/init.test.ts b/packages/plugin-commands-init/test/init.test.ts index ef07a8444a..e4cd29e82f 100644 --- a/packages/plugin-commands-init/test/init.test.ts +++ b/packages/plugin-commands-init/test/init.test.ts @@ -91,3 +91,16 @@ test('init a new package.json with init-type=module', async () => { const manifest = loadJsonFile(path.resolve('package.json')) expect(manifest.type).toEqual('module') }) + +test('init a new package.json with --bare', async () => { + prepareEmpty() + await init.handler({ rawConfig: {}, cliOptions: {}, bare: true }) + const manifest = loadJsonFileSync(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']) +})