@@ -65,8 +65,20 @@ module specification, the '.js' suffix is optional.
6565 var File = java . io . File ,
6666 FileReader = java . io . FileReader ,
6767 BufferedReader = java . io . BufferedReader ;
68+
69+ function fileExists ( file ) {
70+ if ( file . isDirectory ( ) ) {
71+ return readModuleFromDirectory ( file ) ;
72+ } else {
73+ return file ;
74+ }
75+ }
76+
77+ function _canonize ( file ) {
78+ return "" + file . canonicalPath . replaceAll ( "\\\\" , "/" ) ;
79+ }
6880
69- var readModuleFromDirectory = function ( dir ) {
81+ function readModuleFromDirectory ( dir ) {
7082
7183 // look for a package.json file
7284 var pkgJsonFile = new File ( dir , './package.json' ) ;
@@ -87,19 +99,8 @@ module specification, the '.js' suffix is optional.
8799 return null ;
88100 }
89101 }
90- } ;
91-
92- var fileExists = function ( file ) {
93- if ( file . isDirectory ( ) ) {
94- return readModuleFromDirectory ( file ) ;
95- } else {
96- return file ;
97- }
98- } ;
102+ }
99103
100- var _canonize = function ( file ) {
101- return "" + file . canonicalPath . replaceAll ( "\\\\" , "/" ) ;
102- } ;
103104
104105/**********************************************************************
105106### module name resolution
@@ -135,7 +136,7 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
135136 3.2 if no package.json file exists then look for an index.js file in the directory
136137
137138***/
138- var resolveModuleToFile = function ( moduleName , parentDir ) {
139+ function resolveModuleToFile ( moduleName , parentDir ) {
139140 var file = new File ( moduleName ) ,
140141 i = 0 ,
141142 pathWithJSExt ,
@@ -179,13 +180,11 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
179180 }
180181 }
181182 return null ;
182- } ;
183- var _loadedModules = { } ;
184- var _format = java . lang . String . format ;
183+ }
185184 /*
186185 require() function implementation
187186 */
188- var _require = function ( parentFile , path , options ) {
187+ function _require ( parentFile , path , options ) {
189188 var file ,
190189 canonizedFilename ,
191190 moduleInfo ,
@@ -209,6 +208,29 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
209208 if ( ! ( ( '' + path ) . match ( / ^ \. / ) ) ) {
210209 errMsg = errMsg + ' and not found in paths ' + JSON . stringify ( modulePaths ) ;
211210 }
211+ var find = _require ( parentFile , 'find' ) . exports ;
212+ var allJS = [ ] ;
213+ for ( var i = 0 ; i < modulePaths . length ; i ++ ) {
214+ var js = find ( modulePaths [ i ] ) ;
215+ for ( var j = 0 ; j < js . length ; j ++ ) {
216+ if ( js [ j ] . match ( / \. j s $ / ) ) {
217+ allJS . push ( js [ j ] . replace ( modulePaths [ i ] , '' ) ) ;
218+ }
219+ }
220+ }
221+ var pathL = path . toLowerCase ( ) ;
222+ var candidates = [ ] ;
223+ for ( i = 0 ; i < allJS . length ; i ++ ) {
224+ var filenameparts = allJS [ i ] ;
225+ var candidate = filenameparts . replace ( / \. j s / , '' ) ;
226+ var lastpart = candidate . toLowerCase ( ) ;
227+ if ( pathL . indexOf ( lastpart ) > - 1 || lastpart . indexOf ( pathL ) > - 1 ) {
228+ candidates . push ( candidate ) ;
229+ }
230+ }
231+ if ( candidates . length > 0 ) {
232+ errMsg += '\nBut found module/s named: ' + candidates . join ( ',' ) + ' - is this what you meant?' ;
233+ }
212234 throw new Error ( errMsg ) ;
213235 }
214236 canonizedFilename = _canonize ( file ) ;
@@ -285,14 +307,16 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
285307 }
286308 moduleInfo . loaded = true ;
287309 return moduleInfo ;
288- } ;
310+ }
289311
290- var _requireClosure = function ( parent ) {
291- return function ( path , options ) {
312+ function _requireClosure ( parent ) {
313+ return function requireBoundToParent ( path , options ) {
292314 var module = _require ( parent , path , options ) ;
293315 return module . exports ;
294316 } ;
295- } ;
317+ }
318+ var _loadedModules = { } ;
319+ var _format = java . lang . String . format ;
296320 return _requireClosure ( new java . io . File ( rootDir ) ) ;
297321 // last line deliberately has no semicolon!
298322} )
0 commit comments