@@ -96,9 +96,7 @@ exports.compileString = async (sources, options) => {
96
96
if ( typeof sources === "string" ) sources = { "input.ts" : sources } ;
97
97
const output = Object . create ( {
98
98
stdout : createMemoryStream ( ) ,
99
- stderr : createMemoryStream ( ) ,
100
- binary : null ,
101
- text : null
99
+ stderr : createMemoryStream ( )
102
100
} ) ;
103
101
debugger ;
104
102
var argv = [ ] ;
@@ -278,7 +276,7 @@ exports.main = async function main(argv, options, callback) {
278
276
}
279
277
for ( let j = 0 , l = libFiles . length ; j < l ; ++ j ) {
280
278
let libPath = libFiles [ j ] ;
281
- let libText = await readFile ( path . join ( libDir , libPath ) ) ;
279
+ let libText = readFile ( libPath , libDir ) ;
282
280
if ( libText === null ) return callback ( Error ( "Library file '" + libPath + "' not found." ) ) ;
283
281
stats . parseCount ++ ;
284
282
stats . parseTime += measure ( ( ) => {
@@ -293,35 +291,10 @@ exports.main = async function main(argv, options, callback) {
293
291
}
294
292
}
295
293
296
- // Include entry files
297
- for ( let i = 0 , k = argv . length ; i < k ; ++ i ) {
298
- const filename = argv [ i ] ;
299
- if ( filename == "undefined" ) continue ;
300
-
301
- let sourcePath = String ( filename ) . replace ( / \\ / g, "/" ) . replace ( / ( \. t s | \/ ) $ / , "" ) ;
302
-
303
- // Try entryPath.ts, then entryPath/index.ts
304
- let sourceText = await readFile ( path . join ( baseDir , sourcePath ) + ".ts" ) ;
305
- if ( sourceText === null ) {
306
- sourceText = await readFile ( path . join ( baseDir , sourcePath , "index.ts" ) ) ;
307
- if ( sourceText === null ) {
308
- return callback ( Error ( "Entry file '" + sourcePath + ".ts' not found." ) ) ;
309
- } else {
310
- sourcePath += "/index.ts" ;
311
- }
312
- } else {
313
- sourcePath += ".ts" ;
314
- }
315
- debugger ;
316
-
317
- stats . parseCount ++ ;
318
- stats . parseTime += measure ( ( ) => {
319
- parser = assemblyscript . parseFile ( sourceText , sourcePath , true , parser ) ;
320
- } ) ;
321
-
322
- // Process backlog
294
+ // Parses the backlog of imported files after including entry files
295
+ function parseBacklog ( ) {
296
+ var sourcePath , sourceText ;
323
297
while ( ( sourcePath = parser . nextFile ( ) ) != null ) {
324
- let found = false ;
325
298
326
299
// Load library file if explicitly requested
327
300
if ( sourcePath . startsWith ( exports . libraryPrefix ) ) {
@@ -335,13 +308,12 @@ exports.main = async function main(argv, options, callback) {
335
308
sourcePath = exports . libraryPrefix + indexName + ".ts" ;
336
309
} else {
337
310
for ( let i = 0 , k = customLibDirs . length ; i < k ; ++ i ) {
338
- const dir = customLibDirs [ i ] ;
339
- sourceText = await readFile ( path . join ( dir , plainName + ".ts" ) ) ;
311
+ sourceText = readFile ( plainName + ".ts" , customLibDirs [ i ] ) ;
340
312
if ( sourceText !== null ) {
341
313
sourcePath = exports . libraryPrefix + plainName + ".ts" ;
342
314
break ;
343
315
} else {
344
- sourceText = await readFile ( path . join ( dir , indexName + ".ts" ) ) ;
316
+ sourceText = readFile ( indexName + ".ts" , customLibDirs [ i ] ) ;
345
317
if ( sourceText !== null ) {
346
318
sourcePath = exports . libraryPrefix + indexName + ".ts" ;
347
319
break ;
@@ -354,11 +326,11 @@ exports.main = async function main(argv, options, callback) {
354
326
} else {
355
327
const plainName = sourcePath ;
356
328
const indexName = sourcePath + "/index" ;
357
- sourceText = await readFile ( path . join ( baseDir , plainName + ".ts" ) ) ;
329
+ sourceText = readFile ( plainName + ".ts" , baseDir ) ;
358
330
if ( sourceText !== null ) {
359
331
sourcePath = plainName + ".ts" ;
360
332
} else {
361
- sourceText = await readFile ( path . join ( baseDir , indexName + ".ts" ) ) ;
333
+ sourceText = readFile ( indexName + ".ts" , baseDir ) ;
362
334
if ( sourceText !== null ) {
363
335
sourcePath = indexName + ".ts" ;
364
336
} else if ( ! plainName . startsWith ( "." ) ) {
@@ -371,12 +343,12 @@ exports.main = async function main(argv, options, callback) {
371
343
} else {
372
344
for ( let i = 0 , k = customLibDirs . length ; i < k ; ++ i ) {
373
345
const dir = customLibDirs [ i ] ;
374
- sourceText = await readFile ( path . join ( dir , plainName + ".ts" ) ) ;
346
+ sourceText = readFile ( plainName + ".ts" , customLibDirs [ i ] ) ;
375
347
if ( sourceText !== null ) {
376
348
sourcePath = exports . libraryPrefix + plainName + ".ts" ;
377
349
break ;
378
350
} else {
379
- sourceText = await readFile ( path . join ( dir , indexName + ".ts" ) ) ;
351
+ sourceText = readFile ( indexName + ".ts" , customLibDirs [ i ] ) ;
380
352
if ( sourceText !== null ) {
381
353
sourcePath = exports . libraryPrefix + indexName + ".ts" ;
382
354
break ;
@@ -400,7 +372,38 @@ exports.main = async function main(argv, options, callback) {
400
372
}
401
373
}
402
374
375
+ // Include entry files
376
+ for ( let i = 0 , k = argv . length ; i < k ; ++ i ) {
377
+ const filename = argv [ i ] ;
378
+
379
+ let sourcePath = String ( filename ) . replace ( / \\ / g, "/" ) . replace ( / ( \. t s | \/ ) $ / , "" ) ;
380
+
381
+ // Try entryPath.ts, then entryPath/index.ts
382
+ let sourceText = readFile ( sourcePath + ".ts" , baseDir ) ;
383
+ if ( sourceText === null ) {
384
+ sourceText = readFile ( sourcePath + "/index.ts" , baseDir ) ;
385
+ if ( sourceText === null ) {
386
+ return callback ( Error ( "Entry file '" + sourcePath + ".ts' not found." ) ) ;
387
+ } else {
388
+ sourcePath += "/index.ts" ;
389
+ }
390
+ } else {
391
+ sourcePath += ".ts" ;
392
+ }
393
+
394
+ stats . parseCount ++ ;
395
+ stats . parseTime += measure ( ( ) => {
396
+ parser = assemblyscript . parseFile ( sourceText , sourcePath , true , parser ) ;
397
+ } ) ;
398
+ let code = parseBacklog ( ) ;
399
+ if ( code ) return code ;
400
+ }
401
+
403
402
applyTransform ( "afterParse" , parser ) ;
403
+ {
404
+ let code = parseBacklog ( ) ;
405
+ if ( code ) return code ;
406
+ }
404
407
405
408
// Finish parsing
406
409
const program = assemblyscript . finishParsing ( parser ) ;
@@ -578,7 +581,7 @@ exports.main = async function main(argv, options, callback) {
578
581
} ) ;
579
582
580
583
if ( args . binaryFile . length ) {
581
- await writeFile ( path . join ( baseDir , args . binaryFile ) , wasm . output ) ;
584
+ writeFile ( args . binaryFile , wasm . output , baseDir ) ;
582
585
} else {
583
586
writeStdout ( wasm . output ) ;
584
587
hasStdout = true ;
@@ -598,27 +601,23 @@ exports.main = async function main(argv, options, callback) {
598
601
text = exports . libraryFiles [ stdName ] ;
599
602
} else {
600
603
for ( let i = 0 , k = customLibDirs . length ; i < k ; ++ i ) {
601
- text = await readFile ( path . join (
602
- customLibDirs [ i ] ,
603
- name . substring ( exports . libraryPrefix . length ) )
604
- ) ;
604
+ text = readFile ( name . substring ( exports . libraryPrefix . length ) , customLibDirs [ i ] ) ;
605
605
if ( text !== null ) break ;
606
606
}
607
607
}
608
608
} else {
609
- text = await readFile ( path . join ( baseDir , name ) ) ;
609
+ text = readFile ( name , baseDir ) ;
610
610
}
611
611
if ( text === null ) {
612
612
return callback ( Error ( "Source file '" + name + "' not found." ) ) ;
613
613
}
614
614
if ( ! sourceMap . sourceContents ) sourceMap . sourceContents = [ ] ;
615
615
sourceMap . sourceContents [ index ] = text ;
616
616
} ) ;
617
- await writeFile ( path . join (
618
- baseDir ,
617
+ writeFile ( path . join (
619
618
path . dirname ( args . binaryFile ) ,
620
619
path . basename ( sourceMapURL )
621
- ) , JSON . stringify ( sourceMap ) ) ;
620
+ ) . replace ( / ^ \. \/ / , "" ) , JSON . stringify ( sourceMap ) , baseDir ) ;
622
621
} else {
623
622
stderr . write ( "Skipped source map (stdout already occupied)" + EOL ) ;
624
623
}
@@ -633,7 +632,7 @@ exports.main = async function main(argv, options, callback) {
633
632
stats . emitTime += measure ( ( ) => {
634
633
asm = module . toAsmjs ( ) ;
635
634
} ) ;
636
- await writeFile ( path . join ( baseDir , args . asmjsFile ) , asm ) ;
635
+ writeFile ( args . asmjsFile , asm , baseDir ) ;
637
636
} else if ( ! hasStdout ) {
638
637
stats . emitCount ++ ;
639
638
stats . emitTime += measure ( ( ) => {
@@ -653,7 +652,7 @@ exports.main = async function main(argv, options, callback) {
653
652
stats . emitTime += measure ( ( ) => {
654
653
idl = assemblyscript . buildIDL ( program ) ;
655
654
} ) ;
656
- await writeFile ( path . join ( baseDir , args . idlFile ) , idl ) ;
655
+ writeFile ( args . idlFile , idl , baseDir ) ;
657
656
} else if ( ! hasStdout ) {
658
657
stats . emitCount ++ ;
659
658
stats . emitTime += measure ( ( ) => {
@@ -673,7 +672,7 @@ exports.main = async function main(argv, options, callback) {
673
672
stats . emitTime += measure ( ( ) => {
674
673
tsd = assemblyscript . buildTSD ( program ) ;
675
674
} ) ;
676
- await writeFile ( path . join ( baseDir , args . tsdFile ) , tsd ) ;
675
+ writeFile ( args . tsdFile , tsd , baseDir ) ;
677
676
} else if ( ! hasStdout ) {
678
677
stats . emitCount ++ ;
679
678
stats . emitTime += measure ( ( ) => {
@@ -693,7 +692,7 @@ exports.main = async function main(argv, options, callback) {
693
692
stats . emitTime += measure ( ( ) => {
694
693
wat = module . toText ( ) ;
695
694
} ) ;
696
- await writeFile ( path . join ( baseDir , args . textFile ) , wat ) ;
695
+ writeFile ( args . textFile , wat , baseDir ) ;
697
696
} else if ( ! hasStdout ) {
698
697
stats . emitCount ++ ;
699
698
stats . emitTime += measure ( ( ) => {
@@ -710,28 +709,28 @@ exports.main = async function main(argv, options, callback) {
710
709
}
711
710
return callback ( null ) ;
712
711
713
- function readFileNode ( filename ) {
712
+ function readFileNode ( filename , baseDir ) {
714
713
try {
715
714
let text ;
716
715
stats . readCount ++ ;
717
716
stats . readTime += measure ( ( ) => {
718
- text = fs . readFileSync ( filename , { encoding : "utf8" } ) ;
717
+ text = fs . readFileSync ( path . join ( baseDir , filename ) , { encoding : "utf8" } ) ;
719
718
} ) ;
720
719
return text ;
721
720
} catch ( e ) {
722
721
return null ;
723
722
}
724
723
}
725
724
726
- function writeFileNode ( filename , contents ) {
725
+ function writeFileNode ( filename , contents , baseDir ) {
727
726
try {
728
727
stats . writeCount ++ ;
729
728
stats . writeTime += measure ( ( ) => {
730
- mkdirp ( path . dirname ( filename ) ) ;
729
+ mkdirp ( path . join ( baseDir , path . dirname ( filename ) ) ) ;
731
730
if ( typeof contents === "string" ) {
732
- fs . writeFileSync ( filename , contents , { encoding : "utf8" } ) ;
731
+ fs . writeFileSync ( path . join ( baseDir , filename ) , contents , { encoding : "utf8" } ) ;
733
732
} else {
734
- fs . writeFileSync ( filename , contents ) ;
733
+ fs . writeFileSync ( path . join ( baseDir , filename ) , contents ) ;
735
734
}
736
735
} ) ;
737
736
return true ;
@@ -740,11 +739,11 @@ exports.main = async function main(argv, options, callback) {
740
739
}
741
740
}
742
741
743
- function listFilesNode ( dirname ) {
742
+ function listFilesNode ( dirname , baseDir ) {
744
743
var files ;
745
744
try {
746
745
stats . readTime += measure ( ( ) => {
747
- files = fs . readdirSync ( dirname ) . filter ( file => / ^ (? ! .* \. d \. t s $ ) .* \. t s $ / . test ( file ) ) ;
746
+ files = fs . readdirSync ( path . join ( baseDir , dirname ) ) . filter ( file => / ^ (? ! .* \. d \. t s $ ) .* \. t s $ / . test ( file ) ) ;
748
747
} ) ;
749
748
return files ;
750
749
} catch ( e ) {
0 commit comments