pot-js
Version:
Process management module
109 lines (80 loc) • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _potLogger = require('pot-logger');
var _PrepareCli = require('../utils/PrepareCli');
var _cli = require('../Schemas/cli');
var _inquirer = require('inquirer');
var _inquirer2 = _interopRequireDefault(_inquirer);
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
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"); }); }; }
const isValidNumber = n => /^-?\d+$/.test(n);
exports.default = (() => {
var _ref = _asyncToGenerator(function* () {
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (options.instances === undefined && isValidNumber(options.name)) {
var _ref2 = yield (0, _PrepareCli.prepareTarget)({}, { noConnection: true });
const targetName = _ref2.targetName;
const maybeInstances = options.name;
const promptOptions = {
name: 'instance',
type: 'confirm',
message: `Did you mean \`scale ${targetName} ${maybeInstances}\`?`
};
const anweser = yield _inquirer2.default.prompt(promptOptions);
if (anweser.instance) {
options.instances = maybeInstances;
delete options.name;
}
}
(0, _PrepareCli.prepareRun)(_cli.scale, options);
var _ref3 = yield (0, _PrepareCli.prepareTarget)(options);
const connection = _ref3.connection,
targetName = _ref3.targetName;
const errorMessage = 'INVALID number';
const instances = yield (0, _PrepareCli.ensureArg)({
type: 'input',
validate: function (input) {
return isValidNumber(input) || errorMessage;
},
value: options.instances,
message: 'Please input instances count (Integer)',
errorMessage
});
var _ref4 = yield connection.scale(instances);
const ok = _ref4.ok,
added = _ref4.added,
removed = _ref4.removed,
errors = _ref4.errors;
if (errors && errors.length) {
const length = errors.length;
_potLogger.logger.warn(`${length} error${length > 1 ? 's' : ''} occurred`);
errors.forEach(function (error) {
return _potLogger.logger.debug(error.stack);
});
}
if (ok) {
if (added) {
added.forEach(function (_ref5) {
let displayName = _ref5.displayName;
_potLogger.logger.info(_chalk2.default.gray(`"${displayName}" created`));
});
} else if (removed) {
removed.forEach(function (_ref6) {
let displayName = _ref6.displayName;
_potLogger.logger.info(_chalk2.default.gray(`"${displayName}" removed`));
});
}
_potLogger.logger.info(`"${targetName}" scale compeleted`);
} else {
throw new Error(`"${targetName}" scale failed`);
}
});
function scale() {
return _ref.apply(this, arguments);
}
return scale;
})();