code-server-2/packages/server/src/vscode/bootstrapFork.ts

75 lines
2.5 KiB
TypeScript
Raw Normal View History

import * as cp from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as zlib from "zlib";
import * as vm from "vm";
2019-02-07 17:47:00 +00:00
import { isCli } from "../constants";
export const requireModule = (modulePath: string, builtInExtensionsDir: string): void => {
process.env.AMD_ENTRYPOINT = modulePath;
const xml = require("xhr2");
// tslint:disable-next-line no-any this makes installing extensions work.
(global as any).XMLHttpRequest = xml.XMLHttpRequest;
const mod = require("module") as typeof import("module");
/**
* Used for loading extensions. Using __non_webpack_require__ didn't work
* as it was not resolving to the FS.
*/
(global as any).nativeNodeRequire = (id: string) => {
const customMod = new mod.Module(id);
customMod.filename = id;
customMod.paths = (<any>mod)._nodeModulePaths(path.dirname(id));
if (id.startsWith(builtInExtensionsDir)) {
customMod.loaded = true;
const req = vm.runInThisContext(mod.wrap(fs.readFileSync(id + ".js").toString()), {
displayErrors: true,
filename: id + ".js",
});
req(customMod.exports, customMod.require.bind(customMod), customMod, __filename, path.dirname(id));
return customMod.exports;
}
return customMod.require(id);
};
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 17:47:00 +00:00
if (isCli) {
content = zlib.gunzipSync(readFile("bootstrap-fork.js.gz"));
} else {
content = readFile("../resources/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");
* 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.
*/
export const forkModule = (modulePath: string, env?: NodeJS.ProcessEnv): cp.ChildProcess => {
2019-01-23 01:05:56 +00:00
let proc: cp.ChildProcess | undefined;
const args = ["--bootstrap-fork", modulePath];
if (env) {
args.push("--env", JSON.stringify(env));
}
2019-01-23 17:52:58 +00:00
const options: cp.SpawnOptions = {
stdio: [null, null, null, "ipc"],
2019-01-23 17:52:58 +00:00
};
2019-02-07 17:47:00 +00:00
if (isCli) {
proc = cp.execFile(process.execPath, args, options);
} else {
proc = cp.spawn(process.execPath, ["--require", "ts-node/register", "--require", "tsconfig-paths/register", process.argv[1], ...args], options);
}
2019-01-23 01:05:56 +00:00
return proc;
};