2019-02-06 00:15:20 +07:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2019-02-08 00:47:00 +07:00
|
|
|
import { isCli, buildDir } from "./constants";
|
2019-02-06 00:15:20 +07:00
|
|
|
|
|
|
|
declare var __non_webpack_require__: typeof require;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handling of native modules within the CLI
|
|
|
|
*/
|
|
|
|
export const setup = (dataDirectory: string): void => {
|
2019-02-08 00:47:00 +07:00
|
|
|
if (!isCli) {
|
2019-02-06 00:15:20 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(path.join(dataDirectory, "modules"));
|
|
|
|
} catch (ex) {
|
|
|
|
if (ex.code !== "EEXIST") {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const unpackModule = (moduleName: string): void => {
|
2019-02-08 00:47:00 +07:00
|
|
|
const memFile = path.join(buildDir!, "build/modules", moduleName + ".node");
|
2019-02-06 00:15:20 +07:00
|
|
|
const diskFile = path.join(dataDirectory, "modules", moduleName + ".node");
|
|
|
|
if (!fs.existsSync(diskFile)) {
|
|
|
|
fs.writeFileSync(diskFile, fs.readFileSync(memFile));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need to unpack node-pty and patch its `loadNative` function to require our unpacked pty.node
|
|
|
|
* If pty.node isn't unpacked a SIGSEGV is thrown and the application exits. The exact reasoning
|
|
|
|
* for this is unknown ATM, but this patch works around it.
|
|
|
|
*/
|
|
|
|
unpackModule("pty");
|
2019-02-22 08:32:08 +07:00
|
|
|
unpackModule("spdlog");
|
2019-02-06 00:15:20 +07:00
|
|
|
const nodePtyUtils = require("../../protocol/node_modules/node-pty/lib/utils") as typeof import("../../protocol/node_modules/node-pty/src/utils");
|
2019-02-22 00:55:42 +07:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
nodePtyUtils.loadNative = (modName: string): any => {
|
2019-02-06 00:15:20 +07:00
|
|
|
return __non_webpack_require__(path.join(dataDirectory, "modules", modName + ".node"));
|
|
|
|
};
|
2019-02-22 08:32:08 +07:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
(<any>global).SPDLOG_LOCATION = path.join(dataDirectory, "modules", "spdlog.node");
|
2019-02-22 00:55:42 +07:00
|
|
|
// tslint:disable-next-line:no-unused-expression
|
2019-02-06 00:15:20 +07:00
|
|
|
require("../../protocol/node_modules/node-pty/lib/index") as typeof import("../../protocol/node_modules/node-pty/src/index");
|
2019-02-08 00:47:00 +07:00
|
|
|
};
|