From 17fa76e2bbb64985c42462ac2013756cf52d66f0 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sun, 24 Apr 2022 04:24:53 +0300 Subject: [PATCH] fix(init): don't fail if a parent dir has a package.json (#4615) close #4589 --- .changeset/short-coats-accept.md | 5 +++++ packages/plugin-commands-init/src/init.ts | 10 ++++++---- packages/plugin-commands-init/test/init.test.ts | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 .changeset/short-coats-accept.md diff --git a/.changeset/short-coats-accept.md b/.changeset/short-coats-accept.md new file mode 100644 index 0000000000..44b3427877 --- /dev/null +++ b/.changeset/short-coats-accept.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-init": patch +--- + +`pnpm init` should not fail if one of the parent directories contains a `package.json` file [#4589](https://github.com/pnpm/pnpm/issues/4589). diff --git a/packages/plugin-commands-init/src/init.ts b/packages/plugin-commands-init/src/init.ts index bd39770017..96b506cea4 100644 --- a/packages/plugin-commands-init/src/init.ts +++ b/packages/plugin-commands-init/src/init.ts @@ -25,14 +25,16 @@ export function help () { } export async function handler ( - opts: Pick + opts: Pick ) { - const manifestPath = path.join(opts.dir, 'package.json') + // Using cwd instead of the dir option because the dir option + // is set to the first parent directory that has a package.json file. + const manifestPath = path.join(process.cwd(), 'package.json') if (fs.existsSync(manifestPath)) { throw new PnpmError('PACKAGE_JSON_EXISTS', 'package.json already exists') } const manifest = { - name: path.basename(opts.dir), + name: path.basename(process.cwd()), version: '1.0.0', description: '', main: 'index.js', @@ -48,7 +50,7 @@ export async function handler ( await writeProjectManifest(manifestPath, packageJson, { indent: 2, }) - return `Wrote to ${path.join(opts.dir, 'package.json')} + return `Wrote to ${manifestPath} ${JSON.stringify(packageJson, null, 2)}` } diff --git a/packages/plugin-commands-init/test/init.test.ts b/packages/plugin-commands-init/test/init.test.ts index 3af56800a2..3770ba6d12 100644 --- a/packages/plugin-commands-init/test/init.test.ts +++ b/packages/plugin-commands-init/test/init.test.ts @@ -5,7 +5,7 @@ import { sync as loadJsonFile } from 'load-json-file' test('init a new package.json', async () => { prepareEmpty() - await init.handler({ dir: process.cwd(), rawConfig: {} }) + await init.handler({ rawConfig: {} }) const manifest = loadJsonFile(path.resolve('package.json')) expect(manifest).toBeTruthy() }) @@ -14,7 +14,7 @@ test('throws an error if a package.json exists in the current directory', async prepare({}) await expect( - init.handler({ dir: process.cwd(), rawConfig: {} }) + init.handler({ rawConfig: {} }) ).rejects.toThrow('package.json already exists') }) @@ -27,7 +27,7 @@ test('init a new package.json with npmrc', async () => { 'init-version': '2.0.0', } prepareEmpty() - await init.handler({ dir: process.cwd(), rawConfig }) + await init.handler({ rawConfig }) const manifest: Record = loadJsonFile(path.resolve('package.json')) const expectAuthor = `${rawConfig['init-author-name']} <${rawConfig['init-author-email']}> (${rawConfig['init-author-url']})` expect(manifest.version).toBe(rawConfig['init-version'])