Skip to content

Fix the custom path to typescriptServices.js feature #1117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions dist/typescript/makeTypeScriptGlobal.js
Original file line number Diff line number Diff line change
@@ -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;
38 changes: 13 additions & 25 deletions lib/typescript/makeTypeScriptGlobal.ts
Original file line number Diff line number Diff line change
@@ -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((<any>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);
}