UNPKG

onemon

Version:

Run a npm script as a deamon, once

159 lines 6.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.kill = exports.run = void 0; const path_1 = __importDefault(require("path")); const chalk_1 = __importDefault(require("chalk")); const supports_color_1 = __importDefault(require("supports-color")); const exits = __importStar(require("exits")); const runner_1 = __importDefault(require("./runner")); const manager_1 = require("./manager"); const ipc_client_1 = require("./ipc-client"); ; ; ; ; const run = (deamonPath, options) => __awaiter(void 0, void 0, void 0, function* () { const { script, silent, wait } = options; let pkgScript; if (script) { if (script.split('.').pop() !== 'js') { pkgScript = yield manager_1.getPKG(script); } } const socketName = manager_1.getSocketName(deamonPath); const socketPath = path_1.default.join('/tmp', socketName); const { client, deamonReady } = yield connectToDeamon({ socketPath, deamonPath, waitDeamonReady: wait, retry: true }); console.log(chalk_1.default.yellow(`[ONEMON] IPC connected on ${socketName}`)); exits.attach(); exits.add(() => client.close()); client.on('disconnect', () => { console.log(chalk_1.default.yellow(`[ONEMON] IPC disconnected from ${socketName}`)); }); if (!silent) { client.on(`message.${"ERROR" /* ERROR */}`, (message) => { process.stdout.write(Buffer.from(message.data)); }); client.on(`message.${"PRINT" /* PRINT */}`, (message) => { process.stdout.write(Buffer.from(message.data)); }); } if (wait) yield deamonReady; if (script) { console.log(chalk_1.default.yellow('[ONEMON] Launching script')); if (pkgScript) { runner_1.default({ stdio: 'inherit', cwd: pkgScript, shell: true }) .spawn(manager_1.getShellScript(pkgScript, script)) .on('exit', (code) => exits.terminate('exit', code)); } else { runner_1.default({ stdio: 'inherit' }) .fork(script, []) .on('exit', (code) => exits.terminate('exit', code)); } } else if (wait) { console.log(chalk_1.default.yellow('[ONEMON] Notified ready')); } }); exports.run = run; const kill = (deamonPath) => __awaiter(void 0, void 0, void 0, function* () { const socketName = manager_1.getSocketName(deamonPath); const socketPath = path_1.default.join('/tmp', socketName); const { client } = yield connectToDeamon({ socketPath, deamonPath, retry: false }); exits.attach(); exits.add(() => client.close()); client.send("CLOSE" /* CLOSE */); client.close(); }); exports.kill = kill; const connectToDeamon = (options) => __awaiter(void 0, void 0, void 0, function* () { const { socketPath, retry } = options; try { return yield connectToSocket(socketPath); } catch (error) { if (retry) { yield createDeamon(options); return connectToSocket(socketPath); } else { throw error; } } }); const connectToSocket = (socketPath) => __awaiter(void 0, void 0, void 0, function* () { const client = new ipc_client_1.IPCClient({ socketPath }); const deamonReady = new Promise(resolve => client .on(`message.${"DEAMON_READY" /* DEAMON_READY */}`, () => resolve())); yield client.connect(); return { client, deamonReady }; }); const createDeamon = (options) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve) => { const { deamonPath, socketPath, waitDeamonReady } = options; const resolved = path_1.default.resolve(deamonPath); const proc = runner_1.default({ detached: true, stdio: 'ignore', cwd: path_1.default.dirname(resolved) }) .fork(path_1.default.resolve(__dirname, 'deamon.js'), [ resolved, socketPath, waitDeamonReady ? "TRUE" /* TRUE */ : "FALSE" /* FALSE */, supports_color_1.default.stdout ? "TRUE" /* TRUE */ : "FALSE" /* FALSE */ ]); proc.on('message', message => { if (message === "SERVER_READY" /* SERVER_READY */) { proc.disconnect(); proc.unref(); resolve(); } }); }); }); //# sourceMappingURL=deamonize.js.map