Skip to content

Commit bac8a08

Browse files
authored
Merge pull request #1117 from TypeStrong/fix-custom-path-to-ts
Fix the custom path to typescriptServices.js feature
2 parents 157dea2 + 9c66ed5 commit bac8a08

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
"use strict";
2-
var vm = require("vm");
3-
var fs = require("fs");
2+
var path_1 = require("path");
43
global.stack = function () {
54
console.error(new Error().stack);
65
};
7-
function makeTsGlobal(typescriptServices) {
8-
var sandbox = {
9-
ts: {},
10-
console: console,
11-
stack: global.stack,
12-
require: require,
13-
module: module,
14-
process: process
15-
};
16-
vm.createContext(sandbox);
17-
if (typescriptServices) {
18-
vm.runInContext(fs.readFileSync(typescriptServices).toString(), sandbox);
6+
function makeTsGlobal(typescriptPath) {
7+
if (typescriptPath) {
8+
if (!path_1.isAbsolute(typescriptPath)) {
9+
throw new Error("Path to Typescript \"" + typescriptPath + "\" is not absolute");
10+
}
11+
typescriptPath = typescriptPath.trim();
1912
}
2013
else {
21-
sandbox.ts = require('typescript');
14+
typescriptPath = "typescript";
2215
}
23-
global.ts = sandbox.ts;
16+
global.ts = require(typescriptPath);
2417
}
2518
exports.makeTsGlobal = makeTsGlobal;
Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
1-
////////////////////////////////// MAGIC
2-
import vm = require('vm');
3-
import fs = require('fs');
4-
import os = require('os');
5-
import path = require('path');
1+
import {isAbsolute} from "path"
62

3+
// Debugging helper
74
global.stack = function() {
85
console.error((<any>new Error()).stack);
96
}
107

11-
/** Makes the bundled typescript services global or (if passed in) a custom typescriptServices file */
12-
export function makeTsGlobal(typescriptServices?: string) {
13-
var sandbox = {
14-
// This is going to gather the ts module exports
15-
ts: {},
16-
console: console,
17-
stack: global.stack,
18-
require: require,
19-
module: module,
20-
process: process
21-
};
22-
vm.createContext(sandbox);
23-
24-
if (typescriptServices) {
25-
vm.runInContext(fs.readFileSync(typescriptServices).toString(), sandbox);
26-
}
27-
else {
28-
sandbox.ts = require('typescript');
8+
// Export Typescript as a global. Takes an optional full path to typescriptServices.js
9+
export function makeTsGlobal(typescriptPath?: string) {
10+
if (typescriptPath) {
11+
if (!isAbsolute(typescriptPath)) {
12+
throw new Error(`Path to Typescript "${typescriptPath}" is not absolute`)
2913
}
3014

31-
// Finally export ts to the local global namespace
32-
global.ts = sandbox.ts;
15+
typescriptPath = typescriptPath.trim()
16+
} else {
17+
typescriptPath = "typescript"
18+
}
19+
20+
global.ts = require(typescriptPath);
3321
}

0 commit comments

Comments
 (0)