UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA EdgeRoutine (Edge Functions).

116 lines (115 loc) 5.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 chalk from 'chalk'; import logger from '../../libs/logger.js'; import { checkDirectory, checkIsLoginSuccess, isValidRouteForDomain } from '../utils.js'; import { getProjectConfig } from '../../utils/fileUtils/index.js'; import { ApiService } from '../../libs/apiService.js'; import t from '../../i18n/index.js'; import { descriptionInput } from '../../components/descriptionInput.js'; import { promptFilterSelector } from '../../components/filterSelector.js'; import { validRoutine } from '../../utils/checkIsRoutineCreated.js'; const addRoute = { command: 'add [route] [site]', describe: `📥 ${t('route_add_describe').d('Bind a Route to a routine')}`, builder: (yargs) => { return yargs.fail((msg, err, yargsIns) => { if (err) throw err; if (msg) { console.error(msg); yargsIns.showHelp('log'); } process.exit(1); }); }, handler: (argv) => __awaiter(void 0, void 0, void 0, function* () { handlerAddRoute(argv); }) }; export function handlerAddRoute(argv) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!checkDirectory()) { return; } const projectConfig = getProjectConfig(); if (!projectConfig) return logger.notInProject(); const isSuccess = yield checkIsLoginSuccess(); if (!isSuccess) return; yield validRoutine(projectConfig.name); // input route and site const { route, site } = argv; const listSitesReq = { SiteSearchType: 'fuzzy', Status: 'active', PageNumber: 1, PageSize: 500 }; const server = yield ApiService.getInstance(); const ListSitesRes = yield server.listSites(listSitesReq); const siteList = ((ListSitesRes === null || ListSitesRes === void 0 ? void 0 : ListSitesRes.data.Sites) || []).map((i) => ({ label: i.SiteName, value: i.SiteId })); if (route && site) { const siteId = (_a = siteList.find((item) => item.label === site)) === null || _a === void 0 ? void 0 : _a.value; const req = { Name: projectConfig.name, SiteId: Number(siteId), SiteName: String(site), Route: String(route) }; const res = yield server.createRoutineRelatedRoute(req); const addSuccess = ((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.Status) === 'OK'; if (addSuccess) { logger.success(t('route_add_success').d('Add route success!')); } else { logger.error(t('route_add_fail').d('Add route fail!')); } return; } logger.warn(t('interactive_mode').d('Interactive mode')); // not input route and site, enter interactive mode logger.log(`🖊️ ${t('domain_input').d('Enter the name of domain (Support fuzzy matching on tab press):')}`); const domain = yield promptFilterSelector(siteList); const inputRoute = yield descriptionInput(`🖊️ ${t('route_input').d('Enter a Route:')} (${chalk.green(t('route_validate').d('You can add an asterisk (*) as the prefix or suffix to match more URLs, such as "*.example.com/*".'))})`, true); const ROUTE_PATTERN = /^(?:\*\.)?([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,})(\/\*|\/[^?#]*)?$/; if (!ROUTE_PATTERN.test(inputRoute)) { return logger.error(t('route_add_invalid_route').d('Invalid route')); } if (!isValidRouteForDomain(inputRoute, domain.label)) { return logger.error(t('route_site_not_match').d('The route does not correspond to the domain.')); } if (domain.value !== '') { const req = { Name: projectConfig.name, SiteId: Number(domain.value), SiteName: domain.label, Route: inputRoute }; const res = yield server.createRoutineRelatedRoute(req); const addSuccess = ((_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.Status) === 'OK'; if (addSuccess) { logger.success(t('route_add_success').d('Add route success!')); } else { logger.error(t('route_add_fail').d('Add route fail!')); } } else { logger.error(t('invalid_domain').d('Input domain is invalid')); } }); } export default addRoute;