UNPKG

extremum-cli

Version:
199 lines (198 loc) 8.84 kB
"use strict"; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeConfigFile = exports.getLoginConfig = exports.getConfigFilePath = exports.getCredentialsConfigFilePath = exports.getAppName = exports.loginServerHandler = exports.LIST_OPTIONS = exports.safeReadFile = exports.safeParseJson = exports.convertIntegerParameter = exports.actionRunner = void 0; var chalk_1 = __importDefault(require("chalk")); var fs_1 = __importDefault(require("fs")); var server_1 = __importDefault(require("./server")); var child_process_1 = __importDefault(require("child_process")); var os_1 = __importDefault(require("os")); var path_1 = __importDefault(require("path")); var yaml_1 = require("yaml"); var SERVER_URL = 'http://localhost:3003'; function actionRunner(fn, raw) { var _this = this; if (raw === void 0) { raw = false; } return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return __awaiter(_this, void 0, void 0, function () { var res, e_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, fn.apply(void 0, args)]; case 1: res = _a.sent(); if (Buffer.isBuffer(res)) { process.stdout.write(res); } else { if (res !== undefined) { console.log(raw ? res : JSON.stringify(res, null, 2)); } } return [3 /*break*/, 3]; case 2: e_1 = _a.sent(); console.error(chalk_1.default.red("Error: ".concat(e_1.message))); console.error(); console.error('Error details:'); console.error(JSON.stringify(e_1, null, 2)); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); }; } exports.actionRunner = actionRunner; var convertIntegerParameter = function (name) { return function (s) { var n = Number.parseInt(s); if (Number.isNaN(n)) { console.error(chalk_1.default.red("Bad '".concat(name, "' parameter value: '").concat(s, "'"))); process.exit(-1); } return n; }; }; exports.convertIntegerParameter = convertIntegerParameter; var safeParseJson = function (data) { try { return JSON.parse(data); } catch (e) { console.error(chalk_1.default.red("Error: ".concat(e.message))); process.exit(-1); } }; exports.safeParseJson = safeParseJson; var safeReadFile = function (filename) { try { return fs_1.default.readFileSync(filename, { encoding: 'utf-8' }); } catch (e) { console.error(chalk_1.default.red("Error: ".concat(e.message))); process.exit(-1); } }; exports.safeReadFile = safeReadFile; exports.LIST_OPTIONS = { offset: 0, limit: 10000, expand: true, }; var loginServerHandler = function (config, appName, env) { try { (0, server_1.default)(config, appName, env); var start = process.platform == 'darwin' ? 'open' : process.platform == 'win32' ? 'start' : 'xdg-open'; child_process_1.default.exec(start + ' ' + SERVER_URL); } catch (e) { console.error("login error: ".concat(e)); } }; exports.loginServerHandler = loginServerHandler; var getAppName = function () { if (os_1.default.platform() === 'linux' || os_1.default.platform() === 'darwin') { var executable = process.argv[1]; return executable.split(path_1.default.sep).pop(); } else if (os_1.default.platform() === 'win32') { var executable = process.argv[2]; return executable.slice(11); } else { throw new Error('App name is not defined'); } }; exports.getAppName = getAppName; var getCredentialsConfigFilePath = function (appName) { var homeDir = os_1.default.homedir(); var credentialsPathDir = path_1.default.join(homeDir, '.e6m', 'state'); if (!fs_1.default.existsSync(credentialsPathDir)) { fs_1.default.mkdirSync(credentialsPathDir); } return path_1.default.join(credentialsPathDir, "".concat(appName, ".yaml")); }; exports.getCredentialsConfigFilePath = getCredentialsConfigFilePath; var getConfigFilePath = function (appName) { var homeDir = os_1.default.homedir(); var configPathDir = path_1.default.join(homeDir, '.e6m'); if (!fs_1.default.existsSync(configPathDir)) { fs_1.default.mkdirSync(configPathDir); } return path_1.default.join(configPathDir, "".concat(appName, ".yaml")); }; exports.getConfigFilePath = getConfigFilePath; var getLoginConfig = function (configPath, env) { var data = (0, yaml_1.parse)(fs_1.default.readFileSync(configPath, { encoding: 'utf8' })); var currentEnv = data.environments.find(function (environment) { return environment.name === env; }); if (!currentEnv) { console.log(chalk_1.default.red('error: you should set an available environment before logging in')); process.exit(-1); } return { realm: process.env.EXTR_REALM, url: currentEnv.baseUrl.replace('api', "auth.app-".concat(currentEnv.app)), clientId: currentEnv.auth.client, }; }; exports.getLoginConfig = getLoginConfig; var writeConfigFile = function (appName) { try { var configPathFile = (0, exports.getConfigFilePath)(appName); fs_1.default.writeFileSync(configPathFile, (0, yaml_1.stringify)({ environments: [] })); console.log("Config file ".concat(configPathFile, " was successfully created")); } catch (e) { console.log(chalk_1.default.red('Error: cannot add new application')); } }; exports.writeConfigFile = writeConfigFile;