@adpt/core
Version:
AdaptJS core library
69 lines • 2.69 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 compile_1 = require("./compile");
const context_1 = require("./context");
const hosts_1 = require("./hosts");
function exec(rootFiles, options) {
if (typeof rootFiles === "string")
rootFiles = [rootFiles];
if (rootFiles.length === 0)
throw new Error(`No root files to exec`);
const mainPath = rootFiles[0];
const tsDirname = path.dirname(mainPath);
const tsBasename = path.basename(mainPath);
let compiler = options.compiler;
const context = options.context || {};
const projectRoot = options.projectRoot || tsDirname;
if (compiler) {
if (compiler.rootFiles.indexOf(mainPath) === -1) {
throw new Error(`File ${mainPath} must be present in ` +
`Compiler.rootFiles`);
}
}
else {
const host = options.host || hosts_1.MemFileHost("/", projectRoot);
compiler = new compile_1.Compiler(projectRoot, rootFiles, host);
}
const ccontext = new context_1.VmContext(context, tsDirname, tsBasename, compiler.host);
const contents = compiler.host.readFile(mainPath);
if (!contents)
throw new Error(`Unable to read file ${mainPath}`);
const jsText = compiler.compile(contents, mainPath, ccontext.mainModule, options.lineOffset || 0);
if (hosts_1.debugChainableHosts) {
// tslint:disable-next-line:no-console
console.log(`After compile`);
compiler.dir();
}
if (jsText == null)
return undefined;
// And run the transpiled JS in the context vm
return ccontext.run(jsText);
}
exports.exec = exec;
function execString(code, context = {}, host) {
if (!host) {
host = hosts_1.MemFileHost("/");
}
const rootPath = path.join(host.getCurrentDirectory(), "[root].ts");
host.writeFile(rootPath, code);
return exec(rootPath, { context, host });
}
exports.execString = execString;
//# sourceMappingURL=exec.js.map