diff --git a/packages/cli/src/commands/addPlatform/addPlatform.ts b/packages/cli/src/commands/addPlatform/addPlatform.ts index c3c9fdbe6..a296fbfad 100644 --- a/packages/cli/src/commands/addPlatform/addPlatform.ts +++ b/packages/cli/src/commands/addPlatform/addPlatform.ts @@ -43,13 +43,23 @@ const getAppName = async (root: string) => { throw new CLIError(`Unable to find package.json inside ${root}`); } - let {name} = JSON.parse(readFileSync(pkgJsonPath, 'utf8')); + let name; + + try { + name = JSON.parse(readFileSync(pkgJsonPath, 'utf8')); + } catch (e) { + throw new CLIError(`Failed to read ${pkgJsonPath} file.`, e as Error); + } if (!name) { const appJson = join(root, 'app.json'); if (appJson) { logger.log(`Reading ${chalk.cyan('name')} from app.json…`); - name = JSON.parse(readFileSync(appJson, 'utf8')).name; + try { + name = JSON.parse(readFileSync(appJson, 'utf8')).name; + } catch (e) { + throw new CLIError(`Failed to read ${pkgJsonPath} file.`, e as Error); + } } if (!name) { diff --git a/packages/cli/src/commands/init/editTemplate.ts b/packages/cli/src/commands/init/editTemplate.ts index e90d4f688..9692a4387 100644 --- a/packages/cli/src/commands/init/editTemplate.ts +++ b/packages/cli/src/commands/init/editTemplate.ts @@ -278,9 +278,17 @@ export function getTemplateName(cwd: string) { // We use package manager to infer the name of the template module for us. // That's why we get it from temporary package.json, where the name is the // first and only dependency (hence 0). - const name = Object.keys( - JSON.parse(fs.readFileSync(path.join(cwd, './package.json'), 'utf8')) - .dependencies, - )[0]; + let name; + try { + name = Object.keys( + JSON.parse(fs.readFileSync(path.join(cwd, './package.json'), 'utf8')) + .dependencies, + )[0]; + } catch { + throw new CLIError( + 'Failed to read template name from package.json. Please make sure that the template you are using has a valid package.json file.', + ); + } + return name; }