@@ -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 ,
@@ -39,15 +38,14 @@ function addServiceWorker(options: PwaOptions): Rule {
39
38
40
39
function getIndent ( text : string ) : string {
41
40
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
+ }
51
49
52
50
return indent ;
53
51
}
@@ -70,43 +68,32 @@ function updateIndexFile(options: PwaOptions): Rule {
70
68
const content = buffer . toString ( ) ;
71
69
const lines = content . split ( '\n' ) ;
72
70
let closingHeadTagLineIndex = - 1 ;
73
- let closingHeadTagLine = '' ;
74
71
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 ) ) {
79
74
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 ) ) {
84
76
closingBodyTagLineIndex = index ;
85
77
}
86
78
} ) ;
87
79
88
- const headTagIndent = getIndent ( closingHeadTagLine ) + ' ' ;
80
+ const headIndent = getIndent ( lines [ closingHeadTagLineIndex ] ) + ' ' ;
89
81
const itemsToAddToHead = [
90
82
'<link rel="manifest" href="manifest.json">' ,
91
83
'<meta name="theme-color" content="#1976d2">' ,
92
84
] ;
93
85
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
+ ] ;
103
90
104
91
const updatedIndex = [
105
92
...lines . slice ( 0 , closingHeadTagLineIndex ) ,
106
- textToInsertIntoHead ,
93
+ ... itemsToAddToHead . map ( line => headIndent + line ) ,
107
94
...lines . slice ( closingHeadTagLineIndex , closingBodyTagLineIndex ) ,
108
- textToInsertIntoBody ,
109
- ...lines . slice ( closingBodyTagLineIndex ) ,
95
+ ... itemsToAddToBody . map ( line => bodyIndent + line ) ,
96
+ ...lines . slice ( closingHeadTagLineIndex ) ,
110
97
] . join ( '\n' ) ;
111
98
112
99
host . overwrite ( path , updatedIndex ) ;
@@ -137,12 +124,9 @@ function addManifestToAssetsConfig(options: PwaOptions) {
137
124
[ 'build' , 'test' ] . forEach ( ( target ) => {
138
125
139
126
const applyTo = architect [ target ] . options ;
127
+ const assets = applyTo . assets || ( applyTo . assets = [ ] ) ;
140
128
141
- if ( ! applyTo . assets ) {
142
- applyTo . assets = [ assetEntry ] ;
143
- } else {
144
- applyTo . assets . push ( assetEntry ) ;
145
- }
129
+ assets . push ( assetEntry ) ;
146
130
147
131
} ) ;
148
132
@@ -163,27 +147,24 @@ export default function (options: PwaOptions): Rule {
163
147
throw new SchematicsException ( `PWA requires a project type of "application".` ) ;
164
148
}
165
149
166
- const assetPath = join ( project . root as Path , 'src' , 'assets' ) ;
167
150
const sourcePath = join ( project . root as Path , 'src' ) ;
151
+ const assetsPath = join ( sourcePath , 'assets' ) ;
168
152
169
153
options . title = options . title || options . project ;
170
154
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 ) ,
176
162
] ) ;
177
163
178
164
return chain ( [
179
165
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 ) ,
187
168
updateIndexFile ( options ) ,
188
169
addManifestToAssetsConfig ( options ) ,
189
170
] ) ;
0 commit comments