UNPKG

esa-cli

Version:

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

141 lines (140 loc) 5.79 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 fs from 'fs'; import { execSync } from 'child_process'; import { isInstalledGit } from '../libs/git/index.js'; import { getCliConfig, projectConfigPath } from '../utils/fileUtils/index.js'; import { getRoot } from '../utils/fileUtils/base.js'; import chalk from 'chalk'; import t from '../i18n/index.js'; import { ApiService } from '../libs/apiService.js'; import logger from '../libs/logger.js'; import api from '../libs/api.js'; export const checkDirectory = (isCheckGit = false) => { const root = getRoot(); if (fs.existsSync(projectConfigPath)) { try { if (isCheckGit && isInstalledGit()) { const gitStatus = execSync('git status --porcelain', { cwd: root }).toString(); if (gitStatus) { logger.warn(t('utils_git_not_commit').d('There are uncommitted changes in the repository.')); return false; } } } catch (error) { logger.error(`${t('utils_git_error').d('An error occurred while checking the Git status')}: ${error}`); return false; } return true; } else { logger.notInProject(); return false; } }; export const bindRoutineWithDomain = (name, domain) => __awaiter(void 0, void 0, void 0, function* () { const server = yield ApiService.getInstance(); const req = { RecordName: domain }; const res = yield server.getMatchSite(req); if (res) { const record = res; const createReq = { Name: name, SiteId: record.data.SiteId, SiteName: record.data.SiteName, RecordName: domain }; const response = yield server.createRoutineRelatedRecord(createReq); const isBindSuccess = (response === null || response === void 0 ? void 0 : response.data.Status) === 'OK'; if (isBindSuccess) { logger.success(t('utils_bind_success', { domain }).d(`Binding domain ${domain} to routine successfully`)); } else { logger.error(t('utils_bind_error', { domain }).d(`Binding domain ${domain} to routine failed`)); } } else { logger.error(t('utils_domain_error').d('Domain is not active')); } }); export const getRoutineVersionList = (name) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; const req = { name }; const res = yield api.listRoutineCodeVersions(req); return (_b = (_a = res.body) === null || _a === void 0 ? void 0 : _a.codeVersions) !== null && _b !== void 0 ? _b : []; }); export function validName(name) { return /^[a-zA-Z0-9-_]+$/.test(name); } // 校验域名是否有效 export function validDomain(domain) { return /^(?:[a-z0-9-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/.test(domain); } export function checkIsLoginSuccess() { return __awaiter(this, void 0, void 0, function* () { const cliConfig = getCliConfig(); const namedCommand = chalk.green('esa login'); if (!cliConfig || !cliConfig.auth) { return false; } if (!cliConfig.auth.accessKeyId || !cliConfig.auth.accessKeySecret) { logger.log(`❌ ${t('utils_login_error').d('Maybe you are not logged in yet.')}`); logger.log(`🔔 ${t('utils_login_error_config', { namedCommand }).d(`Please run command to login: ${namedCommand}`)}`); return false; } const server = yield ApiService.getInstance(); server.updateConfig(cliConfig); const res = yield server.checkLogin(); if (res.success) { return true; } logger.log(res.message || ''); logger.log(`❌ ${t('utils_login_error').d('Maybe you are not logged in yet.')}`); logger.log(`🔔 ${t('utils_login_error_config', { namedCommand }).d(`Please run command to login: ${namedCommand}`)}`); return false; }); } export function isValidRouteForDomain(route, domain) { // 构建一个允许子域和任意路径的正则表达式 // 例如,匹配形式如 *.example.com/* 的URL return route.includes(domain); } export function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& 表示整个被匹配的字符串 } export const getAllSites = () => __awaiter(void 0, void 0, void 0, function* () { var _a; const server = yield ApiService.getInstance(); const res = []; while (true) { const req = { SiteSearchType: 'fuzzy', Status: 'active', PageNumber: res.length + 1, PageSize: 500 }; const response = yield server.listSites(req); if ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.Sites) { res.push(...response.data.Sites); } else { break; } } return res.map((site) => { return { label: site.SiteName, value: site.SiteId }; }); });