refactor(@schematics/angular): use new workspace helpers in ngsw-config migration

This commit is contained in:
Charles Lyding 2020-09-22 15:25:20 -04:00 committed by Filipe Silva
parent 39fd968673
commit c859fcb65e

View File

@ -7,29 +7,30 @@
*/ */
import { Rule } from '@angular-devkit/schematics'; import { Rule } from '@angular-devkit/schematics';
import { JSONFile } from '../../utility/json-file'; import { JSONFile } from '../../utility/json-file';
import { findPropertyInAstObject } from '../../utility/json-utils'; import { allTargetOptions, allWorkspaceTargets, getWorkspace } from '../../utility/workspace';
import { Builders } from '../../utility/workspace-models'; import { Builders } from '../../utility/workspace-models';
import { getAllOptions, getTargets, getWorkspace } from './utils';
/** /**
* Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277 * Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277
*/ */
export function updateNGSWConfig(): Rule { export function updateNGSWConfig(): Rule {
return (tree, { logger }) => { return async (tree, { logger }) => {
const workspace = getWorkspace(tree); const workspace = await getWorkspace(tree);
for (const { target } of getTargets(workspace, 'build', Builders.Browser)) { for (const [targetName, target] of allWorkspaceTargets(workspace)) {
for (const options of getAllOptions(target)) { if (targetName !== 'build' || target.builder !== Builders.Browser) {
const ngswConfigPath = findPropertyInAstObject(options, 'ngswConfigPath'); continue;
if (!ngswConfigPath || ngswConfigPath.kind !== 'string') { }
for (const [, options] of allTargetOptions(target)) {
const ngswConfigPath = options.ngswConfigPath;
if (!ngswConfigPath || typeof ngswConfigPath !== 'string') {
continue; continue;
} }
const path = ngswConfigPath.value;
let ngswConfigJson; let ngswConfigJson;
try { try {
ngswConfigJson = new JSONFile(tree, path); ngswConfigJson = new JSONFile(tree, ngswConfigPath);
} catch { } catch {
logger.warn(`Cannot find file: ${ngswConfigPath}`); logger.warn(`Cannot find file: ${ngswConfigPath}`);
continue; continue;
@ -54,8 +55,9 @@ export function updateNGSWConfig(): Rule {
continue; continue;
} }
const hasManifest = files const hasManifest = files.some(
.some((value) => typeof value === 'string' && value.endsWith('manifest.webmanifest')); (value) => typeof value === 'string' && value.endsWith('manifest.webmanifest'),
);
if (hasManifest) { if (hasManifest) {
continue; continue;
} }
@ -64,7 +66,5 @@ export function updateNGSWConfig(): Rule {
ngswConfigJson.modify([...filesPath, -1], '/manifest.webmanifest'); ngswConfigJson.modify([...filesPath, -1], '/manifest.webmanifest');
} }
} }
return tree;
}; };
} }