|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + BaseException, |
| 11 | + Path, |
| 12 | + getSystemPath, |
| 13 | + join, |
| 14 | + normalize, |
| 15 | + virtualFs, |
| 16 | +} from '@angular-devkit/core'; |
| 17 | +import { Observable, from, of } from 'rxjs'; |
| 18 | +import { concat, concatMap, ignoreElements, map, mergeMap, tap, toArray } from 'rxjs/operators'; |
| 19 | +import { |
| 20 | + CurrentFileReplacement, |
| 21 | + DeprecatedFileReplacment, |
| 22 | + FileReplacement, |
| 23 | +} from '../browser/schema'; |
| 24 | + |
| 25 | + |
| 26 | +export class MissingFileReplacementException extends BaseException { |
| 27 | + constructor(path: String) { |
| 28 | + super(`The ${path} path in file replacements does not exist.`); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export interface NormalizedFileReplacement { |
| 33 | + replace: Path; |
| 34 | + with: Path; |
| 35 | +} |
| 36 | + |
| 37 | +export function normalizeFileReplacements( |
| 38 | + fileReplacements: FileReplacement[], |
| 39 | + host: virtualFs.Host, |
| 40 | + root: Path, |
| 41 | +): Observable<NormalizedFileReplacement[]> { |
| 42 | + if (fileReplacements.length === 0) { |
| 43 | + return of([]); |
| 44 | + } |
| 45 | + |
| 46 | + // Ensure all the replacements exist. |
| 47 | + const errorOnFalse = (path: Path) => tap((exists: boolean) => { |
| 48 | + if (!exists) { |
| 49 | + throw new MissingFileReplacementException(getSystemPath(path)); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + return from(fileReplacements).pipe( |
| 54 | + map(replacement => normalizeFileReplacement(replacement, root)), |
| 55 | + concatMap(normalized => { |
| 56 | + return from([normalized.replace, normalized.with]).pipe( |
| 57 | + mergeMap(path => host.exists(path).pipe(errorOnFalse(path))), |
| 58 | + ignoreElements(), |
| 59 | + concat(of(normalized)), |
| 60 | + ); |
| 61 | + }), |
| 62 | + toArray(), |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +function normalizeFileReplacement( |
| 67 | + fileReplacement: FileReplacement, |
| 68 | + root?: Path, |
| 69 | +): NormalizedFileReplacement { |
| 70 | + const currentFormat = fileReplacement as CurrentFileReplacement; |
| 71 | + const maybeOldFormat = fileReplacement as DeprecatedFileReplacment; |
| 72 | + |
| 73 | + let replacePath: Path; |
| 74 | + let withPath: Path; |
| 75 | + if (maybeOldFormat.src && maybeOldFormat.replaceWith) { |
| 76 | + replacePath = normalize(maybeOldFormat.src); |
| 77 | + withPath = normalize(maybeOldFormat.replaceWith); |
| 78 | + } else { |
| 79 | + replacePath = normalize(currentFormat.replace); |
| 80 | + withPath = normalize(currentFormat.with); |
| 81 | + } |
| 82 | + |
| 83 | + // TODO: For 7.x should this only happen if not absolute? |
| 84 | + if (root) { |
| 85 | + replacePath = join(root, replacePath); |
| 86 | + } |
| 87 | + if (root) { |
| 88 | + withPath = join(root, withPath); |
| 89 | + } |
| 90 | + |
| 91 | + return { replace: replacePath, with: withPath }; |
| 92 | +} |
0 commit comments