Skip to content

Commit

Permalink
fix: don't use deprecated JSON parser
Browse files Browse the repository at this point in the history
  • Loading branch information
fmalcher committed Nov 5, 2021
1 parent 98f291e commit f6eb6c6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/ng-add.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { JsonParseMode, parseJson } from '@angular-devkit/core';
import { ParseError, parse, printParseErrorCode } from 'jsonc-parser';
import { JsonValue } from '@angular-devkit/core';
import {
SchematicContext,
SchematicsException,
Tree
} from '@angular-devkit/schematics';
import { Workspace } from './interfaces';

export function parseJsonFile(json: string): JsonValue {
const errors: ParseError[] = [];
const content = parse(json, errors, { allowTrailingComma: true });

if (errors.length) {
const { error, offset } = errors[0];
throw new Error(
`Failed to parse as JSON AST Object. ${printParseErrorCode(
error,
)} at location: ${offset}.`,
);
}

return content;
}


function getWorkspace(host: Tree): { path: string; workspace: Workspace } {
const possibleFiles = ['/angular.json', '/.angular.json'];
const path = possibleFiles.filter(path => host.exists(path))[0];
Expand All @@ -18,7 +36,7 @@ function getWorkspace(host: Tree): { path: string; workspace: Workspace } {

let workspace: Workspace;
try {
workspace = (parseJson(content, JsonParseMode.Loose) as {}) as Workspace;
workspace = (parseJsonFile(content) as {}) as Workspace;
} catch (e) {
throw new SchematicsException(`Could not parse angular.json: ` + e.message);
}
Expand Down
5 changes: 5 additions & 0 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"dependencies": {
"commander": "^3.0.0-0",
"fs-extra": "^9.0.1",
"gh-pages": "^3.1.0"
"gh-pages": "^3.1.0",
"jsonc-parser": "^3.0.0"
},
"husky": {
"hooks": {
Expand Down

0 comments on commit f6eb6c6

Please sign in to comment.