@@ -2,25 +2,38 @@ import * as fs from 'fs';
22import * as path from 'path' ;
33const SilentError = require ( 'silent-error' ) ;
44
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+ */
514export default function findParentModule ( project : any , currentDir : string ) : string {
6- const sourceRoot = path . join ( project . root , project . ngConfig . apps [ 0 ] . root , 'app' ) ;
715
816 // trim currentDir
9- currentDir = currentDir . replace ( path . join ( project . ngConfig . apps [ 0 ] . root , 'app' ) , '' ) ;
17+ currentDir = currentDir . replace ( project . root , '' ) ;
1018
11- let pathToCheck = path . join ( sourceRoot , currentDir ) ;
19+ // normalize to project.root
20+ let pathToCheck = path . join ( project . root , currentDir ) ;
1221
13- while ( pathToCheck . length >= sourceRoot . length ) {
22+ function isModule ( fileName : string ) {
1423 // 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 ) ;
1931
2032 if ( files . length === 1 ) {
2133 return path . join ( pathToCheck , files [ 0 ] ) ;
2234 } 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 , '' ) } ` ) ;
2437 }
2538
2639 // move to parent directory
0 commit comments