From 9c66ed54ca0a0637efd13992ff1956a3be10bf26 Mon Sep 17 00:00:00 2001 From: Guntars Asmanis-Graham Date: Sun, 30 Oct 2016 13:00:51 -0400 Subject: [PATCH] Fix the custom path to typescriptServices.js feature --- dist/typescript/makeTypeScriptGlobal.js | 25 ++++++---------- lib/typescript/makeTypeScriptGlobal.ts | 38 +++++++++---------------- 2 files changed, 22 insertions(+), 41 deletions(-) diff --git a/dist/typescript/makeTypeScriptGlobal.js b/dist/typescript/makeTypeScriptGlobal.js index 5b689fb06..56e3d65d7 100644 --- a/dist/typescript/makeTypeScriptGlobal.js +++ b/dist/typescript/makeTypeScriptGlobal.js @@ -1,25 +1,18 @@ "use strict"; -var vm = require("vm"); -var fs = require("fs"); +var path_1 = require("path"); global.stack = function () { console.error(new Error().stack); }; -function makeTsGlobal(typescriptServices) { - var sandbox = { - ts: {}, - console: console, - stack: global.stack, - require: require, - module: module, - process: process - }; - vm.createContext(sandbox); - if (typescriptServices) { - vm.runInContext(fs.readFileSync(typescriptServices).toString(), sandbox); +function makeTsGlobal(typescriptPath) { + if (typescriptPath) { + if (!path_1.isAbsolute(typescriptPath)) { + throw new Error("Path to Typescript \"" + typescriptPath + "\" is not absolute"); + } + typescriptPath = typescriptPath.trim(); } else { - sandbox.ts = require('typescript'); + typescriptPath = "typescript"; } - global.ts = sandbox.ts; + global.ts = require(typescriptPath); } exports.makeTsGlobal = makeTsGlobal; diff --git a/lib/typescript/makeTypeScriptGlobal.ts b/lib/typescript/makeTypeScriptGlobal.ts index 22e2460c0..6404e9313 100644 --- a/lib/typescript/makeTypeScriptGlobal.ts +++ b/lib/typescript/makeTypeScriptGlobal.ts @@ -1,33 +1,21 @@ -////////////////////////////////// MAGIC -import vm = require('vm'); -import fs = require('fs'); -import os = require('os'); -import path = require('path'); +import {isAbsolute} from "path" +// Debugging helper global.stack = function() { console.error((new Error()).stack); } -/** Makes the bundled typescript services global or (if passed in) a custom typescriptServices file */ -export function makeTsGlobal(typescriptServices?: string) { - var sandbox = { - // This is going to gather the ts module exports - ts: {}, - console: console, - stack: global.stack, - require: require, - module: module, - process: process - }; - vm.createContext(sandbox); - - if (typescriptServices) { - vm.runInContext(fs.readFileSync(typescriptServices).toString(), sandbox); - } - else { - sandbox.ts = require('typescript'); +// Export Typescript as a global. Takes an optional full path to typescriptServices.js +export function makeTsGlobal(typescriptPath?: string) { + if (typescriptPath) { + if (!isAbsolute(typescriptPath)) { + throw new Error(`Path to Typescript "${typescriptPath}" is not absolute`) } - // Finally export ts to the local global namespace - global.ts = sandbox.ts; + typescriptPath = typescriptPath.trim() + } else { + typescriptPath = "typescript" + } + + global.ts = require(typescriptPath); }