2019-01-18 21:46:40 +00:00
|
|
|
import * as cp from "child_process";
|
|
|
|
import * as fs from "fs";
|
2019-01-23 17:52:58 +00:00
|
|
|
import * as net from "net";
|
2019-01-18 21:46:40 +00:00
|
|
|
import * as path from "path";
|
|
|
|
|
|
|
|
export const requireModule = (modulePath: string): void => {
|
|
|
|
process.env.AMD_ENTRYPOINT = modulePath;
|
2019-01-26 00:18:21 +00:00
|
|
|
|
2019-01-28 17:14:06 +00:00
|
|
|
const xml = require("xhr2");
|
|
|
|
|
|
|
|
(<any>global).XMLHttpRequest = xml.XMLHttpRequest;
|
|
|
|
|
2019-01-26 00:18:21 +00:00
|
|
|
// Always do this so we can see console.logs.
|
|
|
|
// process.env.VSCODE_ALLOW_IO = "true";
|
2019-01-23 17:52:58 +00:00
|
|
|
|
|
|
|
if (!process.send) {
|
|
|
|
const socket = new net.Socket({ fd: 3 });
|
|
|
|
socket.on("data", (data) => {
|
|
|
|
process.emit("message", JSON.parse(data.toString()), undefined);
|
|
|
|
});
|
|
|
|
|
2019-01-23 19:43:20 +00:00
|
|
|
// tslint:disable-next-line no-any
|
2019-01-23 17:52:58 +00:00
|
|
|
process.send = (message: any): void => {
|
|
|
|
socket.write(JSON.stringify(message));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-18 21:46:40 +00:00
|
|
|
const content = fs.readFileSync(path.join(process.env.BUILD_DIR as string || path.join(__dirname, "../.."), "./build/bootstrap-fork.js"));
|
|
|
|
eval(content.toString());
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the internal bootstrap-fork.js to load a module
|
|
|
|
* @example
|
2019-01-23 19:43:20 +00:00
|
|
|
* const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain");
|
2019-01-18 21:46:40 +00:00
|
|
|
* cp.stdout.on("data", (data) => console.log(data.toString("utf8")));
|
|
|
|
* cp.stderr.on("data", (data) => console.log(data.toString("utf8")));
|
2019-01-23 01:05:56 +00:00
|
|
|
* @param modulePath Path of the VS Code module to load.
|
2019-01-18 21:46:40 +00:00
|
|
|
*/
|
2019-01-26 00:18:21 +00:00
|
|
|
export const forkModule = (modulePath: string, env?: NodeJS.ProcessEnv): cp.ChildProcess => {
|
2019-01-23 01:05:56 +00:00
|
|
|
let proc: cp.ChildProcess | undefined;
|
|
|
|
|
2019-01-18 21:46:40 +00:00
|
|
|
const args = ["--bootstrap-fork", modulePath];
|
2019-01-26 00:18:21 +00:00
|
|
|
if (env) {
|
|
|
|
args.push("--env", JSON.stringify(env));
|
|
|
|
}
|
2019-01-23 17:52:58 +00:00
|
|
|
const options: cp.SpawnOptions = {
|
|
|
|
stdio: [null, null, null, "pipe"],
|
|
|
|
};
|
2019-01-18 21:46:40 +00:00
|
|
|
if (process.env.CLI === "true") {
|
2019-01-23 19:43:20 +00:00
|
|
|
proc = cp.spawn(process.execPath, args, options);
|
2019-01-18 21:46:40 +00:00
|
|
|
} else {
|
2019-01-23 17:52:58 +00:00
|
|
|
proc = cp.spawn(process.execArgv[0], ["-r", "tsconfig-paths/register", process.argv[1], ...args], options);
|
2019-01-18 21:46:40 +00:00
|
|
|
}
|
2019-01-23 01:05:56 +00:00
|
|
|
|
|
|
|
return proc;
|
2019-01-18 21:46:40 +00:00
|
|
|
};
|