UNPKG

esa-cli

Version:

A CLI for operating Alibaba Cloud ESA Functions and Pages.

156 lines (155 loc) 7.53 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 { execSync } from 'child_process'; import fs from 'fs'; import chalk from 'chalk'; import t from '../i18n/index.js'; import api from '../libs/api.js'; import { ApiService } from '../libs/apiService.js'; import { isInstalledGit } from '../libs/git/index.js'; import logger from '../libs/logger.js'; import { getRoot } from '../utils/fileUtils/base.js'; import { getCliConfig, projectConfigPath } from '../utils/fileUtils/index.js'; import { validateCredentials } from '../utils/validateCredentials.js'; import { getRoutineDetails } from './common/utils.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 project successfully`)); } else { logger.error(t('utils_bind_error', { domain }).d(`Binding domain ${domain} to project failed`)); } } else { logger.error(t('utils_domain_error').d('Domain is not active')); } }); export function validName(name) { return /^[a-zA-Z0-9-_]+$/.test(name); } // Validate if domain is valid 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* () { var _a, _b; const cliConfig = getCliConfig(); let accessKeyId = process.env.ESA_ACCESS_KEY_ID || ((_a = cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.auth) === null || _a === void 0 ? void 0 : _a.accessKeyId); let accessKeySecret = process.env.ESA_ACCESS_KEY_SECRET || ((_b = cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.auth) === null || _b === void 0 ? void 0 : _b.accessKeySecret); if (accessKeyId && accessKeySecret) { const result = yield validateCredentials(accessKeyId, accessKeySecret); const server = yield ApiService.getInstance(); if (result.valid) { server.updateConfig({ auth: { accessKeyId, accessKeySecret }, endpoint: result.endpoint }); api.updateConfig({ auth: { accessKeyId, accessKeySecret }, endpoint: result.endpoint }); return true; } } const namedCommand = chalk.green('esa-cli login'); 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) { // Build a regex that allows subdomains and arbitrary paths // For example, match URLs like *.example.com/* return route.includes(domain); } export function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& represents the entire matched string } 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.toString() }; }); }); export const getRoutineCodeVersions = (projectName) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; const routineDetail = yield getRoutineDetails(projectName); const req = { name: projectName }; const res = yield api.listRoutineCodeVersions(req); const allVersions = (_b = (_a = res.body) === null || _a === void 0 ? void 0 : _a.codeVersions) !== null && _b !== void 0 ? _b : []; const stagingVersions = ((_g = (_f = (_e = (_d = (_c = routineDetail === null || routineDetail === void 0 ? void 0 : routineDetail.data) === null || _c === void 0 ? void 0 : _c.Envs) === null || _d === void 0 ? void 0 : _d.find((item) => item.Env === 'staging')) === null || _e === void 0 ? void 0 : _e.CodeDeploy) === null || _f === void 0 ? void 0 : _f.CodeVersions) === null || _g === void 0 ? void 0 : _g.map((item) => item.CodeVersion)) || []; const productionVersions = ((_m = (_l = (_k = (_j = (_h = routineDetail === null || routineDetail === void 0 ? void 0 : routineDetail.data) === null || _h === void 0 ? void 0 : _h.Envs) === null || _j === void 0 ? void 0 : _j.find((item) => item.Env === 'production')) === null || _k === void 0 ? void 0 : _k.CodeDeploy) === null || _l === void 0 ? void 0 : _l.CodeVersions) === null || _m === void 0 ? void 0 : _m.map((item) => item.CodeVersion)) || []; return { allVersions, stagingVersions, productionVersions }; });