@@ -21,6 +21,7 @@ const colorsUtil = require("./util/colors");
2121const optionsUtil = require ( "./util/options" ) ;
2222const mkdirp = require ( "./util/mkdirp" ) ;
2323const EOL = process . platform === "win32" ? "\r\n" : "\n" ;
24+ const SEP = process . platform === "win32" ? "\\" : "/" ;
2425
2526// global.Binaryen = require("../lib/binaryen");
2627
@@ -229,6 +230,10 @@ exports.main = function main(argv, options, callback) {
229230 // Begin parsing
230231 var parser = null ;
231232
233+ // Maps package names to parent directory
234+ var packages = new Map ( ) ;
235+ var importPathMap = new Map ( ) ;
236+
232237 // Include library files
233238 Object . keys ( exports . libraryFiles ) . forEach ( libPath => {
234239 if ( libPath . indexOf ( "/" ) >= 0 ) return ; // in sub-directory: imported on demand
@@ -254,13 +259,14 @@ exports.main = function main(argv, options, callback) {
254259 libFiles = [ path . basename ( libDir ) ] ;
255260 libDir = path . dirname ( libDir ) ;
256261 } else {
257- libFiles = listFiles ( libDir ) ;
262+ libFiles = listFiles ( libDir ) || [ ] ;
258263 }
259264 for ( let j = 0 , l = libFiles . length ; j < l ; ++ j ) {
260265 let libPath = libFiles [ j ] ;
261266 let libText = readFile ( libPath , libDir ) ;
262267 if ( libText === null ) return callback ( Error ( "Library file '" + libPath + "' not found." ) ) ;
263268 stats . parseCount ++ ;
269+ exports . libraryFiles [ libPath . replace ( / \. t s $ / , "" ) ] = libText ;
264270 stats . parseTime += measure ( ( ) => {
265271 parser = assemblyscript . parseFile (
266272 libText ,
@@ -272,13 +278,32 @@ exports.main = function main(argv, options, callback) {
272278 }
273279 }
274280 }
281+ args . path = args . path || [ ] ;
282+ // Find all valid node_module paths starting at baseDir
283+ function nodePaths ( basePath , _path ) {
284+ return basePath . split ( SEP )
285+ . map ( ( _ , i , arr ) => {
286+ let dir = arr . slice ( 0 , i + 1 ) . join ( SEP ) || SEP ;
287+ let dirFrom = path . relative ( baseDir , dir ) ;
288+ return path . join ( dirFrom , _path ) ;
289+ } )
290+ . filter ( dir => listFiles ( dir , baseDir ) )
291+ . reverse ( ) ;
292+ }
293+ function getPaths ( basePath ) {
294+ let paths = args . path . map ( p => nodePaths ( basePath , p ) ) ;
295+ return nodePaths ( basePath , "node_modules" ) . concat ( ...paths )
296+ }
275297
276298 // Parses the backlog of imported files after including entry files
277299 function parseBacklog ( ) {
278- var sourcePath , sourceText ;
300+ var sourcePath , sourceText , sysPath ;
301+ // dependee is the path of the file that depends on sourcePath
279302 while ( ( sourcePath = parser . nextFile ( ) ) != null ) {
303+ dependee = importPathMap . get ( assemblyscript . getDependee ( parser , sourcePath ) ) || baseDir ;
280304 sourceText = null ;
281-
305+ sysPath = null ;
306+
282307 // Load library file if explicitly requested
283308 if ( sourcePath . startsWith ( exports . libraryPrefix ) ) {
284309 const plainName = sourcePath . substring ( exports . libraryPrefix . length ) ;
@@ -294,11 +319,13 @@ exports.main = function main(argv, options, callback) {
294319 sourceText = readFile ( plainName + ".ts" , customLibDirs [ i ] ) ;
295320 if ( sourceText !== null ) {
296321 sourcePath = exports . libraryPrefix + plainName + ".ts" ;
322+ sysPath = path . join ( customLibDirs [ i ] , plainName + ".ts" ) ;
297323 break ;
298324 } else {
299325 sourceText = readFile ( indexName + ".ts" , customLibDirs [ i ] ) ;
300326 if ( sourceText !== null ) {
301327 sourcePath = exports . libraryPrefix + indexName + ".ts" ;
328+ sysPath = path . join ( customLibDirs [ i ] , indexName + ".ts" ) ;
302329 break ;
303330 }
304331 }
@@ -329,11 +356,13 @@ exports.main = function main(argv, options, callback) {
329356 sourceText = readFile ( plainName + ".ts" , dir ) ;
330357 if ( sourceText !== null ) {
331358 sourcePath = exports . libraryPrefix + plainName + ".ts" ;
359+ sysPath = path . join ( dir , plainName + ".ts" ) ;
332360 break ;
333361 } else {
334362 sourceText = readFile ( indexName + ".ts" , dir ) ;
335363 if ( sourceText !== null ) {
336364 sourcePath = exports . libraryPrefix + indexName + ".ts" ;
365+ sysPath = path . join ( dir , indexName + ".ts" ) ;
337366 break ;
338367 }
339368 }
@@ -342,9 +371,77 @@ exports.main = function main(argv, options, callback) {
342371 }
343372 }
344373 }
374+ /*
375+ In this case the library wasn't found so we check paths
376+ */
377+ if ( sourceText == null ) {
378+ if ( args . traceResolution ) {
379+ stderr . write ( "Looking for " + sourcePath + " imported by " + dependee + " " + EOL ) ;
380+ }
381+ paths = getPaths ( path . join ( baseDir , dependee ) ) ;
382+ let _package = sourcePath . replace ( / \~ l i b \/ ( [ ^ \/ ] * ) .* / , "$1" ) ;
383+ for ( let _path of paths ) {
384+ let ascMain = ( ( ) => {
385+ if ( packages . has ( _package ) ) {
386+ return packages . get ( _package ) ;
387+ }
388+ let p = path . join ( _path , _package , "package.json" ) ;
389+ let res = readFile ( p , baseDir ) ;
390+ if ( res ) {
391+ let package_json ;
392+ try {
393+ package_json = JSON . parse ( res ) ;
394+ } catch ( e ) {
395+ return callback ( Error ( "Parsing " + p + " failed" ) ) ;
396+ }
397+ let mainFile = package_json . ascMain ;
398+ if ( mainFile && ( typeof mainFile === 'string' ) ) {
399+ let newPackage = mainFile . replace ( / ( .* ) \/ i n d e x \. t s / , '$1' ) ;
400+ packages . set ( _package , newPackage ) ;
401+ return newPackage ;
402+ }
403+ }
404+ return "assembly" ;
405+ } ) ( )
406+ let realPath = ( _p ) => {
407+ if ( _p . startsWith ( exports . libraryPrefix ) ) {
408+ _p = _p . substring ( exports . libraryPrefix . length ) ;
409+ }
410+ let first = _p . substring ( 0 , _p . indexOf ( "/" ) ) ;
411+ let second = _p . substring ( _p . indexOf ( "/" ) + 1 ) ;
412+ return path . join ( _path , first , ascMain , second ) ;
413+ }
414+ if ( args . traceResolution ) {
415+ stderr . write ( " in " + realPath ( sourcePath ) ) ;
416+ }
417+ const plainName = sourcePath ;
418+ const indexName = sourcePath + "/index" ;
419+ sourceText = readFile ( realPath ( plainName ) + ".ts" , baseDir ) ;
420+ if ( sourceText !== null ) {
421+ sourcePath = plainName + ".ts" ;
422+ } else {
423+ sourceText = readFile ( realPath ( indexName ) + ".ts" , baseDir ) ;
424+ if ( sourceText !== null ) {
425+ sourcePath = indexName + ".ts" ;
426+ }
427+ }
428+ if ( sourceText !== null ) {
429+ if ( args . traceResolution ) {
430+ stderr . write ( "\nFound at " + realPath ( sourcePath ) + EOL ) ;
431+ }
432+ let newPath = path . join ( _path , _package ) ;
433+ sysPath = newPath ;
434+ break ;
435+ }
436+ if ( args . traceResolution ) {
437+ stderr . write ( EOL ) ;
438+ }
439+ }
440+ }
345441 if ( sourceText == null ) {
346442 return callback ( Error ( "Import file '" + sourcePath + ".ts' not found." ) ) ;
347443 }
444+ importPathMap . set ( sourcePath . replace ( / \. t s $ / , "" ) , sysPath ) ;
348445 stats . parseCount ++ ;
349446 stats . parseTime += measure ( ( ) => {
350447 assemblyscript . parseFile ( sourceText , sourcePath , false , parser ) ;
@@ -418,6 +515,12 @@ exports.main = function main(argv, options, callback) {
418515 // Finish parsing
419516 const program = assemblyscript . finishParsing ( parser ) ;
420517
518+ // Print files and exit if listFiles
519+ if ( args . listFiles ) {
520+ stderr . write ( program . sources . map ( s => s . normalizedPath ) . sort ( ) . join ( EOL ) + EOL ) ;
521+ return callback ( null ) ;
522+ }
523+
421524 // Set up optimization levels
422525 var optimizeLevel = 0 ;
423526 var shrinkLevel = 0 ;
@@ -718,11 +821,12 @@ exports.main = function main(argv, options, callback) {
718821 return callback ( null ) ;
719822
720823 function readFileNode ( filename , baseDir ) {
824+ let name = path . resolve ( baseDir , filename ) ;
721825 try {
722826 let text ;
723827 stats . readCount ++ ;
724828 stats . readTime += measure ( ( ) => {
725- text = fs . readFileSync ( path . join ( baseDir , filename ) , { encoding : "utf8" } ) ;
829+ text = fs . readFileSync ( name , { encoding : "utf8" } ) ;
726830 } ) ;
727831 return text ;
728832 } catch ( e ) {
@@ -755,7 +859,7 @@ exports.main = function main(argv, options, callback) {
755859 } ) ;
756860 return files ;
757861 } catch ( e ) {
758- return [ ] ;
862+ return null ;
759863 }
760864 }
761865
0 commit comments