UNPKG

pot-js

Version:

Process management module

169 lines (128 loc) 4.69 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _potLogger = require('pot-logger'); var _createTable = require('../utils/createTable'); var _createTable2 = _interopRequireDefault(_createTable); var _lodash = require('lodash'); var _Connection = require('../Connection'); var _Connection2 = _interopRequireDefault(_Connection); var _logUpdate = require('log-update'); var _logUpdate2 = _interopRequireDefault(_logUpdate); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _PrepareCli = require('../utils/PrepareCli'); var _cli = require('../Schemas/cli'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } if (process.env !== 'production') { process.on('unhandledRejection', (reason, promise) => { console.warn('unhandledRejection: ' + reason); console.error(promise); }); } const paddingSpaces = ' '; const defaultCells = [{ title: 'Name', width: 10, get: state => state.name }, { title: 'Instance', width: 10, get: state => `#${state.monitor.instanceId || 0}` }, { title: 'Status', width: 8, get: (state, chalk) => { const status = state.monitor.status; switch (status) { case 'running': return chalk.green(status); case 'stopped': case 'crashed': return chalk.red(status); case 'sleeping': return chalk.yellow(status); default: return status; } } }, { title: 'Crashes', width: 8, get: state => state.monitor.crashes }, { title: 'Memory', width: 18, get: state => { var _state$memoryUsage = state.memoryUsage; const styled = _state$memoryUsage.styled, value = _state$memoryUsage.value; const color = value < 20 ? 'reset' : value < 50 ? 'yellow' : 'red'; return _chalk2.default[color](styled); } }, { title: 'CPU', width: 6, get: state => { var _state$cpuUsage = state.cpuUsage; const styled = _state$cpuUsage.styled, value = _state$cpuUsage.value; const color = value < 40 ? 'reset' : value < 80 ? 'yellow' : 'red'; return _chalk2.default[color](styled); } }, { title: 'Started', width: 20, get: state => state.monitor.startedLocal }, { title: 'PID', width: 6, get: state => state.pid }]; const list = (() => { var _ref = _asyncToGenerator(function* () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; (0, _PrepareCli.prepareRun)(_cli.list, options); var _options$cells = options.cells; const getCells = _options$cells === undefined ? defaultCells : _options$cells; const cells = (0, _lodash.isFunction)(getCells) ? getCells(defaultCells) : getCells; if (!cells.length) cells.push(); const instances = yield _Connection2.default.getAllInstances({ keepAlive: true }); const loop = (() => { var _ref2 = _asyncToGenerator(function* () { const table = (0, _createTable2.default)({ head: cells.map(function (_ref3) { var _ref3$title = _ref3.title; let title = _ref3$title === undefined ? '' : _ref3$title; return _chalk2.default.blue(title); }), colWidths: cells.map(function (_ref4) { let width = _ref4.width; return width || 10; }) }); let stateList = yield Promise.all(instances.map(function (instance) { return instance.getInfoVerbose(); })); stateList = stateList.filter(Boolean); if (!stateList.length) { _logUpdate2.default.clear(); return _potLogger.logger.warn('No process'); } table.push(new Array(cells.length)); stateList.forEach(function (state) { table.push(cells.map(function (_ref5) { let get = _ref5.get; if (!get) return ''; const val = get(state, _chalk2.default); return (0, _lodash.isUndefined)(val) ? '-' : val; })); }); let contents = table.toString(); contents = paddingSpaces + contents.split('\n').join(`\n${paddingSpaces}`); (0, _logUpdate2.default)(`\n\n${contents}\n\n`); setTimeout(loop, 1000); }); return function loop() { return _ref2.apply(this, arguments); }; })(); yield loop(); }); function list() { return _ref.apply(this, arguments); } return list; })(); list.defaultCells = defaultCells; exports.default = list;