Skip to content

Commit 9167122

Browse files
clydinmgechev
authored andcommitted
refactor(@schematics/angular): use new JSON helpers in typescript-helpers migration
1 parent 14c49bd commit 9167122

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

packages/schematics/angular/migrations/update-7/typescript-helpers.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
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, Tree, chain } from '@angular-devkit/schematics';
8+
import { Rule, chain } from '@angular-devkit/schematics';
109
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
1110
import {
1211
NodeDependencyType,
1312
addPackageJsonDependency,
1413
getPackageJsonDependency,
1514
} from '../../utility/dependencies';
16-
import {
17-
findPropertyInAstObject,
18-
insertPropertyInAstObjectInOrder,
19-
} from '../../utility/json-utils';
15+
import { JSONFile } from '../../utility/json-file';
2016
import { latestVersions } from '../../utility/latest-versions';
2117

2218
export function typeScriptHelpersRule(): Rule {
@@ -42,38 +38,27 @@ export function typeScriptHelpersRule(): Rule {
4238
}
4339

4440
function _updateTsConfig(): Rule {
45-
return (host: Tree) => {
41+
return (host) => {
4642
const tsConfigPath = '/tsconfig.json';
47-
const buffer = host.read(tsConfigPath);
48-
if (!buffer) {
49-
return host;
50-
}
51-
52-
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
53-
if (tsCfgAst.kind !== 'object') {
54-
return host;
55-
}
5643

57-
const compilerOptions = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
58-
if (!compilerOptions || compilerOptions.kind !== 'object') {
59-
return host;
44+
let tsConfigJson;
45+
try {
46+
tsConfigJson = new JSONFile(host, tsConfigPath);
47+
} catch {
48+
return;
6049
}
6150

62-
const importHelpers = findPropertyInAstObject(compilerOptions, 'importHelpers');
63-
if (importHelpers && importHelpers.value === true) {
64-
return host;
51+
const compilerOptions = tsConfigJson.get(['compilerOptions']);
52+
if (!compilerOptions || typeof compilerOptions !== 'object') {
53+
return;
6554
}
6655

67-
const recorder = host.beginUpdate(tsConfigPath);
68-
if (importHelpers) {
69-
const { start, end } = importHelpers;
70-
recorder.remove(start.offset, end.offset - start.offset);
71-
recorder.insertLeft(start.offset, 'true');
72-
} else {
73-
insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'importHelpers', true, 4);
56+
const importHelpersPath = ['compilerOptions', 'importHelpers'];
57+
const importHelpers = tsConfigJson.get(importHelpersPath);
58+
if (importHelpers === true) {
59+
return;
7460
}
75-
host.commitUpdate(recorder);
7661

77-
return host;
62+
tsConfigJson.modify(importHelpersPath, true);
7863
};
7964
}

0 commit comments

Comments
 (0)