@@ -3,6 +3,7 @@ import * as nativeAssert from "assert";
33import { lauxlib , lua , lualib , to_jsstring , to_luastring } from "fengari" ;
44import * as fs from "fs" ;
55import { stringify } from "javascript-stringify" ;
6+ import { Volume } from "memfs" ;
67import * as path from "path" ;
78import * as prettyFormat from "pretty-format" ;
89import * as ts from "typescript" ;
@@ -176,6 +177,13 @@ export abstract class TestBuilder {
176177 return this ;
177178 }
178179
180+ private nativeFileSystem = false ;
181+ public useNativeFileSystem ( ) {
182+ expect ( this . hasProgram ) . toBe ( false ) ;
183+ this . nativeFileSystem = true ;
184+ return this ;
185+ }
186+
179187 private options : tstl . CompilerOptions = {
180188 luaTarget : tstl . LuaTarget . Lua53 ,
181189 noHeader : true ,
@@ -207,6 +215,17 @@ export abstract class TestBuilder {
207215 return this ;
208216 }
209217
218+ protected getSourceFiles ( ) {
219+ return { ...this . extraFiles , [ this . mainFileName ] : this . getTsCode ( ) } ;
220+ }
221+
222+ private extraRawFiles : Record < string , string > = { } ;
223+ public addRawFile ( fileName : string , content : string ) : this {
224+ expect ( this . hasProgram ) . toBe ( false ) ;
225+ this . extraRawFiles [ fileName ] = content ;
226+ return this ;
227+ }
228+
210229 private customTransformers ?: ts . CustomTransformers ;
211230 public setCustomTransformers ( customTransformers ?: ts . CustomTransformers ) : this {
212231 expect ( this . hasProgram ) . toBe ( false ) ;
@@ -224,14 +243,26 @@ export abstract class TestBuilder {
224243 @memoize
225244 public getProgram ( ) : ts . Program {
226245 this . hasProgram = true ;
227- return tstl . createVirtualProgram ( { ... this . extraFiles , [ this . mainFileName ] : this . getTsCode ( ) } , this . options ) ;
246+ return tstl . createVirtualProgram ( this . getSourceFiles ( ) , this . options ) ;
228247 }
229248
230249 @memoize
231250 public getLuaResult ( ) : tstl . TranspileVirtualProjectResult {
232- const program = this . getProgram ( ) ;
233251 const collector = createEmitOutputCollector ( ) ;
234- const { diagnostics : transpileDiagnostics } = new tstl . Transpiler ( ) . emit ( {
252+ const program = this . getProgram ( ) ;
253+
254+ const emitHost : tstl . EmitHost = { ...ts . sys } ;
255+ if ( ! this . nativeFileSystem ) {
256+ const virtualFS = Volume . fromJSON ( { ...this . extraRawFiles , ...this . getSourceFiles ( ) } , "/" ) ;
257+ emitHost . resolutionFileSystem = virtualFS ;
258+ emitHost . getCurrentDirectory = ( ) => "/" ;
259+ emitHost . readFile = ( fileName , encoding = "utf8" ) =>
260+ fileName . endsWith ( "lualib_bundle.lua" )
261+ ? ts . sys . readFile ( fileName , encoding )
262+ : ( virtualFS . readFileSync ( fileName , encoding ) as string ) ;
263+ }
264+
265+ const { diagnostics : transpileDiagnostics } = new tstl . Transpiler ( { emitHost } ) . emit ( {
235266 program,
236267 customTransformers : this . customTransformers ,
237268 writeFile : collector . writeFile ,
0 commit comments