@@ -12,7 +12,6 @@ import {
1212 SchematicsException ,
1313 Tree ,
1414 apply ,
15- branchAndMerge ,
1615 chain ,
1716 externalSchematic ,
1817 mergeWith ,
@@ -39,15 +38,14 @@ function addServiceWorker(options: PwaOptions): Rule {
3938
4039function getIndent ( text : string ) : string {
4140 let indent = '' ;
42- let hitNonSpace = false ;
43- text . split ( '' )
44- . forEach ( char => {
45- if ( char === ' ' && ! hitNonSpace ) {
46- indent += ' ' ;
47- } else {
48- hitNonSpace = true ;
49- }
50- } , 0 ) ;
41+
42+ for ( const char of text ) {
43+ if ( char === ' ' || char === '\t' ) {
44+ indent += char ;
45+ } else {
46+ break ;
47+ }
48+ }
5149
5250 return indent ;
5351}
@@ -70,43 +68,32 @@ function updateIndexFile(options: PwaOptions): Rule {
7068 const content = buffer . toString ( ) ;
7169 const lines = content . split ( '\n' ) ;
7270 let closingHeadTagLineIndex = - 1 ;
73- let closingHeadTagLine = '' ;
7471 let closingBodyTagLineIndex = - 1 ;
75- let closingBodyTagLine = '' ;
76- lines . forEach ( ( line : string , index : number ) => {
77- if ( / < \/ h e a d > / . test ( line ) && closingHeadTagLineIndex === - 1 ) {
78- closingHeadTagLine = line ;
72+ lines . forEach ( ( line , index ) => {
73+ if ( closingHeadTagLineIndex === - 1 && / < \/ h e a d > / . test ( line ) ) {
7974 closingHeadTagLineIndex = index ;
80- }
81-
82- if ( / < \/ b o d y > / . test ( line ) && closingBodyTagLineIndex === - 1 ) {
83- closingBodyTagLine = line ;
75+ } else if ( closingBodyTagLineIndex === - 1 && / < \/ b o d y > / . test ( line ) ) {
8476 closingBodyTagLineIndex = index ;
8577 }
8678 } ) ;
8779
88- const headTagIndent = getIndent ( closingHeadTagLine ) + ' ' ;
80+ const headIndent = getIndent ( lines [ closingHeadTagLineIndex ] ) + ' ' ;
8981 const itemsToAddToHead = [
9082 '<link rel="manifest" href="manifest.json">' ,
9183 '<meta name="theme-color" content="#1976d2">' ,
9284 ] ;
9385
94- const textToInsertIntoHead = itemsToAddToHead
95- . map ( text => headTagIndent + text )
96- . join ( '\n' ) ;
97-
98- const bodyTagIndent = getIndent ( closingBodyTagLine ) + ' ' ;
99- const itemsToAddToBody
100- = '<noscript>Please enable JavaScript to continue using this application.</noscript>' ;
101-
102- const textToInsertIntoBody = bodyTagIndent + itemsToAddToBody ;
86+ const bodyIndent = getIndent ( lines [ closingBodyTagLineIndex ] ) + ' ' ;
87+ const itemsToAddToBody = [
88+ '<noscript>Please enable JavaScript to continue using this application.</noscript>' ,
89+ ] ;
10390
10491 const updatedIndex = [
10592 ...lines . slice ( 0 , closingHeadTagLineIndex ) ,
106- textToInsertIntoHead ,
93+ ... itemsToAddToHead . map ( line => headIndent + line ) ,
10794 ...lines . slice ( closingHeadTagLineIndex , closingBodyTagLineIndex ) ,
108- textToInsertIntoBody ,
109- ...lines . slice ( closingBodyTagLineIndex ) ,
95+ ... itemsToAddToBody . map ( line => bodyIndent + line ) ,
96+ ...lines . slice ( closingHeadTagLineIndex ) ,
11097 ] . join ( '\n' ) ;
11198
11299 host . overwrite ( path , updatedIndex ) ;
@@ -137,12 +124,9 @@ function addManifestToAssetsConfig(options: PwaOptions) {
137124 [ 'build' , 'test' ] . forEach ( ( target ) => {
138125
139126 const applyTo = architect [ target ] . options ;
127+ const assets = applyTo . assets || ( applyTo . assets = [ ] ) ;
140128
141- if ( ! applyTo . assets ) {
142- applyTo . assets = [ assetEntry ] ;
143- } else {
144- applyTo . assets . push ( assetEntry ) ;
145- }
129+ assets . push ( assetEntry ) ;
146130
147131 } ) ;
148132
@@ -163,27 +147,24 @@ export default function (options: PwaOptions): Rule {
163147 throw new SchematicsException ( `PWA requires a project type of "application".` ) ;
164148 }
165149
166- const assetPath = join ( project . root as Path , 'src' , 'assets' ) ;
167150 const sourcePath = join ( project . root as Path , 'src' ) ;
151+ const assetsPath = join ( sourcePath , 'assets' ) ;
168152
169153 options . title = options . title || options . project ;
170154
171- const templateSource = apply ( url ( './files/assets' ) , [
172- template ( {
173- ...options ,
174- } ) ,
175- move ( assetPath ) ,
155+ const rootTemplateSource = apply ( url ( './files/root' ) , [
156+ template ( { ...options } ) ,
157+ move ( sourcePath ) ,
158+ ] ) ;
159+ const assetsTemplateSource = apply ( url ( './files/assets' ) , [
160+ template ( { ...options } ) ,
161+ move ( assetsPath ) ,
176162 ] ) ;
177163
178164 return chain ( [
179165 addServiceWorker ( options ) ,
180- branchAndMerge ( chain ( [
181- mergeWith ( templateSource ) ,
182- ] ) ) ,
183- mergeWith ( apply ( url ( './files/root' ) , [
184- template ( { ...options } ) ,
185- move ( sourcePath ) ,
186- ] ) ) ,
166+ mergeWith ( rootTemplateSource ) ,
167+ mergeWith ( assetsTemplateSource ) ,
187168 updateIndexFile ( options ) ,
188169 addManifestToAssetsConfig ( options ) ,
189170 ] ) ;
0 commit comments