UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA Functions and Pages.

104 lines (103 loc) 4.42 kB
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()); }); }; import Table from 'cli-table3'; import t from '../../i18n/index.js'; import api from '../../libs/api.js'; import logger from '../../libs/logger.js'; import { validRoutine } from '../../utils/checkIsRoutineCreated.js'; import { getProjectConfig } from '../../utils/fileUtils/index.js'; import { checkDirectory, checkIsLoginSuccess } from '../utils.js'; import { transferRuleStringToRoute } from './helper.js'; const listRoute = { command: 'list', describe: `🔍 ${t('route_list_describe').d('List all related routes')}`, handler: () => __awaiter(void 0, void 0, void 0, function* () { handleListRoutes(); }) }; export default listRoute; export function handleListRoutes() { return __awaiter(this, void 0, void 0, function* () { var _a; if (!checkDirectory()) { return; } const projectConfig = getProjectConfig(); if (!projectConfig) return logger.notInProject(); const isSuccess = yield checkIsLoginSuccess(); if (!isSuccess) return; yield validRoutine(projectConfig.name); const req = { routineName: projectConfig.name }; const res = yield api.listRoutineRoutes(req); const configs = ((_a = res.body) === null || _a === void 0 ? void 0 : _a.configs) || []; if (configs.length === 0) { logger.warn(`🙅 ${t('route_list_empty').d('No related routes found')}`); return; } const simpleRoutes = configs .filter((item) => item.mode !== 'custom') .map((config) => { var _a, _b, _c; return { RouteName: (_a = config.routeName) !== null && _a !== void 0 ? _a : '', Route: transferRuleStringToRoute((_b = config.rule) !== null && _b !== void 0 ? _b : ''), SiteName: (_c = config.siteName) !== null && _c !== void 0 ? _c : '' }; }); if (simpleRoutes.length > 0) { logger.log(`📃 ${t('route_list_simple_title').d('Related simple mode routes')}:`); displayRelatedRouteList(simpleRoutes); } const customRoutes = configs .filter((item) => item.mode === 'custom') .map((config) => { var _a, _b, _c; return { RouteName: (_a = config.routeName) !== null && _a !== void 0 ? _a : '', Route: (_b = config.rule) !== null && _b !== void 0 ? _b : '', SiteName: (_c = config.siteName) !== null && _c !== void 0 ? _c : '' }; }); if (customRoutes.length > 0) { logger.log(`📃 ${t('route_list_custom_title').d('Related custom mode routes')}:`); displayRelatedRouteRuleList(customRoutes); } }); } export function displayRelatedRouteList(routeList) { return __awaiter(this, void 0, void 0, function* () { const table = new Table({ head: ['Route Name', 'Route', 'Site'], colWidths: [20] }); for (let i = 0; i < routeList.length; i++) { const route = routeList[i]; table.push([route.RouteName, route.Route, route.SiteName]); } console.log(table.toString()); }); } export function displayRelatedRouteRuleList(routeList) { return __awaiter(this, void 0, void 0, function* () { const table = new Table({ head: ['Route Name', 'Rule', 'Site'], colWidths: [20] }); for (let i = 0; i < routeList.length; i++) { const route = routeList[i]; table.push([route.RouteName, route.Route, route.SiteName]); } console.log(table.toString()); }); }