From 94a447de150604e6a39f3af57b9f1da7c785806d Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 5 Dec 2023 10:38:46 +0100 Subject: [PATCH] fix: safely read json --- node/npm_dependencies.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/node/npm_dependencies.ts b/node/npm_dependencies.ts index 2844d396..19ea8a56 100644 --- a/node/npm_dependencies.ts +++ b/node/npm_dependencies.ts @@ -86,6 +86,13 @@ const safelyDetectTypes = async (packageJsonPath: string): Promise { + try { + const contents = await fs.readFile(filePath, { encoding: 'utf8' }) + return JSON.parse(contents) + } catch {} +} + // Workaround for https://github.com/evanw/esbuild/issues/1921. const banner = { js: ` @@ -198,13 +205,12 @@ const getNPMSpecifiers = async ({ basePath, functions, importMap, environment, r // dependencies. Because we'll bundle all modules in a subsequent step, // any transitive dependencies will be handled then. if (isDirectDependency) { - const packJson = JSON.parse(await fs.readFile(path.join(basePath, filePath), { encoding: 'utf-8' })) - - const subpaths = packJson.exports ? Object.keys(packJson.exports) : ['.'] + const packJson = await tryReadJson(path.join(basePath, filePath)) + const relativeExports = packJson?.exports ? Object.keys(packJson.exports) : ['.'] // eslint-disable-next-line max-depth - for (const subpath of subpaths) { + for (const relativeExport of relativeExports) { npmSpecifiers.push({ - specifier: path.join(packageName, subpath), + specifier: path.join(packageName, relativeExport), types: environment === 'development' ? await safelyDetectTypes(path.join(basePath, filePath)) : undefined, }) }