@adpt/core
Version:
AdaptJS core library
77 lines (75 loc) • 2.99 kB
JavaScript
;
/*
* Copyright 2018-2019 Unbounded Systems, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("path"));
const error_1 = require("../error");
const stack_1 = require("../stack");
const exec_1 = require("./exec");
const hosts_1 = require("./hosts");
function createAdaptContext() {
const adaptStacks = new Map();
adaptStacks.set("(null)", stack_1.nullStack());
return Object.assign(Object.create(null), {
pluginModules: new Map(),
adaptStacks,
observers: new Map(),
});
}
exports.createAdaptContext = createAdaptContext;
function getAdaptContext() {
const g = global;
if (typeof g.getAdaptContext !== "function") {
// If we're running inside a VmContext, this should exist on global.
// It is invalid to call this function from outside a VmContext.
throw new error_1.InternalError(`Unable to get global AdaptContext`);
}
return g.getAdaptContext();
}
exports.getAdaptContext = getAdaptContext;
function projectExec(projectRoot, rootFileName) {
// This becomes "global" inside the project program
const context = Object.create(null);
const adaptContext = createAdaptContext();
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 = hosts_1.MemFileHost("/", projectRoot);
host.writeFile(wrapperFileName, wrapper);
exec_1.exec([wrapperFileName, rootFileName], { context, host });
return adaptContext;
}
exports.projectExec = projectExec;
// Testing only
function mockAdaptContext() {
const ctx = createAdaptContext();
const g = global;
if (g.getAdaptContext != null)
throw new Error(`Can't mock AdaptContext. getAdaptContext already set.`);
g.getAdaptContext = () => ctx;
return Object.assign({}, ctx, { stop: () => delete g.getAdaptContext });
}
exports.mockAdaptContext = mockAdaptContext;
//# sourceMappingURL=projectExec.js.map