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 { getSystemPath , join , normalize } from '@angular-devkit/core' ;
8+ import { join , normalize } from '@angular-devkit/core' ;
99import {
1010 Rule ,
1111 SchematicsException ,
@@ -18,6 +18,7 @@ import {
1818 template ,
1919 url ,
2020} from '@angular-devkit/schematics' ;
21+ import { getWorkspace , updateWorkspace } from '@schematics/angular/utility/workspace' ;
2122import { Readable , Writable } from 'stream' ;
2223import { Schema as PwaOptions } from './schema' ;
2324
@@ -31,7 +32,6 @@ function updateIndexFile(path: string): Rule {
3132 }
3233
3334 const rewriter = new RewritingStream ( ) ;
34-
3535 let needsNoScript = true ;
3636 rewriter . on ( 'startTag' , ( startTag : { tagName : string } ) => {
3737 if ( startTag . tagName === 'noscript' ) {
@@ -88,9 +88,6 @@ export default function(options: PwaOptions): Rule {
8888 options . title = options . project ;
8989 }
9090
91- // Keep Bazel from failing due to deep import
92- const { getWorkspace, updateWorkspace } = require ( '@schematics/angular/utility/workspace' ) ;
93-
9491 const workspace = await getWorkspace ( host ) ;
9592
9693 if ( ! options . project ) {
@@ -138,43 +135,38 @@ export default function(options: PwaOptions): Rule {
138135 // Find all index.html files in build targets
139136 const indexFiles = new Set < string > ( ) ;
140137 for ( const target of buildTargets ) {
141- if ( target . options && typeof target . options . index === 'string' ) {
138+ if ( typeof target . options ? .index === 'string' ) {
142139 indexFiles . add ( target . options . index ) ;
143140 }
144141
145142 if ( ! target . configurations ) {
146143 continue ;
147144 }
148- for ( const configName in target . configurations ) {
149- const configuration = target . configurations [ configName ] ;
150- if ( configuration && typeof configuration . index === 'string' ) {
151- indexFiles . add ( configuration . index ) ;
145+
146+ for ( const options of Object . values ( target . configurations ) ) {
147+ if ( typeof options ? .index === 'string' ) {
148+ indexFiles . add ( options . index ) ;
152149 }
153150 }
154151 }
155152
156153 // Setup sources for the assets files to add to the project
157- const sourcePath = join ( normalize ( project . root ) , 'src' ) ;
158- const assetsPath = join ( sourcePath , 'assets' ) ;
159- const rootTemplateSource = apply ( url ( './files/root' ) , [
160- template ( { ...options } ) ,
161- move ( getSystemPath ( sourcePath ) ) ,
162- ] ) ;
163- const assetsTemplateSource = apply ( url ( './files/assets' ) , [
164- template ( { ...options } ) ,
165- move ( getSystemPath ( assetsPath ) ) ,
166- ] ) ;
154+ const sourcePath = normalize ( project . sourceRoot ?? 'src' ) ;
167155
168156 // Setup service worker schematic options
169- const swOptions = { ...options } ;
170- delete swOptions . title ;
157+ const { title, ...swOptions } = options ;
171158
172- // Chain the rules and return
173159 return chain ( [
174160 updateWorkspace ( workspace ) ,
175161 externalSchematic ( '@schematics/angular' , 'service-worker' , swOptions ) ,
176- mergeWith ( rootTemplateSource ) ,
177- mergeWith ( assetsTemplateSource ) ,
162+ mergeWith ( apply ( url ( './files/root' ) , [
163+ template ( { ...options } ) ,
164+ move ( sourcePath ) ,
165+ ] ) ) ,
166+ mergeWith ( apply ( url ( './files/assets' ) , [
167+ template ( { ...options } ) ,
168+ move ( join ( sourcePath , 'assets' ) ) ,
169+ ] ) ) ,
178170 ...[ ...indexFiles ] . map ( path => updateIndexFile ( path ) ) ,
179171 ] ) ;
180172 } ;
0 commit comments