import * as fs from 'fs' ; import * as path from 'path' ; export class TestHost implements pxt . Host { static files : pxt . Map < pxt . Map < string > > = { } ; constructor ( public name , public packageFiles , public extraDependencies , private includeCommon = false ) { } resolve ( module , filename ) { return "" ; } readFile ( module , filename ) { if ( TestHost . files [ module . id ] && TestHost . files [ module . id ] [ filename ] ) { return TestHost . files [ module . id ] [ filename ] ; } if ( module . id == "this" ) { if ( filename == "pxt.json" ) { let commonFiles = this . includeCommon ? [ "pxt-core.d.ts" , "pxt-helpers.ts" , "pxt-python.d.ts" , "pxt-python-helpers.ts" ] : [ ] ; let packageFileNames = Object . keys ( this . packageFiles ) ; return JSON . stringify ( { "name" : this . name , "dependencies" : this . dependencies , "description" : "" , "files" : packageFileNames . concat ( commonFiles ) } ) ; } else if ( filename in this . packageFiles ) { return this . packageFiles [ filename ] ; } } else if ( pxt . appTarget . bundledpkgs [ module . id ] && filename === pxt . CONFIG_NAME ) { return pxt . appTarget . bundledpkgs [ module . id ] [ pxt . CONFIG_NAME ] ; } else { let p0 = path . join ( module . id , filename ) ; let p1 = path . join ( 'libs' , module . id , filename ) ; let p2 = path . join ( 'libs' , module . id , 'built' , filename ) ; let contents = null ; try { contents = fs . readFileSync ( p0 , 'utf8' ) ; } catch ( e ) { try { contents = fs . readFileSync ( p1 , 'utf8' ) ; } catch ( e ) { try { contents = fs . readFileSync ( p2 , 'utf8' ) ; } catch ( e ) { } } } if ( contents ) { this . writeFile ( module , filename , contents ) ; return contents ; } } if ( filename === "pxt-core.d.ts" || filename === "pxt-helpers.ts" ) { const contents = fs . readFileSync ( path . resolve ( "libs" , "pxt-common" , filename ) , 'utf8' ) ; this . writeFile ( module , filename , contents ) ; return contents ; } else if ( filename === "pxt-python.d.ts" || filename === "pxt-python-helpers.ts" ) { const contents = fs . readFileSync ( path . resolve ( "libs" , "pxt-python" , filename ) , 'utf8' ) ; this . writeFile ( module , filename , contents ) ; return contents ; } return "" ; } writeFile ( module , filename , contents ) { if ( ! TestHost . files [ module . id ] ) { TestHost . files [ module . id ] = { } ; } TestHost . files [ module . id ] [ filename ] = contents ; } getHexInfoAsync ( extInfo ) { return Promise . resolve < any > ( null ) ; } cacheStoreAsync ( id , val ) { return Promise . resolve ( ) ; } cacheGetAsync ( id ) { return Promise . resolve ( "" ) ; } downloadPackageAsync ( pkg ) { return Promise . resolve ( ) ; } resolveVersionAsync ( pkg ) { return Promise . resolve ( "*" ) ; } private get dependencies ( ) : { [ key ] : string ; } { let stdDeps = { } ; for ( let extraDep of this . extraDependencies ) { stdDeps [ extraDep ] = `file:../${ extraDep }` ; } return stdDeps ; } }