6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { BaseException , Path , join , normalize , virtualFs } from '@angular-devkit/core' ;
10
- import { Observable , forkJoin , of } from 'rxjs' ;
11
- import { map , tap } from 'rxjs/operators' ;
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' ;
12
19
import {
13
20
CurrentFileReplacement ,
14
21
DeprecatedFileReplacment ,
@@ -22,53 +29,64 @@ export class MissingFileReplacementException extends BaseException {
22
29
}
23
30
}
24
31
25
- // Note: This method changes the file replacements in place.
26
- export function addFileReplacements (
27
- root : Path ,
28
- host : virtualFs . AliasHost ,
29
- fileReplacements : FileReplacement [ ] ,
30
- ) : Observable < null > {
32
+ export interface NormalizedFileReplacement {
33
+ replace : Path ;
34
+ with : Path ;
35
+ }
31
36
37
+ export function normalizeFileReplacements (
38
+ fileReplacements : FileReplacement [ ] ,
39
+ host : virtualFs . Host ,
40
+ root : Path ,
41
+ ) : Observable < NormalizedFileReplacement [ ] > {
32
42
if ( fileReplacements . length === 0 ) {
33
- return of ( null ) ;
34
- }
35
-
36
- // Normalize the legacy format into the current one.
37
- for ( const fileReplacement of fileReplacements ) {
38
- const currentFormat = fileReplacement as CurrentFileReplacement ;
39
- const maybeOldFormat = fileReplacement as DeprecatedFileReplacment ;
40
-
41
- if ( maybeOldFormat . src && maybeOldFormat . replaceWith ) {
42
- currentFormat . replace = maybeOldFormat . src ;
43
- currentFormat . with = maybeOldFormat . replaceWith ;
44
- }
43
+ return of ( [ ] ) ;
45
44
}
46
45
47
- const normalizedFileReplacements = fileReplacements as CurrentFileReplacement [ ] ;
48
-
49
46
// Ensure all the replacements exist.
50
- const errorOnFalse = ( path : string ) => tap ( ( exists : boolean ) => {
47
+ const errorOnFalse = ( path : Path ) => tap ( ( exists : boolean ) => {
51
48
if ( ! exists ) {
52
- throw new MissingFileReplacementException ( path ) ;
49
+ throw new MissingFileReplacementException ( getSystemPath ( path ) ) ;
53
50
}
54
51
} ) ;
55
52
56
- const existObservables = normalizedFileReplacements
57
- . map ( replacement => [
58
- host . exists ( join ( root , replacement . replace ) ) . pipe ( errorOnFalse ( replacement . replace ) ) ,
59
- host . exists ( join ( root , replacement . with ) ) . pipe ( errorOnFalse ( replacement . with ) ) ,
60
- ] )
61
- . reduce ( ( prev , curr ) => prev . concat ( curr ) , [ ] ) ;
62
-
63
- return forkJoin ( existObservables ) . pipe (
64
- tap ( ( ) => {
65
- normalizedFileReplacements . forEach ( replacement => {
66
- host . aliases . set (
67
- join ( root , normalize ( replacement . replace ) ) ,
68
- join ( root , normalize ( replacement . with ) ) ,
69
- ) ;
70
- } ) ;
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
+ ) ;
71
61
} ) ,
72
- map ( ( ) => null ) ,
62
+ toArray ( ) ,
73
63
) ;
74
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