@@ -8,6 +8,10 @@ const compareTitle = (original: string, test: string) => {
88 return false ;
99} ;
1010
11+ const throwError = ( path : string ) => {
12+ throw new Error ( `Cannot find module in ${ path } ` ) ;
13+ } ;
14+
1115/**
1216 * Convert the module path to a module
1317 */
@@ -19,40 +23,38 @@ export default (
1923) => {
2024 // Split path
2125 const splitPath = path . replace ( / ^ .\/ / , '' ) . split ( '/' ) ;
22- const founddirectoryShortid = splitPath . reduce (
26+ const foundDirectoryShortid = splitPath . reduce (
2327 ( dirId : ?string , pathPart : string , i : number ) => {
2428 // Meaning this is the last argument, so the file
2529 if ( i === splitPath . length - 1 ) return dirId ;
2630
2731 if ( pathPart === '..' ) {
2832 // Find the parent
2933 const dir = directories . find ( d => d . shortid === dirId ) ;
30- if ( dir == null ) throw new Error ( `Cannot find module in ${ path } ` ) ;
34+ if ( dir == null ) throwError ( path ) ;
3135
3236 return dir . directoryShortid ;
3337 }
3438
35- // For == check on null
36- // eslint-disable-next-line eqeqeq
3739 const directoriesInDirectory = directories . filter (
40+ // eslint-disable-next-line eqeqeq
3841 m => m . directoryShortid == dirId ,
3942 ) ;
4043 const nextDirectory = directoriesInDirectory . find ( d =>
4144 compareTitle ( d . title , pathPart ) ,
4245 ) ;
4346
44- if ( nextDirectory == null )
45- throw new Error ( `Cannot find module in ${ path } ` ) ;
47+ if ( nextDirectory == null ) throwError ( path ) ;
4648
4749 return nextDirectory . shortid ;
4850 } ,
4951 startdirectoryShortid ,
5052 ) ;
5153
5254 const lastPath = splitPath [ splitPath . length - 1 ] ;
53- // eslint-disable-next-line eqeqeq
5455 const modulesInFoundDirectory = modules . filter (
55- m => m . directoryShortid == founddirectoryShortid ,
56+ // eslint-disable-next-line eqeqeq
57+ m => m . directoryShortid == foundDirectoryShortid ,
5658 ) ;
5759
5860 // Find module with same name
@@ -61,22 +63,25 @@ export default (
6163 ) ;
6264 if ( foundModule ) return foundModule ;
6365
64- // eslint-disable-next-line eqeqeq
66+ // Check all directories in said directory for same name
6567 const directoriesInFoundDirectory = directories . filter (
66- m => m . directoryShortid == founddirectoryShortid ,
68+ // eslint-disable-next-line eqeqeq
69+ m => m . directoryShortid == foundDirectoryShortid ,
6770 ) ;
6871 const foundDirectory = directoriesInFoundDirectory . find ( m =>
6972 compareTitle ( m . title , lastPath ) ,
7073 ) ;
7174
75+ // If it refers to a directory
7276 if ( foundDirectory ) {
73- // eslint-disable-next-line eqeqeq
77+ // Find module named index
7478 const indexModule = modules . find (
7579 m =>
80+ // eslint-disable-next-line eqeqeq
7681 m . directoryShortid == foundDirectory . shortid &&
7782 compareTitle ( m . title , 'index' ) ,
7883 ) ;
79- if ( indexModule == null ) throw new Error ( `Cannot find module in ${ path } ` ) ;
84+ if ( indexModule == null ) throwError ( path ) ;
8085 return indexModule ;
8186 }
8287
@@ -87,5 +92,6 @@ export default (
8792 ) ;
8893 if ( indexModule ) return indexModule ;
8994 }
90- throw new Error ( `Cannot find module in ${ path } ` ) ;
95+
96+ throwError ( path ) ;
9197} ;
0 commit comments