11/**
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' ;
169import {
1710 Rule ,
1811 SchematicsException ,
@@ -30,28 +23,6 @@ import { Schema as PwaOptions } from './schema';
3023
3124const RewritingStream = require ( 'parse5-html-rewriting-stream' ) ;
3225
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-
5526function updateIndexFile ( path : string ) : Rule {
5627 return ( host : Tree ) => {
5728 const buffer = host . read ( path ) ;
@@ -111,40 +82,38 @@ function updateIndexFile(path: string): Rule {
11182 } ;
11283}
11384
114- export default function ( options : PwaOptions ) : Rule {
115- return ( host : Tree ) => {
85+ export default function ( options : PwaOptions ) : Rule {
86+ return async host => {
11687 if ( ! options . title ) {
11788 options . title = options . project ;
11889 }
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 ) ;
12095
12196 if ( ! options . project ) {
12297 throw new SchematicsException ( 'Option "project" is required.' ) ;
12398 }
12499
125- const project = workspace . projects [ options . project ] ;
100+ const project = workspace . projects . get ( options . project ) ;
126101 if ( ! project ) {
127102 throw new SchematicsException ( `Project is not defined in this workspace.` ) ;
128103 }
129104
130- if ( project . projectType !== 'application' ) {
105+ if ( project . extensions [ ' projectType' ] !== 'application' ) {
131106 throw new SchematicsException ( `PWA requires a project type of "application".` ) ;
132107 }
133108
134109 // 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 ) {
137111 throw new SchematicsException ( `Targets are not defined for this project.` ) ;
138112 }
139113
140114 const buildTargets = [ ] ;
141115 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 ( ) ) {
148117 if ( target . builder === '@angular-devkit/build-angular:browser' ) {
149118 buildTargets . push ( target ) ;
150119 } else if ( target . builder === '@angular-devkit/build-angular:karma' ) {
@@ -156,21 +125,20 @@ export default function (options: PwaOptions): Rule {
156125 const assetEntry = join ( normalize ( project . root ) , 'src' , 'manifest.webmanifest' ) ;
157126 for ( const target of [ ...buildTargets , ...testTargets ] ) {
158127 if ( target . options ) {
159- if ( target . options . assets ) {
128+ if ( Array . isArray ( target . options . assets ) ) {
160129 target . options . assets . push ( assetEntry ) ;
161130 } else {
162- target . options . assets = [ assetEntry ] ;
131+ target . options . assets = [ assetEntry ] ;
163132 }
164133 } else {
165- target . options = { assets : [ assetEntry ] } ;
134+ target . options = { assets : [ assetEntry ] } ;
166135 }
167136 }
168- host . overwrite ( workspacePath , JSON . stringify ( workspace , null , 2 ) ) ;
169137
170138 // Find all index.html files in build targets
171139 const indexFiles = new Set < string > ( ) ;
172140 for ( const target of buildTargets ) {
173- if ( target . options && target . options . index ) {
141+ if ( target . options && typeof target . options . index === 'string' ) {
174142 indexFiles . add ( target . options . index ) ;
175143 }
176144
@@ -179,7 +147,7 @@ export default function (options: PwaOptions): Rule {
179147 }
180148 for ( const configName in target . configurations ) {
181149 const configuration = target . configurations [ configName ] ;
182- if ( configuration && configuration . index ) {
150+ if ( configuration && typeof configuration . index === 'string' ) {
183151 indexFiles . add ( configuration . index ) ;
184152 }
185153 }
@@ -203,6 +171,7 @@ export default function (options: PwaOptions): Rule {
203171
204172 // Chain the rules and return
205173 return chain ( [
174+ updateWorkspace ( workspace ) ,
206175 externalSchematic ( '@schematics/angular' , 'service-worker' , swOptions ) ,
207176 mergeWith ( rootTemplateSource ) ,
208177 mergeWith ( assetsTemplateSource ) ,
0 commit comments