Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 5111d41

Browse files
committed
refactor: remove usage on @schematics/angular old json helpers
1 parent 2293fa1 commit 5111d41

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

modules/common/schematics/add/index.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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, dirname, join, normalize, parseJsonAst } from '@angular-devkit/core';
8+
import { dirname, join, normalize } from '@angular-devkit/core';
99
import {
1010
Rule,
1111
SchematicsException,
@@ -15,10 +15,7 @@ import {
1515
} from '@angular-devkit/schematics';
1616
import { Schema as UniversalOptions } from '@schematics/angular/universal/schema';
1717
import { NodeDependencyType, addPackageJsonDependency } from '@schematics/angular/utility/dependencies';
18-
import {
19-
appendValueInAstArray,
20-
findPropertyInAstObject,
21-
} from '@schematics/angular/utility/json-utils';
18+
import { JSONFile } from '@schematics/angular/utility/json-file';
2219
import { updateWorkspace } from '@schematics/angular/utility/workspace';
2320
import * as ts from 'typescript';
2421

@@ -152,33 +149,14 @@ function updateServerTsConfigRule(options: AddUniversalOptions): Rule {
152149
return;
153150
}
154151

155-
const configBuffer = host.read(tsConfigPath);
156-
if (!configBuffer) {
157-
throw new SchematicsException(`Could not find (${tsConfigPath})`);
158-
}
159-
160-
const content = configBuffer.toString();
161-
const tsConfigAst = parseJsonAst(content, JsonParseMode.Loose);
162-
if (!tsConfigAst || tsConfigAst.kind !== 'object') {
163-
throw new SchematicsException(`Invalid JSON AST Object (${tsConfigPath})`);
164-
}
165-
166-
const filesAstNode = findPropertyInAstObject(tsConfigAst, 'files');
167-
152+
const tsConfig = new JSONFile(host, tsConfigPath);
153+
const filesAstNode = tsConfig.get(['files']);
168154
const serverFilePath = stripTsExtension(options.serverFileName) + '.ts';
169-
if (
170-
filesAstNode &&
171-
filesAstNode.kind === 'array' &&
172-
!filesAstNode.elements.some(({ text }) => text === serverFilePath)) {
173-
const recorder = host.beginUpdate(tsConfigPath);
174-
175-
appendValueInAstArray(
176-
recorder,
177-
filesAstNode,
178-
stripTsExtension(options.serverFileName) + '.ts',
179-
);
180-
181-
host.commitUpdate(recorder);
155+
if (Array.isArray(filesAstNode) && !filesAstNode.some(({ text }) => text === serverFilePath)) {
156+
tsConfig.modify(['files'], [
157+
...filesAstNode,
158+
serverFilePath
159+
]);
182160
}
183161
};
184162
}

0 commit comments

Comments
 (0)