@@ -12,7 +12,6 @@ import {
12
12
SchematicsException ,
13
13
Tree ,
14
14
apply ,
15
- branchAndMerge ,
16
15
chain ,
17
16
externalSchematic ,
18
17
mergeWith ,
@@ -38,18 +37,8 @@ function addServiceWorker(options: PwaOptions): Rule {
38
37
}
39
38
40
39
function getIndent ( text : string ) : string {
41
- 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 ) ;
51
-
52
- return indent ;
40
+ // This RegExp is guaranteed to match any string (even if it is a zero-length match).
41
+ return ( / ^ * / . exec ( text ) as RegExpExecArray ) [ 0 ] ;
53
42
}
54
43
55
44
function updateIndexFile ( options : PwaOptions ) : Rule {
@@ -70,43 +59,32 @@ function updateIndexFile(options: PwaOptions): Rule {
70
59
const content = buffer . toString ( ) ;
71
60
const lines = content . split ( '\n' ) ;
72
61
let closingHeadTagLineIndex = - 1 ;
73
- let closingHeadTagLine = '' ;
74
62
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 ;
63
+ lines . forEach ( ( line , index ) => {
64
+ if ( closingHeadTagLineIndex === - 1 && / < \/ h e a d > / . test ( line ) ) {
79
65
closingHeadTagLineIndex = index ;
80
- }
81
-
82
- if ( / < \/ b o d y > / . test ( line ) && closingBodyTagLineIndex === - 1 ) {
83
- closingBodyTagLine = line ;
66
+ } else if ( closingBodyTagLineIndex === - 1 && / < \/ b o d y > / . test ( line ) ) {
84
67
closingBodyTagLineIndex = index ;
85
68
}
86
69
} ) ;
87
70
88
- const headTagIndent = getIndent ( closingHeadTagLine ) + ' ' ;
71
+ const headIndent = getIndent ( lines [ closingHeadTagLineIndex ] ) + ' ' ;
89
72
const itemsToAddToHead = [
90
73
'<link rel="manifest" href="manifest.json">' ,
91
74
'<meta name="theme-color" content="#1976d2">' ,
92
75
] ;
93
76
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 ;
77
+ const bodyIndent = getIndent ( lines [ closingBodyTagLineIndex ] ) + ' ' ;
78
+ const itemsToAddToBody = [
79
+ '<noscript>Please enable JavaScript to continue using this application.</noscript>' ,
80
+ ] ;
103
81
104
82
const updatedIndex = [
105
83
...lines . slice ( 0 , closingHeadTagLineIndex ) ,
106
- textToInsertIntoHead ,
84
+ ... itemsToAddToHead . map ( line => headIndent + line ) ,
107
85
...lines . slice ( closingHeadTagLineIndex , closingBodyTagLineIndex ) ,
108
- textToInsertIntoBody ,
109
- ...lines . slice ( closingBodyTagLineIndex ) ,
86
+ ... itemsToAddToBody . map ( line => bodyIndent + line ) ,
87
+ ...lines . slice ( closingHeadTagLineIndex ) ,
110
88
] . join ( '\n' ) ;
111
89
112
90
host . overwrite ( path , updatedIndex ) ;
@@ -137,12 +115,9 @@ function addManifestToAssetsConfig(options: PwaOptions) {
137
115
[ 'build' , 'test' ] . forEach ( ( target ) => {
138
116
139
117
const applyTo = architect [ target ] . options ;
118
+ const assets = applyTo . assets || ( applyTo . assets = [ ] ) ;
140
119
141
- if ( ! applyTo . assets ) {
142
- applyTo . assets = [ assetEntry ] ;
143
- } else {
144
- applyTo . assets . push ( assetEntry ) ;
145
- }
120
+ assets . push ( assetEntry ) ;
146
121
147
122
} ) ;
148
123
@@ -163,27 +138,24 @@ export default function (options: PwaOptions): Rule {
163
138
throw new SchematicsException ( `PWA requires a project type of "application".` ) ;
164
139
}
165
140
166
- const assetPath = join ( project . root as Path , 'src' , 'assets' ) ;
167
141
const sourcePath = join ( project . root as Path , 'src' ) ;
142
+ const assetsPath = join ( sourcePath , 'assets' ) ;
168
143
169
144
options . title = options . title || options . project ;
170
145
171
- const templateSource = apply ( url ( './files/assets' ) , [
172
- template ( {
173
- ...options ,
174
- } ) ,
175
- move ( assetPath ) ,
146
+ const rootTemplateSource = apply ( url ( './files/root' ) , [
147
+ template ( { ...options } ) ,
148
+ move ( sourcePath ) ,
149
+ ] ) ;
150
+ const assetsTemplateSource = apply ( url ( './files/assets' ) , [
151
+ template ( { ...options } ) ,
152
+ move ( assetsPath ) ,
176
153
] ) ;
177
154
178
155
return chain ( [
179
156
addServiceWorker ( options ) ,
180
- branchAndMerge ( chain ( [
181
- mergeWith ( templateSource ) ,
182
- ] ) ) ,
183
- mergeWith ( apply ( url ( './files/root' ) , [
184
- template ( { ...options } ) ,
185
- move ( sourcePath ) ,
186
- ] ) ) ,
157
+ mergeWith ( rootTemplateSource ) ,
158
+ mergeWith ( assetsTemplateSource ) ,
187
159
updateIndexFile ( options ) ,
188
160
addManifestToAssetsConfig ( options ) ,
189
161
] ) ;
0 commit comments