Skip to content

Commit

Permalink
feat(schematics): allow comments in angular.json file
Browse files Browse the repository at this point in the history
ISSUES CLOSED: scullyio#480
  • Loading branch information
atao60 committed May 6, 2020
1 parent f31b24f commit a0577e5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
19 changes: 19 additions & 0 deletions schematics/scully/src/scully/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports.config = {
routes: { }
};
`;
const ANGULAR_CONF_PATH = '/angular.json';

const defaultOptions = Object.freeze({
project: 'defaultProject',
Expand Down Expand Up @@ -84,4 +85,22 @@ describe('scully schematic', () => {
expect(getFileContent(appTree, SCULLY_PATH)).toEqual('foo');
});
});

describe('when the angular.json contains any comment', () => {
it('should deal with it', async () => {
const options = {...defaultOptions};
const angularConfigContent = getFileContent(appTree, ANGULAR_CONF_PATH);
const angularConfigLines = angularConfigContent.split('\n');
angularConfigLines[3] += ` // dummy comment`;
appTree.overwrite(ANGULAR_CONF_PATH, angularConfigLines.join('\n'));
const NO_ERROR = '';
let error = NO_ERROR;
try {
await customRunner.runSchematicAsync('scully', options, appTree).toPromise();
} catch (e) {
error = e;
}
expect(error).toEqual(NO_ERROR);
});
});
});
31 changes: 25 additions & 6 deletions schematics/scully/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {normalize, strings} from '@angular-devkit/core';
import {normalize, strings, parseJson, JsonParseMode} from '@angular-devkit/core';
import {
apply,
forEach,
Expand Down Expand Up @@ -163,8 +163,18 @@ function getProjectProperty(
}, projectConfig);
}

export function parseJsonObject(jsonContent: string): {[prop: string]: any} {
return JSON.parse(jsonContent);
/** Parser of json content
* By default allow Json5 syntax, eg comments, trailing commas, ..., ie the same
* thing that the Angular json parser itself.
*
* !!! You should always replace JSON.parse by this function !!!
*/
export function parseJsonObject(jsonContent: string, mode = JsonParseMode.Loose): {[prop: string]: any} {
const result = parseJson(jsonContent, mode);
if (result === null || typeof result !== 'object' || Array.isArray(result)) {
throw new Error('Json content is not an object');
}
return result;
}

class FileNotFoundException extends Error {
Expand All @@ -174,14 +184,18 @@ class FileNotFoundException extends Error {
}
}

export const getJsonFile = <T>(tree: Tree, path: string): T => {
/** Parser of json file
*
* By default allow only strict json syntax
*
*/
export const getJsonFile = <T>(tree: Tree, path: string, mode = JsonParseMode.Json): T => {
const file = tree.get(path);
if (!file) {
throw new FileNotFoundException(path);
}
try {
const content = parseJsonObject(file.content.toString());

const content = parseJsonObject(file.content.toString(), mode);
return content as T;
} catch (e) {
throw new SchematicsException(`File ${path} could not be parsed!`);
Expand All @@ -193,6 +207,11 @@ export const getFileContents = (tree: Tree, filePath: string): string => {
return buffer.toString();
};

/** Parser of package.json file
*
* Allow only strict json content
*
*/
export const getPackageJson = (tree: Tree, packagejsonPath = DEFAULT_PACKAGE_JSON_PATH): PackageJson => {
return getJsonFile(tree, packagejsonPath);
};
Expand Down

0 comments on commit a0577e5

Please sign in to comment.