6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
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' ;
18
10
import {
19
11
Rule ,
20
12
SchematicContext ,
21
13
SchematicsException ,
22
14
Tree ,
23
- UpdateRecorder ,
24
15
apply ,
25
16
chain ,
26
17
mergeWith ,
@@ -31,29 +22,6 @@ import {
31
22
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks' ;
32
23
import { Schema } from './schema' ;
33
24
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
-
57
25
function addSchematicToCollectionJson (
58
26
collectionPath : Path ,
59
27
schematicName : string ,
@@ -64,26 +32,14 @@ function addSchematicToCollectionJson(
64
32
if ( ! collectionJsonContent ) {
65
33
throw new Error ( 'Invalid collection path: ' + collectionPath ) ;
66
34
}
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
- }
77
35
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.' ) ;
84
39
}
85
40
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 ) ) ;
87
43
} ;
88
44
}
89
45
@@ -101,9 +57,7 @@ export default function (options: Schema): Rule {
101
57
try {
102
58
const packageJsonContent = tree . read ( '/package.json' ) ;
103
59
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 {
107
61
schematics : unknown ;
108
62
} ;
109
63
if ( typeof packageJson . schematics === 'string' ) {
0 commit comments