2019-02-06 00:15:20 +07:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
2019-03-01 03:34:54 +07:00
|
|
|
import * as os from "os";
|
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-28 04:12:26 +07:00
|
|
|
path.resolve(dataDirectory, "dependencies").split(path.sep).reduce((parentDir, childDir) => {
|
2019-02-23 07:42:59 +07:00
|
|
|
const currentDir = path.join(parentDir, childDir);
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(currentDir);
|
|
|
|
} catch (ex) {
|
2019-03-01 03:04:19 +07:00
|
|
|
if (ex.code !== "EEXIST" && ex.code !== "EISDIR" && ex.code !== "ENOENT") {
|
2019-02-23 07:42:59 +07:00
|
|
|
throw ex;
|
|
|
|
}
|
2019-02-06 00:15:20 +07:00
|
|
|
}
|
2019-02-23 07:42:59 +07:00
|
|
|
|
|
|
|
return currentDir;
|
2019-03-01 03:34:54 +07:00
|
|
|
}, os.platform() === "win32" ? undefined! : path.sep); // Might need path.sep here for linux. Having it for windows causes an error because \C:\Users ...
|
2019-02-06 00:15:20 +07:00
|
|
|
|
2019-03-05 07:42:02 +07:00
|
|
|
const unpackModule = (moduleName: string, markExecutable: boolean = false): void => {
|
2019-02-28 04:12:26 +07:00
|
|
|
const memFile = path.join(isCli ? buildDir! : path.join(__dirname, ".."), "build/dependencies", moduleName);
|
|
|
|
const diskFile = path.join(dataDirectory, "dependencies", moduleName);
|
2019-02-06 00:15:20 +07:00
|
|
|
if (!fs.existsSync(diskFile)) {
|
|
|
|
fs.writeFileSync(diskFile, fs.readFileSync(memFile));
|
2019-03-05 07:42:02 +07:00
|
|
|
|
|
|
|
if (markExecutable) {
|
|
|
|
fs.chmodSync(diskFile, "755");
|
|
|
|
}
|
2019-02-06 00:15:20 +07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-02-28 04:12:26 +07:00
|
|
|
unpackModule("pty.node");
|
|
|
|
unpackModule("spdlog.node");
|
2019-03-05 07:42:02 +07:00
|
|
|
unpackModule("rg", true);
|
2019-03-01 03:04:19 +07:00
|
|
|
// const nodePtyUtils = require("../../protocol/node_modules/node-pty-prebuilt/lib/utils") as typeof import("../../protocol/node_modules/node-pty-prebuilt/src/utils");
|
2019-02-22 00:55:42 +07:00
|
|
|
// tslint:disable-next-line:no-any
|
2019-03-01 03:04:19 +07:00
|
|
|
// nodePtyUtils.loadNative = (modName: string): any => {
|
|
|
|
// return (typeof __non_webpack_require__ !== "undefined" ? __non_webpack_require__ : require)(path.join(dataDirectory, "dependencies", modName + ".node"));
|
|
|
|
// };
|
2019-02-28 04:12:26 +07:00
|
|
|
(<any>global).RIPGREP_LOCATION = path.join(dataDirectory, "dependencies", "rg");
|
2019-03-01 03:04:19 +07:00
|
|
|
(<any>global).NODEPTY_LOCATION = path.join(dataDirectory, "dependencies", "pty.node");
|
2019-02-22 08:32:08 +07:00
|
|
|
// tslint:disable-next-line:no-any
|
2019-02-28 04:12:26 +07:00
|
|
|
(<any>global).SPDLOG_LOCATION = path.join(dataDirectory, "dependencies", "spdlog.node");
|
2019-02-22 00:55:42 +07:00
|
|
|
// tslint:disable-next-line:no-unused-expression
|
2019-02-28 11:16:31 +07:00
|
|
|
require("../../protocol/node_modules/node-pty-prebuilt/lib/index") as typeof import("../../protocol/node_modules/node-pty-prebuilt/src/index");
|
2019-02-08 00:47:00 +07:00
|
|
|
};
|