Skip to content

Commit

Permalink
jsonc-parser doesn't throw
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed May 18, 2021
1 parent 18ce3ba commit 01dce70
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/schematics/ng-add.ts
Expand Up @@ -16,17 +16,15 @@ function getWorkspace(
if (configBuffer === null) {
throw new SchematicsException(`Could not find angular.json`);
}
const content = configBuffer.toString();

const { parse } = (require('jsonc-parser') as typeof import('jsonc-parser'));
// We can not depend on this library to have be included in older (or newer) Angular versions.
// Require here, since the schematic will add it to the package.json and install it before
// continuing.
const { parse }: typeof import('jsonc-parser') = require('jsonc-parser');

let workspace: Workspace;
try {
workspace = parse(
content,
) as Workspace;
} catch (e) {
throw new SchematicsException(`Could not parse angular.json: ` + e.message);
const workspace = parse(configBuffer.toString()) as Workspace|undefined;
if (!workspace) {
throw new SchematicsException('Could not parse angular.json');
}

return {
Expand Down

0 comments on commit 01dce70

Please sign in to comment.