66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { normalize } from '@angular-devkit/core' ;
9+ import { normalize , Path , relative } from '@angular-devkit/core' ;
1010import { Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
11- import { relative } from 'path' ;
11+ import * as path from 'path' ;
1212import { FileSystem } from '../update-tool/file-system' ;
1313
1414/**
1515 * File system that leverages the virtual tree from the CLI devkit. This file
1616 * system is commonly used by `ng update` migrations that run as part of the
1717 * Angular CLI.
1818 */
19- export class DevkitFileSystem implements FileSystem {
19+ export class DevkitFileSystem extends FileSystem < Path > {
2020 private _updateRecorderCache = new Map < string , UpdateRecorder > ( ) ;
21+ private _workspaceFsPath : Path ;
2122
22- constructor ( private _tree : Tree , private _workspaceFsPath : string ) { }
23+ constructor ( private _tree : Tree , workspaceFsPath : string ) {
24+ super ( ) ;
25+ this . _workspaceFsPath = normalize ( workspaceFsPath ) ;
26+ }
2327
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 ;
2640 }
2741
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 ) ! ;
3245 }
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 ) ;
3548 return recorder ;
3649 }
3750
@@ -40,24 +53,24 @@ export class DevkitFileSystem implements FileSystem {
4053 this . _updateRecorderCache . clear ( ) ;
4154 }
4255
43- exists ( fsFilePath : string ) {
44- return this . _tree . exists ( this . resolve ( fsFilePath ) ) ;
56+ exists ( filePath : Path ) {
57+ return this . _tree . exists ( filePath ) ;
4558 }
4659
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 ) ;
4962 }
5063
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 ) ;
5366 }
5467
55- delete ( fsFilePath : string ) {
56- this . _tree . delete ( this . resolve ( fsFilePath ) ) ;
68+ delete ( filePath : Path ) {
69+ this . _tree . delete ( filePath ) ;
5770 }
5871
59- read ( fsFilePath : string ) {
60- const buffer = this . _tree . read ( this . resolve ( fsFilePath ) ) ;
72+ read ( filePath : Path ) {
73+ const buffer = this . _tree . read ( filePath ) ;
6174 return buffer !== null ? buffer . toString ( ) : null ;
6275 }
6376}
0 commit comments