UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

154 lines 5.83 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var debug_1 = __importDefault(require("debug")); var events_1 = require("events"); var child_process_1 = require("child_process"); var d = debug_1.default('remoteit:Process'); /** * A generic system process. */ var Process = /** @class */ (function (_super) { __extends(Process, _super); function Process(command) { var _this = _super.call(this) || this; _this.command = command; _this.handleStart = function (error, stdout, stderr) { d('Process started:', { error: error, stdout: stdout, stderr: stderr }); if (error) return _this.handleError(error.message); if (stderr) return _this.handleError(stderr.toString()); _this.emit('started'); }; _this.handleStdOut = function (buff) { var e_1, _a; var messages = _this.processBuffer(buff); try { for (var messages_1 = __values(messages), messages_1_1 = messages_1.next(); !messages_1_1.done; messages_1_1 = messages_1.next()) { var message = messages_1_1.value; _this.handleMessage(message); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (messages_1_1 && !messages_1_1.done && (_a = messages_1.return)) _a.call(messages_1); } finally { if (e_1) throw e_1.error; } } }; _this.handleStdErr = function (buff) { var e_2, _a; var errors = _this.processBuffer(buff); try { for (var errors_1 = __values(errors), errors_1_1 = errors_1.next(); !errors_1_1.done; errors_1_1 = errors_1.next()) { var error = errors_1_1.value; _this.handleError(error); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (errors_1_1 && !errors_1_1.done && (_a = errors_1.return)) _a.call(errors_1); } finally { if (e_2) throw e_2.error; } } }; _this.handleError = function (error) { error = error instanceof Error ? error.message : error; d('Handle standard error:', error); _this.emit('error', error); }; _this.handleClose = function (code) { d('Process closed with code:', code); _this.emit('closed', Number(code)); }; d('Creating process:', command); return _this; } Process.prototype.execute = function () { d('Execute process:', this.command); var options = { maxBuffer: Infinity }; this.process = child_process_1.execFile(this.file, this.args, options, this.handleStart); if (!this.process || !this.process.stdout || !this.process.stderr) { this.handleError('Could not start process!'); return; } this.process.stdout.on('data', this.handleStdOut); this.process.stderr.on('data', this.handleStdErr); this.process.on('error', this.handleError); this.process.on('close', this.handleClose); }; Process.prototype.kill = function () { if (!this.process) return; this.process.kill(); }; Object.defineProperty(Process.prototype, "pid", { // restart() { // this.kill() // this.execute() // } get: function () { if (!this.process || !this.process.pid) return; return this.process.pid; }, enumerable: true, configurable: true }); Object.defineProperty(Process.prototype, "file", { get: function () { return this.command.split(' ')[0]; }, enumerable: true, configurable: true }); Object.defineProperty(Process.prototype, "args", { get: function () { return this.command.split(' ').slice(1); }, enumerable: true, configurable: true }); Process.prototype.handleMessage = function (message) { d('Handle message:', message); this.emit('message', message); }; Process.prototype.processBuffer = function (buff) { return buff .toString() .trim() .split(/\r?\n/); }; return Process; }(events_1.EventEmitter)); exports.Process = Process; //# sourceMappingURL=Process.js.map