|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 12 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 13 | +}; |
| 14 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 15 | +const commander_1 = __importDefault(require("commander")); |
| 16 | +const env_1 = require("../../env"); |
| 17 | +const utils_1 = require("../../utils"); |
| 18 | +const error_1 = require("../../error"); |
| 19 | +const logger_1 = require("../../logger"); |
| 20 | +commander_1.default |
| 21 | + .command('env:list') |
| 22 | + .description('展示云开发环境信息') |
| 23 | + .action(function () { |
| 24 | + return __awaiter(this, void 0, void 0, function* () { |
| 25 | + const data = yield env_1.listEnvs(); |
| 26 | + const head = [ |
| 27 | + 'Alias', |
| 28 | + 'EnvId', |
| 29 | + 'PackageName', |
| 30 | + 'Source', |
| 31 | + 'CreateTime', |
| 32 | + 'Status' |
| 33 | + ]; |
| 34 | + const sortData = data.sort((prev, next) => { |
| 35 | + if (prev.Alias > next.Alias) { |
| 36 | + return 1; |
| 37 | + } |
| 38 | + if (prev.Alias < next.Alias) { |
| 39 | + return -1; |
| 40 | + } |
| 41 | + return 0; |
| 42 | + }); |
| 43 | + const tableData = sortData.map(item => [ |
| 44 | + item.Alias, |
| 45 | + item.EnvId, |
| 46 | + item.PackageName, |
| 47 | + item.Source === 'miniapp' ? '小程序' : '云开发', |
| 48 | + item.CreateTime, |
| 49 | + item.Status === 'NORMAL' ? '正常' : '不可用' |
| 50 | + ]); |
| 51 | + utils_1.printCliTable(head, tableData); |
| 52 | + const unavailableEnv = data.find(item => item.Status === 'UNAVAILABLE'); |
| 53 | + if (unavailableEnv) { |
| 54 | + logger_1.warnLog(`您的环境中存在不可用的环境:[${unavailableEnv.EnvId}],请留意!`); |
| 55 | + } |
| 56 | + }); |
| 57 | +}); |
| 58 | +function checkEnvAvailability(envId) { |
| 59 | + return __awaiter(this, void 0, void 0, function* () { |
| 60 | + const MAX_TRY = 10; |
| 61 | + let retry = 0; |
| 62 | + return new Promise((resolve, reject) => { |
| 63 | + const timer = setInterval(() => __awaiter(this, void 0, void 0, function* () { |
| 64 | + const envInfo = yield env_1.getEnvInfo(envId); |
| 65 | + if (envInfo.Status === 'NORMAL') { |
| 66 | + clearInterval(timer); |
| 67 | + resolve(); |
| 68 | + } |
| 69 | + else { |
| 70 | + retry++; |
| 71 | + } |
| 72 | + if (retry > MAX_TRY) { |
| 73 | + reject(new error_1.CloudBaseError('环境初始化查询超时,请稍后通过 cloudbase env:list 查看环境状态')); |
| 74 | + } |
| 75 | + }), 1000); |
| 76 | + }); |
| 77 | + }); |
| 78 | +} |
| 79 | +commander_1.default |
| 80 | + .command('env:create <alias>') |
| 81 | + .description('创建新的云开发环境') |
| 82 | + .action(function (alias) { |
| 83 | + return __awaiter(this, void 0, void 0, function* () { |
| 84 | + if (!alias) { |
| 85 | + throw new error_1.CloudBaseError('环境名称不能为空!'); |
| 86 | + } |
| 87 | + const loading = utils_1.loadingFactory(); |
| 88 | + loading.start('创建环境中'); |
| 89 | + const res = yield env_1.createEnv({ |
| 90 | + alias |
| 91 | + }); |
| 92 | + loading.succeed('创建环境成功!'); |
| 93 | + loading.start('环境初始化中'); |
| 94 | + if (res.Status === 'NORMAL') { |
| 95 | + loading.start('环境初始化成功'); |
| 96 | + return; |
| 97 | + } |
| 98 | + try { |
| 99 | + yield checkEnvAvailability(res.EnvId); |
| 100 | + loading.succeed('环境初始化成功'); |
| 101 | + } |
| 102 | + catch (e) { |
| 103 | + loading.fail(e.message); |
| 104 | + } |
| 105 | + }); |
| 106 | +}); |
| 107 | +commander_1.default |
| 108 | + .command('env:rename <name> [envId]') |
| 109 | + .description('重命名云开发环境') |
| 110 | + .action(function (name, envId, options) { |
| 111 | + return __awaiter(this, void 0, void 0, function* () { |
| 112 | + if (!name) { |
| 113 | + throw new error_1.CloudBaseError('环境名称不能为空!'); |
| 114 | + } |
| 115 | + const { configFile } = options.parent; |
| 116 | + const assignEnvId = yield utils_1.getEnvId(envId, configFile); |
| 117 | + yield env_1.updateEnvInfo({ |
| 118 | + envId: assignEnvId, |
| 119 | + alias: name |
| 120 | + }); |
| 121 | + logger_1.successLog('更新环境名成功 !'); |
| 122 | + }); |
| 123 | +}); |
0 commit comments