6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { normalize , Path , PathIsDirectoryException } from '@angular-devkit/core' ;
9
+ import { basename , dirname , normalize , NormalizedRoot , Path } from '@angular-devkit/core' ;
10
10
import { Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
11
11
import { DirectoryEntry , FileSystem } from '../update-tool/file-system' ;
12
12
import * as path from 'path' ;
@@ -43,17 +43,9 @@ export class DevkitFileSystem extends FileSystem {
43
43
}
44
44
45
45
exists ( fileOrDirPath : Path ) {
46
- // The devkit tree does not expose an API for checking whether a given
47
- // directory exists. It throws a specific error though if a directory
48
- // is being read as a file. We use that to check if a directory exists.
49
- try {
50
- return this . _tree . get ( fileOrDirPath ) !== null ;
51
- } catch ( e ) {
52
- if ( e instanceof PathIsDirectoryException ) {
53
- return true ;
54
- }
55
- }
56
- return false ;
46
+ // We need to check for both file or directory existence, in order
47
+ // to comply with the expectation from the TypeScript compiler.
48
+ return this . _tree . exists ( fileOrDirPath ) || this . _isExistingDirectory ( fileOrDirPath ) ;
57
49
}
58
50
59
51
overwrite ( filePath : Path , content : string ) {
@@ -77,4 +69,23 @@ export class DevkitFileSystem extends FileSystem {
77
69
const { subdirs : directories , subfiles : files } = this . _tree . getDir ( dirPath ) ;
78
70
return { directories, files} ;
79
71
}
72
+
73
+ private _isExistingDirectory ( dirPath : Path ) {
74
+ if ( dirPath === NormalizedRoot ) {
75
+ return true ;
76
+ }
77
+
78
+ const parent = dirname ( dirPath ) ;
79
+ const dirName = basename ( dirPath ) ;
80
+ // TypeScript also checks potential entry points, so e.g. importing
81
+ // package.json will result in a lookup of /package.json/package.json
82
+ // and /package.json/index.ts. In order to avoid failure, we check if
83
+ // the parent is an existing file and return false, if that is the case.
84
+ if ( this . _tree . exists ( parent ) ) {
85
+ return false ;
86
+ }
87
+
88
+ const dir = this . _tree . getDir ( parent ) ;
89
+ return dir . subdirs . indexOf ( dirName ) !== - 1 ;
90
+ }
80
91
}
0 commit comments