Skip to content

Commit 4827e7f

Browse files
committed
chore: add try-catch blocks
1 parent 428d3bd commit 4827e7f

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

packages/cli/src/commands/addPlatform/addPlatform.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,23 @@ const getAppName = async (root: string) => {
4343
throw new CLIError(`Unable to find package.json inside ${root}`);
4444
}
4545

46-
let {name} = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
46+
let name;
47+
48+
try {
49+
name = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
50+
} catch (e) {
51+
throw new CLIError(`Failed to read ${pkgJsonPath} file.`, e as Error);
52+
}
4753

4854
if (!name) {
4955
const appJson = join(root, 'app.json');
5056
if (appJson) {
5157
logger.log(`Reading ${chalk.cyan('name')} from app.json…`);
52-
name = JSON.parse(readFileSync(appJson, 'utf8')).name;
58+
try {
59+
name = JSON.parse(readFileSync(appJson, 'utf8')).name;
60+
} catch (e) {
61+
throw new CLIError(`Failed to read ${pkgJsonPath} file.`, e as Error);
62+
}
5363
}
5464

5565
if (!name) {

packages/cli/src/commands/init/editTemplate.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,17 @@ export function getTemplateName(cwd: string) {
278278
// We use package manager to infer the name of the template module for us.
279279
// That's why we get it from temporary package.json, where the name is the
280280
// first and only dependency (hence 0).
281-
const name = Object.keys(
282-
JSON.parse(fs.readFileSync(path.join(cwd, './package.json'), 'utf8'))
283-
.dependencies,
284-
)[0];
281+
let name;
282+
try {
283+
name = Object.keys(
284+
JSON.parse(fs.readFileSync(path.join(cwd, './package.json'), 'utf8'))
285+
.dependencies,
286+
)[0];
287+
} catch {
288+
throw new CLIError(
289+
'Failed to read template name from package.json. Please make sure that the template you are using has a valid package.json file.',
290+
);
291+
}
292+
285293
return name;
286294
}

0 commit comments

Comments
 (0)