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