Skip to content

Commit

Permalink
chore: add try-catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Feb 25, 2024
1 parent 53de944 commit ff9545e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
14 changes: 12 additions & 2 deletions packages/cli/src/commands/addPlatform/addPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 12 additions & 4 deletions packages/cli/src/commands/init/editTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit ff9545e

Please sign in to comment.