fix(install): exit with an error when no package.json is found (#4614)

close #4609
This commit is contained in:
Zoltan Kochan
2022-04-24 13:03:12 +03:00
committed by GitHub
parent c5caf83346
commit 84c7e15bae
3 changed files with 34 additions and 2 deletions

View File

@@ -0,0 +1,6 @@
---
"pnpm": major
"@pnpm/plugin-commands-installation": patch
---
Exit with an error when running `pnpm install` in a directory that has no `package.json` file in it (and in parent directories) [#4609](https://github.com/pnpm/pnpm/issues/4609).

View File

@@ -168,8 +168,8 @@ when running add/update with the --workspace option')
let { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.dir, opts)
if (manifest === null) {
if (opts.update) {
throw new PnpmError('NO_IMPORTER_MANIFEST', 'No package.json found')
if (opts.update === true || params.length === 0) {
throw new PnpmError('NO_PKG_MANIFEST', `No package.json found in ${opts.dir}`)
}
manifest = {}
}

View File

@@ -0,0 +1,26 @@
import path from 'path'
import { add, install } from '@pnpm/plugin-commands-installation'
import { prepareEmpty } from '@pnpm/prepare'
import { DEFAULT_OPTS } from './utils'
test('install fails if no package.json is found', async () => {
prepareEmpty()
await expect(install.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
})).rejects.toThrow(/No package\.json found/)
})
test('install does not fail when a new package is added', async () => {
prepareEmpty()
await add.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, ['is-positive@1.0.0'])
const pkg = await import(path.resolve('package.json'))
expect(pkg?.dependencies).toStrictEqual({ 'is-positive': '1.0.0' })
})