mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-27 18:46:18 -04:00
fix(init): don't fail if a parent dir has a package.json (#4615)
close #4589
This commit is contained in:
5
.changeset/short-coats-accept.md
Normal file
5
.changeset/short-coats-accept.md
Normal file
@@ -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).
|
||||
@@ -25,14 +25,16 @@ export function help () {
|
||||
}
|
||||
|
||||
export async function handler (
|
||||
opts: Pick<UniversalOptions, 'dir' | 'rawConfig'>
|
||||
opts: Pick<UniversalOptions, 'rawConfig'>
|
||||
) {
|
||||
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)}`
|
||||
}
|
||||
|
||||
@@ -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<string, string> = 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'])
|
||||
|
||||
Reference in New Issue
Block a user