Skip to content

Commit

Permalink
Use JSON.parse to parse package.json file in v2 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Aug 5, 2020
1 parent 1d233a4 commit af8c26e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/apollo-angular/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### vNext

### v2.0.3

- Use JSON.parse to parse `package.json` file in v2 migration

### v2.0.2

- Fix duplicated imports in v2 migration
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-angular/schematics/migrations/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function (): Rule {
function migrateDependencies() {
return (tree: Tree, context: SchematicContext) => {
const packageJsonPath = 'package.json';
const packageJson = getJsonFile(tree, packageJsonPath);
const packageJson = getJsonFile(tree, packageJsonPath, true);

packageJson.dependencies = packageJson.dependencies || {};

Expand Down
5 changes: 4 additions & 1 deletion packages/apollo-angular/schematics/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ export function parseJSON<T = any>(content: string): T {
* @param host {Tree} The source tree.
* @param path {String} The path to the file to read. Relative to the root of the tree.
*/
export function getJsonFile(host: Tree, path: string) {
export function getJsonFile(host: Tree, path: string, strict = false) {
const buffer = host.read(path);
if (buffer === null) {
throw new SchematicsException(`Couldn't read ${path}!`);
}

const content = buffer.toString('utf-8');
try {
if (strict) {
return JSON.parse(content);
}
// remove comments :)
return parseJSON(content);
} catch (e) {
Expand Down

0 comments on commit af8c26e

Please sign in to comment.