Skip to content

Commit 60053b5

Browse files
committed
refactor(@angular/pwa): minor refactorings to make code cleaner
1 parent 7309c91 commit 60053b5

File tree

1 file changed

+25
-53
lines changed

1 file changed

+25
-53
lines changed

packages/angular/pwa/pwa/index.ts

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
SchematicsException,
1313
Tree,
1414
apply,
15-
branchAndMerge,
1615
chain,
1716
externalSchematic,
1817
mergeWith,
@@ -38,18 +37,8 @@ function addServiceWorker(options: PwaOptions): Rule {
3837
}
3938

4039
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];
5342
}
5443

5544
function updateIndexFile(options: PwaOptions): Rule {
@@ -70,43 +59,32 @@ function updateIndexFile(options: PwaOptions): Rule {
7059
const content = buffer.toString();
7160
const lines = content.split('\n');
7261
let closingHeadTagLineIndex = -1;
73-
let closingHeadTagLine = '';
7462
let closingBodyTagLineIndex = -1;
75-
let closingBodyTagLine = '';
76-
lines.forEach((line: string, index: number) => {
77-
if (/<\/head>/.test(line) && closingHeadTagLineIndex === -1) {
78-
closingHeadTagLine = line;
63+
lines.forEach((line, index) => {
64+
if (closingHeadTagLineIndex === -1 && /<\/head>/.test(line)) {
7965
closingHeadTagLineIndex = index;
80-
}
81-
82-
if (/<\/body>/.test(line) && closingBodyTagLineIndex === -1) {
83-
closingBodyTagLine = line;
66+
} else if (closingBodyTagLineIndex === -1 && /<\/body>/.test(line)) {
8467
closingBodyTagLineIndex = index;
8568
}
8669
});
8770

88-
const headTagIndent = getIndent(closingHeadTagLine) + ' ';
71+
const headIndent = getIndent(lines[closingHeadTagLineIndex]) + ' ';
8972
const itemsToAddToHead = [
9073
'<link rel="manifest" href="manifest.json">',
9174
'<meta name="theme-color" content="#1976d2">',
9275
];
9376

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+
];
10381

10482
const updatedIndex = [
10583
...lines.slice(0, closingHeadTagLineIndex),
106-
textToInsertIntoHead,
84+
...itemsToAddToHead.map(line => headIndent + line),
10785
...lines.slice(closingHeadTagLineIndex, closingBodyTagLineIndex),
108-
textToInsertIntoBody,
109-
...lines.slice(closingBodyTagLineIndex),
86+
...itemsToAddToBody.map(line => bodyIndent + line),
87+
...lines.slice(closingHeadTagLineIndex),
11088
].join('\n');
11189

11290
host.overwrite(path, updatedIndex);
@@ -137,12 +115,9 @@ function addManifestToAssetsConfig(options: PwaOptions) {
137115
['build', 'test'].forEach((target) => {
138116

139117
const applyTo = architect[target].options;
118+
const assets = applyTo.assets || (applyTo.assets = []);
140119

141-
if (!applyTo.assets) {
142-
applyTo.assets = [assetEntry];
143-
} else {
144-
applyTo.assets.push(assetEntry);
145-
}
120+
assets.push(assetEntry);
146121

147122
});
148123

@@ -163,27 +138,24 @@ export default function (options: PwaOptions): Rule {
163138
throw new SchematicsException(`PWA requires a project type of "application".`);
164139
}
165140

166-
const assetPath = join(project.root as Path, 'src', 'assets');
167141
const sourcePath = join(project.root as Path, 'src');
142+
const assetsPath = join(sourcePath, 'assets');
168143

169144
options.title = options.title || options.project;
170145

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),
176153
]);
177154

178155
return chain([
179156
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),
187159
updateIndexFile(options),
188160
addManifestToAssetsConfig(options),
189161
]);

0 commit comments

Comments
 (0)