File tree 2 files changed +22
-41
lines changed
2 files changed +22
-41
lines changed Original file line number Diff line number Diff line change 1
1
"use strict" ;
2
- var vm = require ( "vm" ) ;
3
- var fs = require ( "fs" ) ;
2
+ var path_1 = require ( "path" ) ;
4
3
global . stack = function ( ) {
5
4
console . error ( new Error ( ) . stack ) ;
6
5
} ;
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 ( ) ;
19
12
}
20
13
else {
21
- sandbox . ts = require ( ' typescript' ) ;
14
+ typescriptPath = " typescript" ;
22
15
}
23
- global . ts = sandbox . ts ;
16
+ global . ts = require ( typescriptPath ) ;
24
17
}
25
18
exports . makeTsGlobal = makeTsGlobal ;
Original file line number Diff line number Diff line change 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"
6
2
3
+ // Debugging helper
7
4
global . stack = function ( ) {
8
5
console . error ( ( < any > new Error ( ) ) . stack ) ;
9
6
}
10
7
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` )
29
13
}
30
14
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 ) ;
33
21
}
You can’t perform that action at this time.
0 commit comments