import * as path from "path" ; import { PluginModule } from "../deploy" ; import { InternalError } from "../error" ; import { ObserverManagerDeployment } from "../observers" ; import { nullStack , Stack , Stacks } from "../stack" ; import { exec } from "./exec" ; import { MemFileHost } from "./hosts" ; import * as Adapt from "../exports" ; export interface AdaptContext { pluginModules : Map < string , PluginModule > ; adaptStacks : Stacks ; observers : Map < string , ObserverManagerDeployment > ; Adapt : typeof Adapt ; projectRoot : string ; } export function createAdaptContext ( projectRoot ) { const adaptStacks = new Map < string , Stack > ( ) ; adaptStacks . set ( "(null)" , nullStack ( ) ) ; return Object . assign ( Object . create ( null ) , { pluginModules : new Map < string , PluginModule > ( ) , adaptStacks , observers : new Map < string , ObserverManagerDeployment > ( ) , projectRoot , } ) ; } export function getAdaptContext ( ) { const g = global ; if ( typeof g . getAdaptContext !== "function" ) { throw new InternalError ( `Unable to get global AdaptContext` ) ; } return g . getAdaptContext ( ) ; } export function projectExec ( projectRoot , rootFileName ) { const context = Object . create ( null ) ; const adaptContext = createAdaptContext ( projectRoot ) ; context . getAdaptContext = ( ) => adaptContext ; const fileExt = path . extname ( rootFileName ) ; const importName = path . basename ( rootFileName , fileExt ) ; const wrapper = `
        require("source-map-support").install();
        import * as Adapt from "@adpt/core";
        import { getAdaptContext } from "@adpt/core/dist/src/ts";

        getAdaptContext().Adapt = Adapt;

        require("./${ importName }");
        ` ; const wrapperFileName = path . join ( projectRoot , "[wrapper].ts" ) ; const host = MemFileHost ( "/" , projectRoot ) ; host . writeFile ( wrapperFileName , wrapper ) ; exec ( [ wrapperFileName , rootFileName ] , { context , host } ) ; return adaptContext ; } export interface MockAdaptContext extends AdaptContext { stop : ( ) => void ; } export function mockAdaptContext ( ) { const ctx = createAdaptContext ( path . resolve ( "." ) ) ; const g = global ; const oldContext = g . getAdaptContext ; g . getAdaptContext = ( ) => ctx ; return { ... ctx , stop : ( ) => { if ( ! oldContext ) delete g . getAdaptContext ; else g . getAdaptContext = oldContext ; } , } ; }