6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { Rule } from '@angular-devkit/schematics' ;
9
- import { appendValueInAstArray , findPropertyInAstObject } from '../../utility/json-utils' ;
9
+ import { JSONFile } from '../../utility/json-file' ;
10
+ import { findPropertyInAstObject } from '../../utility/json-utils' ;
10
11
import { Builders } from '../../utility/workspace-models' ;
11
- import { getAllOptions , getTargets , getWorkspace , readJsonFileAsAstObject } from './utils' ;
12
+ import { getAllOptions , getTargets , getWorkspace } from './utils' ;
12
13
13
14
14
15
/**
15
16
* Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277
16
17
*/
17
18
export function updateNGSWConfig ( ) : Rule {
18
- return ( tree , context ) => {
19
+ return ( tree , { logger } ) => {
19
20
const workspace = getWorkspace ( tree ) ;
20
- const logger = context . logger ;
21
21
22
22
for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
23
23
for ( const options of getAllOptions ( target ) ) {
@@ -27,46 +27,41 @@ export function updateNGSWConfig(): Rule {
27
27
}
28
28
29
29
const path = ngswConfigPath . value ;
30
- const ngswConfigAst = readJsonFileAsAstObject ( tree , path ) ;
31
- if ( ! ngswConfigAst || ngswConfigAst . kind !== 'object' ) {
30
+ let ngswConfigJson ;
31
+ try {
32
+ ngswConfigJson = new JSONFile ( tree , path ) ;
33
+ } catch {
32
34
logger . warn ( `Cannot find file: ${ ngswConfigPath } ` ) ;
33
35
continue ;
34
36
}
35
37
36
- const assetGroups = findPropertyInAstObject ( ngswConfigAst , 'assetGroups' ) ;
37
- if ( ! assetGroups || assetGroups . kind !== 'array' ) {
38
+ const assetGroups = ngswConfigJson . get ( [ 'assetGroups' ] ) ;
39
+ if ( ! assetGroups || ! Array . isArray ( assetGroups ) ) {
38
40
continue ;
39
41
}
40
42
41
- const prefetchElement = assetGroups . elements . find ( element => {
42
- const installMode = element . kind === 'object' && findPropertyInAstObject ( element , 'installMode' ) ;
43
+ const prefetchElementIndex = assetGroups . findIndex (
44
+ ( element ) => element ?. installMode === 'prefetch' ,
45
+ ) ;
43
46
44
- return installMode && installMode . value === 'prefetch' ;
45
- } ) ;
46
-
47
- if ( ! prefetchElement || prefetchElement . kind !== 'object' ) {
48
- continue ;
49
- }
50
-
51
- const resources = findPropertyInAstObject ( prefetchElement , 'resources' ) ;
52
- if ( ! resources || resources . kind !== 'object' ) {
47
+ if ( prefetchElementIndex === - 1 ) {
53
48
continue ;
54
49
}
55
50
56
- const files = findPropertyInAstObject ( resources , 'files' ) ;
57
- if ( ! files || files . kind !== 'array' ) {
51
+ const filesPath = [ 'assetGroups' , prefetchElementIndex , 'resources' , 'files' ] ;
52
+ const files = ngswConfigJson . get ( filesPath ) ;
53
+ if ( ! files || ! Array . isArray ( files ) ) {
58
54
continue ;
59
55
}
60
56
61
- const hasManifest = files . elements
62
- . some ( ( { value } ) => typeof value === 'string' && value . endsWith ( 'manifest.webmanifest' ) ) ;
57
+ const hasManifest = files
58
+ . some ( ( value ) => typeof value === 'string' && value . endsWith ( 'manifest.webmanifest' ) ) ;
63
59
if ( hasManifest ) {
64
60
continue ;
65
61
}
66
62
67
- const recorder = tree . beginUpdate ( path ) ;
68
- appendValueInAstArray ( recorder , files , '/manifest.webmanifest' , 10 ) ;
69
- tree . commitUpdate ( recorder ) ;
63
+ // Append to files array
64
+ ngswConfigJson . modify ( [ ...filesPath , - 1 ] , '/manifest.webmanifest' ) ;
70
65
}
71
66
}
72
67
0 commit comments