UNPKG

twokeys-server

Version:

Server for 2Keys

92 lines (84 loc) 3.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @overview Adds the hotkeys server as a windows service */ const path_1 = require("path"); const mkdirp_1 = __importDefault(require("mkdirp")); const constants_1 = require("../util/constants"); const logger_1 = __importDefault(require("../util/logger")); const fs_1 = require("fs"); const logger = new logger_1.default({ name: "service" }); function gen_startup_js(name) { // NEED A BETTER WAY TO DO THIS return `/** * File auto-generated by 2Keys * Used for windows service to start 2Keys server * Project name: * Root dir: * DO NOT MODIFY */ const { spawn } = require("child_process"); const { createWriteStream } = require("fs"); const { join } = require("path"); const root = "${process.cwd().split("\\").join("\\\\")}"; // CHDIR to root process.chdir(root); // Create log file console.log("Creating log file..."); const date = new Date(); const logger = createWriteStream(join(root, ".2Keys", \`\${ date.getFullYear() }.\${ date.getMonth() }.\${ date.getDate() } \${ date.getHours() }.\${ date.getMinutes() }.\${ date.getSeconds() }.log\`)) console.log("Starting 2Keys server for ${name}..."); const server = spawn("2Keys", ["serve"], { shell: true, env: { "DEBUG": "*", }, }); server.stdout.on("data", (data) => { logger.write(data); }); server.stderr.on("data", (data) => { logger.write(data); }); server.on("close", (code) => { console.log(\`child process exited with code \${ code }\`); process.exit(code); }); server.on("error", (err) => { console.error("Failed to start subprocess."); process.exit(1); });`; } function gen_startup_vbs() { return `' FILE AUTO-GENERATED BY 2KEYS ' DO NOT MODIFY Set oShell = CreateObject("Wscript.Shell") Dim strArgs strArgs = "cmd /c node ${path_1.join(process.cwd(), constants_1.DEFAULT_LOCAL_2KEYS, constants_1.WINDOWS_SERVICE_FILE)}" oShell.Run strArgs, 0, false `; } function create_win_service(name) { logger.debug("Making files..."); // Create required dirs try { mkdirp_1.default.sync(path_1.join(process.cwd(), constants_1.DEFAULT_LOCAL_2KEYS)); logger.debug(`Made dir ${path_1.join(process.cwd(), constants_1.DEFAULT_LOCAL_2KEYS)}...`); } catch (err) { logger.throw(err); } // Create service file logger.debug(`Creating startup js file ${constants_1.WINDOWS_SERVICE_PREFIX}${name}...`); fs_1.writeFileSync(path_1.join(process.cwd(), constants_1.DEFAULT_LOCAL_2KEYS, constants_1.WINDOWS_SERVICE_FILE), gen_startup_js(name)); logger.debug("Adding a .vbs script to user startup folder to run 2Keys..."); fs_1.writeFileSync(path_1.join(process.env.APPDATA, "Microsoft", "Windows", "Start Menu", "Programs", "Startup", `${constants_1.WINDOWS_SERVICE_PREFIX}${name}.vbs`), gen_startup_vbs()); } exports.default = create_win_service; //# sourceMappingURL=service.js.map