@@ -2,25 +2,38 @@ import * as fs from 'fs';
2
2
import * as path from 'path' ;
3
3
const SilentError = require ( 'silent-error' ) ;
4
4
5
+ /**
6
+ * Find the path to the first module, moving up through the directory tree from
7
+ * currentDir.
8
+ *
9
+ * @export
10
+ * @param {* } project - the project definition.
11
+ * @param {string } currentDir - a relative path from the project root.
12
+ * @returns {string } the relative path to the first module above currentDir from the project root.
13
+ */
5
14
export default function findParentModule ( project : any , currentDir : string ) : string {
6
- const sourceRoot = path . join ( project . root , project . ngConfig . apps [ 0 ] . root , 'app' ) ;
7
15
8
16
// trim currentDir
9
- currentDir = currentDir . replace ( path . join ( project . ngConfig . apps [ 0 ] . root , 'app' ) , '' ) ;
17
+ currentDir = currentDir . replace ( project . root , '' ) ;
10
18
11
- let pathToCheck = path . join ( sourceRoot , currentDir ) ;
19
+ // normalize to project.root
20
+ let pathToCheck = path . join ( project . root , currentDir ) ;
12
21
13
- while ( pathToCheck . length >= sourceRoot . length ) {
22
+ function isModule ( fileName : string ) {
14
23
// TODO: refactor to not be based upon file name
15
- const files = fs . readdirSync ( pathToCheck )
16
- . filter ( fileName => ! fileName . endsWith ( 'routing.module.ts' ) )
17
- . filter ( fileName => fileName . endsWith ( '.module.ts' ) )
18
- . filter ( fileName => fs . statSync ( path . join ( pathToCheck , fileName ) ) . isFile ( ) ) ;
24
+ return ! fileName . endsWith ( 'routing.module.ts' ) &&
25
+ fileName . endsWith ( '.module.ts' ) &&
26
+ fs . statSync ( path . join ( pathToCheck , fileName ) ) . isFile ( ) ;
27
+ }
28
+
29
+ while ( pathToCheck . length >= project . root . length ) {
30
+ const files = fs . readdirSync ( pathToCheck ) . filter ( isModule ) ;
19
31
20
32
if ( files . length === 1 ) {
21
33
return path . join ( pathToCheck , files [ 0 ] ) ;
22
34
} else if ( files . length > 1 ) {
23
- throw new SilentError ( `Multiple module files found: ${ pathToCheck . replace ( sourceRoot , '' ) } ` ) ;
35
+ throw new SilentError ( `Multiple module files found:
36
+ ${ pathToCheck . replace ( project . root , '' ) } ` ) ;
24
37
}
25
38
26
39
// move to parent directory
0 commit comments