1
1
/**
2
- * @license
3
- * Copyright Google Inc. All Rights Reserved.
4
- *
5
- * Use of this source code is governed by an MIT-style license that can be
6
- * found in the LICENSE file at https://angular.io/license
7
- */
8
- import {
9
- JsonParseMode ,
10
- experimental ,
11
- getSystemPath ,
12
- join ,
13
- normalize ,
14
- parseJson ,
15
- } from '@angular-devkit/core' ;
2
+ * @license
3
+ * Copyright Google Inc. All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { getSystemPath , join , normalize } from '@angular-devkit/core' ;
16
9
import {
17
10
Rule ,
18
11
SchematicsException ,
@@ -30,28 +23,6 @@ import { Schema as PwaOptions } from './schema';
30
23
31
24
const RewritingStream = require ( 'parse5-html-rewriting-stream' ) ;
32
25
33
-
34
- function getWorkspace (
35
- host : Tree ,
36
- ) : { path : string , workspace : experimental . workspace . WorkspaceSchema } {
37
- const possibleFiles = [ '/angular.json' , '/.angular.json' ] ;
38
- const path = possibleFiles . filter ( path => host . exists ( path ) ) [ 0 ] ;
39
-
40
- const configBuffer = host . read ( path ) ;
41
- if ( configBuffer === null ) {
42
- throw new SchematicsException ( `Could not find (${ path } )` ) ;
43
- }
44
- const content = configBuffer . toString ( ) ;
45
-
46
- return {
47
- path,
48
- workspace : parseJson (
49
- content ,
50
- JsonParseMode . Loose ,
51
- ) as { } as experimental . workspace . WorkspaceSchema ,
52
- } ;
53
- }
54
-
55
26
function updateIndexFile ( path : string ) : Rule {
56
27
return ( host : Tree ) => {
57
28
const buffer = host . read ( path ) ;
@@ -111,40 +82,38 @@ function updateIndexFile(path: string): Rule {
111
82
} ;
112
83
}
113
84
114
- export default function ( options : PwaOptions ) : Rule {
115
- return ( host : Tree ) => {
85
+ export default function ( options : PwaOptions ) : Rule {
86
+ return async host => {
116
87
if ( ! options . title ) {
117
88
options . title = options . project ;
118
89
}
119
- const { path : workspacePath , workspace } = getWorkspace ( host ) ;
90
+
91
+ // Keep Bazel from failing due to deep import
92
+ const { getWorkspace, updateWorkspace } = require ( '@schematics/angular/utility/workspace' ) ;
93
+
94
+ const workspace = await getWorkspace ( host ) ;
120
95
121
96
if ( ! options . project ) {
122
97
throw new SchematicsException ( 'Option "project" is required.' ) ;
123
98
}
124
99
125
- const project = workspace . projects [ options . project ] ;
100
+ const project = workspace . projects . get ( options . project ) ;
126
101
if ( ! project ) {
127
102
throw new SchematicsException ( `Project is not defined in this workspace.` ) ;
128
103
}
129
104
130
- if ( project . projectType !== 'application' ) {
105
+ if ( project . extensions [ ' projectType' ] !== 'application' ) {
131
106
throw new SchematicsException ( `PWA requires a project type of "application".` ) ;
132
107
}
133
108
134
109
// Find all the relevant targets for the project
135
- const projectTargets = project . targets || project . architect ;
136
- if ( ! projectTargets || Object . keys ( projectTargets ) . length === 0 ) {
110
+ if ( project . targets . size === 0 ) {
137
111
throw new SchematicsException ( `Targets are not defined for this project.` ) ;
138
112
}
139
113
140
114
const buildTargets = [ ] ;
141
115
const testTargets = [ ] ;
142
- for ( const targetName in projectTargets ) {
143
- const target = projectTargets [ targetName ] ;
144
- if ( ! target ) {
145
- continue ;
146
- }
147
-
116
+ for ( const target of project . targets . values ( ) ) {
148
117
if ( target . builder === '@angular-devkit/build-angular:browser' ) {
149
118
buildTargets . push ( target ) ;
150
119
} else if ( target . builder === '@angular-devkit/build-angular:karma' ) {
@@ -156,21 +125,20 @@ export default function (options: PwaOptions): Rule {
156
125
const assetEntry = join ( normalize ( project . root ) , 'src' , 'manifest.webmanifest' ) ;
157
126
for ( const target of [ ...buildTargets , ...testTargets ] ) {
158
127
if ( target . options ) {
159
- if ( target . options . assets ) {
128
+ if ( Array . isArray ( target . options . assets ) ) {
160
129
target . options . assets . push ( assetEntry ) ;
161
130
} else {
162
- target . options . assets = [ assetEntry ] ;
131
+ target . options . assets = [ assetEntry ] ;
163
132
}
164
133
} else {
165
- target . options = { assets : [ assetEntry ] } ;
134
+ target . options = { assets : [ assetEntry ] } ;
166
135
}
167
136
}
168
- host . overwrite ( workspacePath , JSON . stringify ( workspace , null , 2 ) ) ;
169
137
170
138
// Find all index.html files in build targets
171
139
const indexFiles = new Set < string > ( ) ;
172
140
for ( const target of buildTargets ) {
173
- if ( target . options && target . options . index ) {
141
+ if ( target . options && typeof target . options . index === 'string' ) {
174
142
indexFiles . add ( target . options . index ) ;
175
143
}
176
144
@@ -179,7 +147,7 @@ export default function (options: PwaOptions): Rule {
179
147
}
180
148
for ( const configName in target . configurations ) {
181
149
const configuration = target . configurations [ configName ] ;
182
- if ( configuration && configuration . index ) {
150
+ if ( configuration && typeof configuration . index === 'string' ) {
183
151
indexFiles . add ( configuration . index ) ;
184
152
}
185
153
}
@@ -203,6 +171,7 @@ export default function (options: PwaOptions): Rule {
203
171
204
172
// Chain the rules and return
205
173
return chain ( [
174
+ updateWorkspace ( workspace ) ,
206
175
externalSchematic ( '@schematics/angular' , 'service-worker' , swOptions ) ,
207
176
mergeWith ( rootTemplateSource ) ,
208
177
mergeWith ( assetsTemplateSource ) ,
0 commit comments