6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { normalize } from '@angular-devkit/core' ;
9
+ import { normalize , Path , relative } from '@angular-devkit/core' ;
10
10
import { Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
11
- import { relative } from 'path' ;
11
+ import * as path from 'path' ;
12
12
import { FileSystem } from '../update-tool/file-system' ;
13
13
14
14
/**
15
15
* File system that leverages the virtual tree from the CLI devkit. This file
16
16
* system is commonly used by `ng update` migrations that run as part of the
17
17
* Angular CLI.
18
18
*/
19
- export class DevkitFileSystem implements FileSystem {
19
+ export class DevkitFileSystem extends FileSystem < Path > {
20
20
private _updateRecorderCache = new Map < string , UpdateRecorder > ( ) ;
21
+ private _workspaceFsPath : Path ;
21
22
22
- constructor ( private _tree : Tree , private _workspaceFsPath : string ) { }
23
+ constructor ( private _tree : Tree , workspaceFsPath : string ) {
24
+ super ( ) ;
25
+ this . _workspaceFsPath = normalize ( workspaceFsPath ) ;
26
+ }
23
27
24
- resolve ( fsFilePath : string ) {
25
- return normalize ( relative ( this . _workspaceFsPath , fsFilePath ) ) as string ;
28
+ resolve ( ...segments : string [ ] ) : Path {
29
+ // Note: We use `posix.resolve` as the devkit paths are using posix separators.
30
+ const resolvedPath = normalize ( path . posix . resolve ( ...segments . map ( normalize ) ) ) ;
31
+ // If the resolved path points to the workspace root, then this is an absolute disk
32
+ // path and we need to compute a devkit tree relative path.
33
+ if ( resolvedPath . startsWith ( this . _workspaceFsPath ) ) {
34
+ return relative ( this . _workspaceFsPath , resolvedPath ) ;
35
+ }
36
+ // Otherwise we know that the path is absolute (due to the resolve), and that it
37
+ // refers to an absolute devkit tree path (like `/angular.json`). We keep those
38
+ // unmodified as they are already resolved workspace paths.
39
+ return resolvedPath ;
26
40
}
27
41
28
- edit ( fsFilePath : string ) {
29
- const treeFilePath = this . resolve ( fsFilePath ) ;
30
- if ( this . _updateRecorderCache . has ( treeFilePath ) ) {
31
- return this . _updateRecorderCache . get ( treeFilePath ) ! ;
42
+ edit ( filePath : Path ) {
43
+ if ( this . _updateRecorderCache . has ( filePath ) ) {
44
+ return this . _updateRecorderCache . get ( filePath ) ! ;
32
45
}
33
- const recorder = this . _tree . beginUpdate ( treeFilePath ) ;
34
- this . _updateRecorderCache . set ( treeFilePath , recorder ) ;
46
+ const recorder = this . _tree . beginUpdate ( filePath ) ;
47
+ this . _updateRecorderCache . set ( filePath , recorder ) ;
35
48
return recorder ;
36
49
}
37
50
@@ -40,24 +53,24 @@ export class DevkitFileSystem implements FileSystem {
40
53
this . _updateRecorderCache . clear ( ) ;
41
54
}
42
55
43
- exists ( fsFilePath : string ) {
44
- return this . _tree . exists ( this . resolve ( fsFilePath ) ) ;
56
+ exists ( filePath : Path ) {
57
+ return this . _tree . exists ( filePath ) ;
45
58
}
46
59
47
- overwrite ( fsFilePath : string , content : string ) {
48
- this . _tree . overwrite ( this . resolve ( fsFilePath ) , content ) ;
60
+ overwrite ( filePath : Path , content : string ) {
61
+ this . _tree . overwrite ( filePath , content ) ;
49
62
}
50
63
51
- create ( fsFilePath : string , content : string ) {
52
- this . _tree . create ( this . resolve ( fsFilePath ) , content ) ;
64
+ create ( filePath : Path , content : string ) {
65
+ this . _tree . create ( filePath , content ) ;
53
66
}
54
67
55
- delete ( fsFilePath : string ) {
56
- this . _tree . delete ( this . resolve ( fsFilePath ) ) ;
68
+ delete ( filePath : Path ) {
69
+ this . _tree . delete ( filePath ) ;
57
70
}
58
71
59
- read ( fsFilePath : string ) {
60
- const buffer = this . _tree . read ( this . resolve ( fsFilePath ) ) ;
72
+ read ( filePath : Path ) {
73
+ const buffer = this . _tree . read ( filePath ) ;
61
74
return buffer !== null ? buffer . toString ( ) : null ;
62
75
}
63
76
}
0 commit comments