2019-01-18 22:46:40 +01:00
|
|
|
import * as cp from "child_process";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2019-02-05 18:15:20 +01:00
|
|
|
import * as zlib from "zlib";
|
|
|
|
import * as vm from "vm";
|
2019-02-07 18:47:00 +01:00
|
|
|
import { isCli } from "../constants";
|
2019-01-18 22:46:40 +01:00
|
|
|
|
2019-02-21 18:55:42 +01:00
|
|
|
let ipcMsgBuffer: Buffer[] | undefined = [];
|
|
|
|
let ipcMsgListener = process.send ? (d: Buffer): number => ipcMsgBuffer!.push(d) : undefined;
|
|
|
|
if (ipcMsgListener) {
|
|
|
|
process.on("message", ipcMsgListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requires a module from the filesystem.
|
|
|
|
*
|
|
|
|
* Will load from the CLI if file is included inside of the default extensions dir
|
|
|
|
*/
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
const requireFilesystemModule = (id: string, builtInExtensionsDir: string): any => {
|
|
|
|
const mod = require("module") as typeof import("module");
|
|
|
|
const customMod = new mod.Module(id);
|
|
|
|
customMod.filename = id;
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
customMod.paths = [(<any>mod)._nodeModulePaths(path.dirname(id)), path.join(__dirname, "../../../../lib/vscode/node_modules")];
|
|
|
|
|
|
|
|
if (id.startsWith(builtInExtensionsDir)) {
|
|
|
|
customMod.loaded = true;
|
|
|
|
const fileName = id.endsWith(".js") ? id : `${id}.js`;
|
|
|
|
const req = vm.runInThisContext(mod.wrap(fs.readFileSync(fileName).toString()), {
|
|
|
|
displayErrors: true,
|
|
|
|
filename: id + fileName,
|
|
|
|
});
|
|
|
|
req(customMod.exports, customMod.require.bind(customMod), customMod, fileName, path.dirname(id));
|
|
|
|
|
|
|
|
return customMod.exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
return customMod.require(id);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called from forking a module
|
|
|
|
*/
|
|
|
|
export const requireFork = (modulePath: string, args: string[], builtInExtensionsDir: string): void => {
|
|
|
|
const Module = require("module") as typeof import("module");
|
|
|
|
const oldRequire = Module.prototype.require;
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
Module.prototype.require = (id: string): any => {
|
|
|
|
if (id === "typescript") {
|
|
|
|
return require("typescript");
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldRequire(id);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!process.send) {
|
|
|
|
throw new Error("No IPC messaging initialized");
|
|
|
|
}
|
|
|
|
|
|
|
|
process.argv = ["", "", ...args];
|
|
|
|
|
|
|
|
requireFilesystemModule(modulePath, builtInExtensionsDir);
|
|
|
|
|
|
|
|
if (ipcMsgBuffer && ipcMsgListener) {
|
|
|
|
process.removeListener("message", ipcMsgListener);
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
ipcMsgBuffer.forEach((i) => process.emit("message" as any, i as any));
|
|
|
|
ipcMsgBuffer = undefined;
|
|
|
|
ipcMsgListener = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-05 18:15:20 +01:00
|
|
|
export const requireModule = (modulePath: string, builtInExtensionsDir: string): void => {
|
2019-01-18 22:46:40 +01:00
|
|
|
process.env.AMD_ENTRYPOINT = modulePath;
|
2019-01-28 18:14:06 +01:00
|
|
|
const xml = require("xhr2");
|
2019-02-21 18:55:42 +01:00
|
|
|
xml.XMLHttpRequest.prototype._restrictedHeaders["user-agent"] = false;
|
2019-01-30 01:23:30 +01:00
|
|
|
// tslint:disable-next-line no-any this makes installing extensions work.
|
|
|
|
(global as any).XMLHttpRequest = xml.XMLHttpRequest;
|
2019-01-28 18:14:06 +01:00
|
|
|
|
2019-02-05 18:15:20 +01:00
|
|
|
const mod = require("module") as typeof import("module");
|
2019-02-21 18:55:42 +01:00
|
|
|
const promiseFinally = require("promise.prototype.finally") as { shim: () => void };
|
|
|
|
promiseFinally.shim();
|
2019-02-05 18:15:20 +01:00
|
|
|
/**
|
|
|
|
* Used for loading extensions. Using __non_webpack_require__ didn't work
|
|
|
|
* as it was not resolving to the FS.
|
|
|
|
*/
|
2019-02-21 18:55:42 +01:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
(global as any).nativeNodeRequire = (id: string): any => {
|
|
|
|
return requireFilesystemModule(id, builtInExtensionsDir);
|
2019-02-05 18:15:20 +01:00
|
|
|
};
|
|
|
|
|
2019-02-21 18:55:42 +01:00
|
|
|
if (isCli) {
|
|
|
|
/**
|
|
|
|
* Needed for properly forking external modules within the CLI
|
|
|
|
*/
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
(<any>cp).fork = (modulePath: string, args: ReadonlyArray<string> = [], options?: cp.ForkOptions): cp.ChildProcess => {
|
|
|
|
return cp.spawn(process.execPath, ["--fork", modulePath, "--args", JSON.stringify(args)], {
|
|
|
|
...options,
|
|
|
|
stdio: [null, null, null, "ipc"],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:15:20 +01:00
|
|
|
let content: Buffer | undefined;
|
|
|
|
const readFile = (name: string): Buffer => {
|
|
|
|
return fs.readFileSync(path.join(process.env.BUILD_DIR as string || path.join(__dirname, "../.."), "./build", name));
|
|
|
|
};
|
2019-02-07 18:47:00 +01:00
|
|
|
if (isCli) {
|
2019-02-05 18:15:20 +01:00
|
|
|
content = zlib.gunzipSync(readFile("bootstrap-fork.js.gz"));
|
|
|
|
} else {
|
2019-02-21 18:55:42 +01:00
|
|
|
content = readFile("../../vscode/bin/bootstrap-fork.js");
|
2019-02-05 18:15:20 +01:00
|
|
|
}
|
2019-01-18 22:46:40 +01:00
|
|
|
eval(content.toString());
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the internal bootstrap-fork.js to load a module
|
|
|
|
* @example
|
2019-01-23 20:43:20 +01:00
|
|
|
* const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain");
|
2019-01-18 22:46:40 +01:00
|
|
|
* cp.stdout.on("data", (data) => console.log(data.toString("utf8")));
|
|
|
|
* cp.stderr.on("data", (data) => console.log(data.toString("utf8")));
|
2019-01-23 02:05:56 +01:00
|
|
|
* @param modulePath Path of the VS Code module to load.
|
2019-01-18 22:46:40 +01:00
|
|
|
*/
|
2019-02-19 17:17:03 +01:00
|
|
|
export const forkModule = (modulePath: string, args: string[], options: cp.ForkOptions): cp.ChildProcess => {
|
|
|
|
let proc: cp.ChildProcess;
|
|
|
|
const forkArgs = ["--bootstrap-fork", modulePath];
|
|
|
|
if (args) {
|
|
|
|
forkArgs.push("--args", JSON.stringify(args));
|
|
|
|
}
|
|
|
|
if (options.env) {
|
|
|
|
// This prevents vscode from trying to load original-fs from electron.
|
|
|
|
delete options.env.ELECTRON_RUN_AS_NODE;
|
|
|
|
forkArgs.push("--env", JSON.stringify(options.env));
|
2019-01-26 01:18:21 +01:00
|
|
|
}
|
2019-02-19 17:17:03 +01:00
|
|
|
const forkOptions: cp.ForkOptions = {
|
2019-01-30 01:23:30 +01:00
|
|
|
stdio: [null, null, null, "ipc"],
|
2019-01-23 18:52:58 +01:00
|
|
|
};
|
2019-02-07 18:47:00 +01:00
|
|
|
if (isCli) {
|
2019-02-21 18:55:42 +01:00
|
|
|
proc = cp.spawn(process.execPath, forkArgs, options);
|
2019-01-18 22:46:40 +01:00
|
|
|
} else {
|
2019-02-19 17:17:03 +01:00
|
|
|
proc = cp.spawn(process.execPath, ["--require", "ts-node/register", "--require", "tsconfig-paths/register", process.argv[1], ...forkArgs], forkOptions);
|
2019-01-18 22:46:40 +01:00
|
|
|
}
|
2019-01-23 02:05:56 +01:00
|
|
|
|
|
|
|
return proc;
|
2019-01-18 22:46:40 +01:00
|
|
|
};
|