Skip to content

Commit 7b1787f

Browse files
clydinfilipesilva
authored andcommitted
refactor(@schematics/angular): use new JSON helpers in ngsw-config migration
1 parent 12cd4a0 commit 7b1787f

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

packages/schematics/angular/migrations/update-9/ngsw-config.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { Rule } from '@angular-devkit/schematics';
9-
import { appendValueInAstArray, findPropertyInAstObject } from '../../utility/json-utils';
9+
import { JSONFile } from '../../utility/json-file';
10+
import { findPropertyInAstObject } from '../../utility/json-utils';
1011
import { Builders } from '../../utility/workspace-models';
11-
import { getAllOptions, getTargets, getWorkspace, readJsonFileAsAstObject } from './utils';
12+
import { getAllOptions, getTargets, getWorkspace } from './utils';
1213

1314

1415
/**
1516
* Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277
1617
*/
1718
export function updateNGSWConfig(): Rule {
18-
return (tree, context) => {
19+
return (tree, { logger }) => {
1920
const workspace = getWorkspace(tree);
20-
const logger = context.logger;
2121

2222
for (const { target } of getTargets(workspace, 'build', Builders.Browser)) {
2323
for (const options of getAllOptions(target)) {
@@ -27,46 +27,41 @@ export function updateNGSWConfig(): Rule {
2727
}
2828

2929
const path = ngswConfigPath.value;
30-
const ngswConfigAst = readJsonFileAsAstObject(tree, path);
31-
if (!ngswConfigAst || ngswConfigAst.kind !== 'object') {
30+
let ngswConfigJson;
31+
try {
32+
ngswConfigJson = new JSONFile(tree, path);
33+
} catch {
3234
logger.warn(`Cannot find file: ${ngswConfigPath}`);
3335
continue;
3436
}
3537

36-
const assetGroups = findPropertyInAstObject(ngswConfigAst, 'assetGroups');
37-
if (!assetGroups || assetGroups.kind !== 'array') {
38+
const assetGroups = ngswConfigJson.get(['assetGroups']);
39+
if (!assetGroups || !Array.isArray(assetGroups)) {
3840
continue;
3941
}
4042

41-
const prefetchElement = assetGroups.elements.find(element => {
42-
const installMode = element.kind === 'object' && findPropertyInAstObject(element, 'installMode');
43+
const prefetchElementIndex = assetGroups.findIndex(
44+
(element) => element?.installMode === 'prefetch',
45+
);
4346

44-
return installMode && installMode.value === 'prefetch';
45-
});
46-
47-
if (!prefetchElement || prefetchElement.kind !== 'object') {
48-
continue;
49-
}
50-
51-
const resources = findPropertyInAstObject(prefetchElement, 'resources');
52-
if (!resources || resources.kind !== 'object') {
47+
if (prefetchElementIndex === -1) {
5348
continue;
5449
}
5550

56-
const files = findPropertyInAstObject(resources, 'files');
57-
if (!files || files.kind !== 'array') {
51+
const filesPath = ['assetGroups', prefetchElementIndex, 'resources', 'files'];
52+
const files = ngswConfigJson.get(filesPath);
53+
if (!files || !Array.isArray(files)) {
5854
continue;
5955
}
6056

61-
const hasManifest = files.elements
62-
.some(({ value }) => typeof value === 'string' && value.endsWith('manifest.webmanifest'));
57+
const hasManifest = files
58+
.some((value) => typeof value === 'string' && value.endsWith('manifest.webmanifest'));
6359
if (hasManifest) {
6460
continue;
6561
}
6662

67-
const recorder = tree.beginUpdate(path);
68-
appendValueInAstArray(recorder, files, '/manifest.webmanifest', 10);
69-
tree.commitUpdate(recorder);
63+
// Append to files array
64+
ngswConfigJson.modify([...filesPath, -1], '/manifest.webmanifest');
7065
}
7166
}
7267

0 commit comments

Comments
 (0)