Skip to content

Commit

Permalink
fix(nx): handle reading json with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Jun 15, 2019
1 parent 7ddc75f commit 1748ac9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/workspace/src/utils/ast-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ describe('readJsonInTree', () => {
});
});

it('should handle json files with comments', () => {
tree.create(
'data.json',
`{
// data: 'data'
}`
);
expect(readJsonInTree(tree, 'data.json')).toEqual({});
});

it('should throw an error if the file does not exist', () => {
expect(() => readJsonInTree(tree, 'data.json')).toThrow(
'Cannot find data.json'
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import { Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
import * as ts from 'typescript';
import * as stripJsonComments from 'strip-json-comments';
import { serializeJson } from './fileutils';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';

Expand Down Expand Up @@ -361,7 +362,7 @@ export function readJsonInTree<T = any>(host: Tree, path: string): T {
if (!host.exists(path)) {
throw new Error(`Cannot find ${path}`);
}
const contents = host.read(path)!.toString('utf-8');
const contents = stripJsonComments(host.read(path)!.toString('utf-8'));
try {
return JSON.parse(contents);
} catch (e) {
Expand Down

0 comments on commit 1748ac9

Please sign in to comment.