Skip to content

Commit b26d6ba

Browse files
alan-agius4vikerman
authored andcommitted
refactor: create readJsonFileAsAstObject utility
1 parent 4203294 commit b26d6ba

File tree

4 files changed

+21
-45
lines changed

4 files changed

+21
-45
lines changed

packages/schematics/angular/migrations/update-9/ivy-libraries.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { JsonParseMode, parseJsonAst } from '@angular-devkit/core';
98
import { Rule, Tree } from '@angular-devkit/schematics';
109
import { getWorkspacePath } from '../../utility/config';
1110
import {
@@ -14,7 +13,7 @@ import {
1413
insertPropertyInAstObjectInOrder,
1514
} from '../../utility/json-utils';
1615
import { Builders } from '../../utility/workspace-models';
17-
import { getTargets, getWorkspace } from './utils';
16+
import { getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
1817

1918
/**
2019
* Updates a pre version 9 library to version 9 Ivy library.
@@ -62,17 +61,7 @@ export function updateLibraries(): Rule {
6261
}
6362

6463
// tsConfig for production already exists.
65-
const tsConfigContent = tree.read(tsConfigOption.value);
66-
if (!tsConfigContent) {
67-
continue;
68-
}
69-
70-
const tsConfigAst = parseJsonAst(tsConfigContent.toString(), JsonParseMode.Loose);
71-
if (!tsConfigAst || tsConfigAst.kind !== 'object') {
72-
// Invalid tsConfig
73-
continue;
74-
}
75-
64+
const tsConfigAst = readJsonFileAsAstObject(tree, tsConfigOption.value);
7665
const tsConfigRecorder = tree.beginUpdate(tsConfigOption.value);
7766
const ngCompilerOptions = findPropertyInAstObject(tsConfigAst, 'angularCompilerOptions');
7867
if (!ngCompilerOptions) {

packages/schematics/angular/migrations/update-9/ngsw-config.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { JsonParseMode, parseJsonAst } from '@angular-devkit/core';
9-
import { Rule, SchematicsException, Tree } from '@angular-devkit/schematics';
8+
import { Rule, Tree } from '@angular-devkit/schematics';
109
import { appendValueInAstArray, findPropertyInAstObject } from '../../utility/json-utils';
1110
import { Builders } from '../../utility/workspace-models';
12-
import { getAllOptions, getTargets, getWorkspace } from './utils';
11+
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
1312

1413

1514
/**
@@ -27,13 +26,7 @@ export function updateNGSWConfig(): Rule {
2726
}
2827

2928
const path = ngswConfigPath.value;
30-
const configBuffer = tree.read(path);
31-
if (!configBuffer) {
32-
throw new SchematicsException(`Could not find (${path})`);
33-
}
34-
35-
const content = configBuffer.toString();
36-
const ngswConfigAst = parseJsonAst(content, JsonParseMode.Loose);
29+
const ngswConfigAst = readJsonFileAsAstObject(tree, path);
3730
if (!ngswConfigAst || ngswConfigAst.kind !== 'object') {
3831
continue;
3932
}

packages/schematics/angular/migrations/update-9/update-app-tsconfigs.ts

+6-21
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { JsonAstObject, JsonParseMode, parseJsonAst } from '@angular-devkit/core';
8+
import { JsonAstObject } from '@angular-devkit/core';
99
import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics';
1010
import {
1111
findPropertyInAstObject,
1212
insertPropertyInAstObjectInOrder,
1313
removePropertyInAstObject,
1414
} from '../../utility/json-utils';
1515
import { Builders } from '../../utility/workspace-models';
16-
import { getAllOptions, getTargets, getWorkspace } from './utils';
16+
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
1717

1818

1919
/**
@@ -52,7 +52,7 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
5252
}
5353

5454
const tsConfigPath = tsConfigOption.value;
55-
let tsConfigAst = getTsConfigAst(tree, tsConfigPath);
55+
let tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
5656
if (!tsConfigAst) {
5757
continue;
5858
}
@@ -77,7 +77,7 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
7777
if (builderName !== Builders.Karma) {
7878
// Note: we need to re-read the tsconfig after very commit because
7979
// otherwise the updates will be out of sync since we are ammending the same node.
80-
tsConfigAst = getTsConfigAst(tree, tsConfigPath) as JsonAstObject;
80+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
8181
const files = findPropertyInAstObject(tsConfigAst, 'files');
8282
const include = findPropertyInAstObject(tsConfigAst, 'include');
8383

@@ -93,32 +93,17 @@ function updateTsConfig(tree: Tree, builderConfig: JsonAstObject, builderName: B
9393
tree.commitUpdate(recorder);
9494

9595
if (builderName === Builders.Browser) {
96-
tsConfigAst = getTsConfigAst(tree, tsConfigPath) as JsonAstObject;
96+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
9797
recorder = tree.beginUpdate(tsConfigPath);
9898
insertPropertyInAstObjectInOrder(recorder, tsConfigAst, 'include', [`${rootSrc}**/*.d.ts`], 2);
9999
tree.commitUpdate(recorder);
100100
}
101101

102-
tsConfigAst = getTsConfigAst(tree, tsConfigPath) as JsonAstObject;
102+
tsConfigAst = readJsonFileAsAstObject(tree, tsConfigPath);
103103
recorder = tree.beginUpdate(tsConfigPath);
104104
removePropertyInAstObject(recorder, tsConfigAst, 'exclude');
105105
tree.commitUpdate(recorder);
106106
}
107107
}
108108
}
109109
}
110-
111-
function getTsConfigAst(tree: Tree, path: string): JsonAstObject | undefined {
112-
const configBuffer = tree.read(path);
113-
if (!configBuffer) {
114-
return undefined;
115-
}
116-
117-
const content = configBuffer.toString();
118-
const tsConfigAst = parseJsonAst(content, JsonParseMode.Loose);
119-
if (!tsConfigAst || tsConfigAst.kind !== 'object') {
120-
return undefined;
121-
}
122-
123-
return tsConfigAst;
124-
}

packages/schematics/angular/migrations/update-9/utils.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,23 @@ export function getAllOptions(builderConfig: JsonAstObject, configurationsOnly =
7272

7373
export function getWorkspace(host: Tree): JsonAstObject {
7474
const path = getWorkspacePath(host);
75+
76+
return readJsonFileAsAstObject(host, path);
77+
}
78+
79+
export function readJsonFileAsAstObject(host: Tree, path: string): JsonAstObject {
7580
const configBuffer = host.read(path);
7681
if (!configBuffer) {
7782
throw new SchematicsException(`Could not find (${path})`);
7883
}
7984

8085
const content = configBuffer.toString();
86+
const astContent = parseJsonAst(content, JsonParseMode.Loose);
87+
if (!astContent || astContent.kind !== 'object') {
88+
throw new SchematicsException(`Invalid JSON AST Object (${path})`);
89+
}
8190

82-
return parseJsonAst(content, JsonParseMode.Loose) as JsonAstObject;
91+
return astContent;
8392
}
8493

8594
export function isIvyEnabled(tree: Tree, tsConfigPath: string): boolean {

0 commit comments

Comments
 (0)