66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { normalize , Path , PathIsDirectoryException } from '@angular-devkit/core' ;
9+ import { basename , dirname , normalize , NormalizedRoot , Path } from '@angular-devkit/core' ;
1010import { Tree , UpdateRecorder } from '@angular-devkit/schematics' ;
1111import { DirectoryEntry , FileSystem } from '../update-tool/file-system' ;
1212import * as path from 'path' ;
@@ -43,17 +43,9 @@ export class DevkitFileSystem extends FileSystem {
4343 }
4444
4545 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 ) ;
5749 }
5850
5951 overwrite ( filePath : Path , content : string ) {
@@ -77,4 +69,23 @@ export class DevkitFileSystem extends FileSystem {
7769 const { subdirs : directories , subfiles : files } = this . _tree . getDir ( dirPath ) ;
7870 return { directories, files} ;
7971 }
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+ }
8091}
0 commit comments