feat(read-project-manifest): safeReadProjectManifestOnly added

This commit is contained in:
Zoltan Kochan
2020-10-03 18:12:42 +03:00
parent 3097f2e167
commit 2762781ccf
3 changed files with 18 additions and 13 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/read-project-manifest": minor
---
safeReadProjectManifestOnly() added.

View File

@@ -2,7 +2,7 @@ import PnpmError from '@pnpm/error'
import binify, { Command } from '@pnpm/package-bins'
import readModulesDir from '@pnpm/read-modules-dir'
import { fromDir as readPackageJsonFromDir } from '@pnpm/read-package-json'
import { readProjectManifestOnly } from '@pnpm/read-project-manifest'
import { safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
import { DependencyManifest } from '@pnpm/types'
import Module = require('module')
import cmdShim = require('@zkochan/cmd-shim')
@@ -125,7 +125,7 @@ async function getPackageBins (
target: string
) {
const manifest = opts.allowExoticManifests
? await safeReadProjectManifestOnly(target) : await safeReadPkgJson(target)
? (await safeReadProjectManifestOnly(target) as DependencyManifest) : await safeReadPkgJson(target)
if (!manifest) {
// There's a directory in node_modules without package.json: ${target}.
@@ -185,14 +185,3 @@ async function safeReadPkgJson (pkgDir: string): Promise<DependencyManifest | nu
throw err
}
}
async function safeReadProjectManifestOnly (projectDir: string) {
try {
return await readProjectManifestOnly(projectDir) as DependencyManifest
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND') {
return null
}
throw err
}
}

View File

@@ -19,6 +19,17 @@ const stat = promisify(fs.stat)
type WriteProjectManifest = (manifest: ProjectManifest, force?: boolean) => Promise<void>
export async function safeReadProjectManifestOnly (projectDir: string) {
try {
return await readProjectManifestOnly(projectDir)
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND') {
return null
}
throw err
}
}
export default async function readProjectManifest (projectDir: string): Promise<{
fileName: string
manifest: ProjectManifest