Skip to content

Commit bb5760c

Browse files
alan-agius4clydin
authored andcommitted
refactor(@angular-devkit/schematics-cli): remove usage of removed json parser
1 parent d722fdf commit bb5760c

File tree

1 file changed

+7
-53
lines changed
  • packages/angular_devkit/schematics_cli/blank

1 file changed

+7
-53
lines changed

packages/angular_devkit/schematics_cli/blank/factory.ts

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {
10-
JsonAstObject,
11-
JsonObject,
12-
JsonValue,
13-
Path,
14-
normalize,
15-
parseJsonAst,
16-
strings,
17-
} from '@angular-devkit/core';
9+
import { JsonObject, Path, isJsonObject, normalize, strings } from '@angular-devkit/core';
1810
import {
1911
Rule,
2012
SchematicContext,
2113
SchematicsException,
2214
Tree,
23-
UpdateRecorder,
2415
apply,
2516
chain,
2617
mergeWith,
@@ -31,29 +22,6 @@ import {
3122
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
3223
import { Schema } from './schema';
3324

34-
function appendPropertyInAstObject(
35-
recorder: UpdateRecorder,
36-
node: JsonAstObject,
37-
propertyName: string,
38-
value: JsonValue,
39-
indent = 4,
40-
) {
41-
const indentStr = '\n' + new Array(indent + 1).join(' ');
42-
43-
if (node.properties.length > 0) {
44-
// Insert comma.
45-
const last = node.properties[node.properties.length - 1];
46-
recorder.insertRight(last.start.offset + last.text.replace(/\s+$/, '').length, ',');
47-
}
48-
49-
recorder.insertLeft(
50-
node.end.offset - 1,
51-
' ' +
52-
`"${propertyName}": ${JSON.stringify(value, null, 2).replace(/\n/g, indentStr)}` +
53-
indentStr.slice(0, -2),
54-
);
55-
}
56-
5725
function addSchematicToCollectionJson(
5826
collectionPath: Path,
5927
schematicName: string,
@@ -64,26 +32,14 @@ function addSchematicToCollectionJson(
6432
if (!collectionJsonContent) {
6533
throw new Error('Invalid collection path: ' + collectionPath);
6634
}
67-
const collectionJsonAst = parseJsonAst(collectionJsonContent.toString('utf-8'));
68-
if (collectionJsonAst.kind !== 'object') {
69-
throw new Error('Invalid collection content.');
70-
}
71-
72-
for (const property of collectionJsonAst.properties) {
73-
if (property.key.value == 'schematics') {
74-
if (property.value.kind !== 'object') {
75-
throw new Error('Invalid collection.json; schematics needs to be an object.');
76-
}
7735

78-
const recorder = tree.beginUpdate(collectionPath);
79-
appendPropertyInAstObject(recorder, property.value, schematicName, description);
80-
tree.commitUpdate(recorder);
81-
82-
return tree;
83-
}
36+
const collectionJson = JSON.parse(collectionJsonContent.toString());
37+
if (!isJsonObject(collectionJson.schematics)) {
38+
throw new Error('Invalid collection.json; schematics needs to be an object.');
8439
}
8540

86-
throw new Error('Could not find the "schematics" property in collection.json.');
41+
collectionJson['schematics'][schematicName] = description;
42+
tree.overwrite(collectionPath, JSON.stringify(collectionJson, undefined, 2));
8743
};
8844
}
8945

@@ -101,9 +57,7 @@ export default function (options: Schema): Rule {
10157
try {
10258
const packageJsonContent = tree.read('/package.json');
10359
if (packageJsonContent) {
104-
// In google3 the return value of JSON.parse() must be immediately typed,
105-
// otherwise it defaults to `any`, which is prohibited.
106-
const packageJson = JSON.parse(packageJsonContent.toString('utf-8')) as {
60+
const packageJson = JSON.parse(packageJsonContent.toString()) as {
10761
schematics: unknown;
10862
};
10963
if (typeof packageJson.schematics === 'string') {

0 commit comments

Comments
 (0)