11import * as path from 'path' ;
2- import * as vm from 'vm' ;
32
43import { App } from '../../definition/App' ;
54import { AppMethod } from '../../definition/metadata' ;
65import { AppAccessors } from '../accessors' ;
76import { AppManager } from '../AppManager' ;
8- import { MustContainFunctionError , MustExtendAppError } from '../errors' ;
7+ import { MustContainFunctionError } from '../errors' ;
98import { AppConsole } from '../logging' ;
10- import { Utilities } from '../misc/Utilities' ;
119import { ProxiedApp } from '../ProxiedApp' ;
10+ import { getRuntime } from '../runtime' ;
11+ import { buildCustomRequire } from '../runtime/require' ;
1212import { IAppStorageItem } from '../storage' ;
1313import { IParseAppPackageResult } from './IParseAppPackageResult' ;
1414
@@ -29,31 +29,30 @@ export class AppCompiler {
2929 `Could not find the classFile (${ storage . info . classFile } ) file.` ) ;
3030 }
3131
32- const exports = { } ;
33- const customRequire = Utilities . buildCustomRequire ( files , storage . info . id ) ;
34- const context = Utilities . buildDefaultAppContext ( { require : customRequire , exports, process : { } , console } ) ;
32+ const Runtime = getRuntime ( ) ;
3533
36- const script = new vm . Script ( files [ path . normalize ( storage . info . classFile ) ] ) ;
37- const result = script . runInContext ( context ) ;
34+ const customRequire = buildCustomRequire ( files , storage . info . id ) ;
35+ const result = Runtime . runCode ( files [ path . normalize ( storage . info . classFile ) ] , {
36+ require : customRequire ,
37+ } ) ;
3838
3939 if ( typeof result !== 'function' ) {
4040 // tslint:disable-next-line:max-line-length
4141 throw new Error ( `The App's main class for ${ storage . info . name } is not valid ("${ storage . info . classFile } ").` ) ;
4242 }
43-
4443 const appAccessors = new AppAccessors ( manager , storage . info . id ) ;
4544 const logger = new AppConsole ( AppMethod . _CONSTRUCTOR ) ;
46- const rl = vm . runInNewContext ( ' new App(info, rcLogger, appAccessors);', Utilities . buildDefaultAppContext ( {
45+ const rl = Runtime . runCode ( 'exports.app = new App(info, rcLogger, appAccessors);', {
4746 rcLogger : logger ,
4847 info : storage . info ,
4948 App : result ,
50- process : { } ,
5149 appAccessors,
52- } ) , { timeout : 1000 , filename : `App_${ storage . info . nameSlug } .js` } ) ;
50+ } , { timeout : 1000 , filename : `App_${ storage . info . nameSlug } .js` } ) ;
5351
54- if ( ! ( rl instanceof App ) ) {
55- throw new MustExtendAppError ( ) ;
56- }
52+ // TODO: app is importing the Class App internally so it's not same object to compare. Need to find a way to make this test
53+ // if (!(rl instanceof App)) {
54+ // throw new MustExtendAppError();
55+ // }
5756
5857 if ( typeof rl . getName !== 'function' ) {
5958 throw new MustContainFunctionError ( storage . info . classFile , 'getName' ) ;
@@ -79,7 +78,8 @@ export class AppCompiler {
7978 throw new MustContainFunctionError ( storage . info . classFile , 'getRequiredApiVersion' ) ;
8079 }
8180
82- const app = new ProxiedApp ( manager , storage , rl as App , customRequire ) ;
81+ // TODO: Fix this type cast from to any to the right one
82+ const app = new ProxiedApp ( manager , storage , rl as App , new Runtime ( rl as App , customRequire as any ) ) ;
8383
8484 manager . getLogStorage ( ) . storeEntries ( app . getID ( ) , logger ) ;
8585
0 commit comments