|
| 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 utils_1 = require("../../utils"); |
| 17 | +const error_1 = require("../../error"); |
| 18 | +const create_1 = require("./create"); |
| 19 | +const delete_1 = require("./delete"); |
| 20 | +const query_1 = require("./query"); |
| 21 | +const domain_bind_1 = require("./domain-bind"); |
| 22 | +const domain_unbind_1 = require("./domain-unbind"); |
| 23 | +const domain_query_1 = require("./domain-query"); |
| 24 | +function getGatewayContext(envId, configPath) { |
| 25 | + return __awaiter(this, void 0, void 0, function* () { |
| 26 | + const cloudBaseConfig = yield utils_1.resolveCloudBaseConfig(configPath); |
| 27 | + const assignEnvId = envId || cloudBaseConfig.envId; |
| 28 | + if (!assignEnvId) { |
| 29 | + throw new error_1.CloudBaseError('未识别到有效的环境 Id 变量,请在项目根目录进行操作或通过 envId 参数指定环境 Id'); |
| 30 | + } |
| 31 | + const ctx = { |
| 32 | + envId: assignEnvId, |
| 33 | + config: cloudBaseConfig |
| 34 | + }; |
| 35 | + return ctx; |
| 36 | + }); |
| 37 | +} |
| 38 | +const commands = [ |
| 39 | + { |
| 40 | + cmd: 'gateway:create <gatewayPath> [envId]', |
| 41 | + options: [ |
| 42 | + { |
| 43 | + flags: '-f, --function <function>', |
| 44 | + desc: '创建云函数网关.' |
| 45 | + } |
| 46 | + ], |
| 47 | + desc: '创建云函数网关.', |
| 48 | + handler: (gatewayPath, envId, options) => __awaiter(void 0, void 0, void 0, function* () { |
| 49 | + const { configFile } = options.parent; |
| 50 | + const ctx = yield getGatewayContext(envId, configFile); |
| 51 | + yield create_1.createGw(ctx, gatewayPath, options); |
| 52 | + }) |
| 53 | + }, |
| 54 | + { |
| 55 | + cmd: 'gateway:delete [envId]', |
| 56 | + options: [ |
| 57 | + { |
| 58 | + flags: '-p, --gateway-path <gatewayPath>', |
| 59 | + desc: 'API Path' |
| 60 | + }, |
| 61 | + { |
| 62 | + flags: '-i, --gateway-id <gatewayId>', |
| 63 | + desc: 'API id' |
| 64 | + } |
| 65 | + ], |
| 66 | + desc: '删除云函数网关.', |
| 67 | + handler: (envId, options) => __awaiter(void 0, void 0, void 0, function* () { |
| 68 | + const { configFile } = options.parent; |
| 69 | + const ctx = yield getGatewayContext(envId, configFile); |
| 70 | + yield delete_1.deleteGw(ctx, options); |
| 71 | + }) |
| 72 | + }, |
| 73 | + { |
| 74 | + cmd: 'gateway:list [envId]', |
| 75 | + options: [ |
| 76 | + { |
| 77 | + flags: '-d, --domain <domain>', |
| 78 | + desc: '域名' |
| 79 | + }, |
| 80 | + { |
| 81 | + flags: '-p, --gateway-path <gatewayPath>', |
| 82 | + desc: 'API Path' |
| 83 | + }, |
| 84 | + { |
| 85 | + flags: '-i, --gateway-id <gatewayId>', |
| 86 | + desc: 'API id' |
| 87 | + } |
| 88 | + ], |
| 89 | + desc: '查询云函数网关.', |
| 90 | + handler: (envId, options) => __awaiter(void 0, void 0, void 0, function* () { |
| 91 | + const { configFile } = options.parent; |
| 92 | + const ctx = yield getGatewayContext(envId, configFile); |
| 93 | + yield query_1.queryGw(ctx, options); |
| 94 | + }) |
| 95 | + }, |
| 96 | + { |
| 97 | + cmd: 'gateway:domain:bind <customDomain> [envId]', |
| 98 | + options: [], |
| 99 | + desc: '绑定自定义网关域名.', |
| 100 | + handler: (customDomain, envId, options) => __awaiter(void 0, void 0, void 0, function* () { |
| 101 | + const { configFile } = options.parent; |
| 102 | + const ctx = yield getGatewayContext(envId, configFile); |
| 103 | + yield domain_bind_1.bindGwDomain(ctx, customDomain); |
| 104 | + }) |
| 105 | + }, |
| 106 | + { |
| 107 | + cmd: 'gateway:domain:unbind <customDomain> [envId]', |
| 108 | + options: [], |
| 109 | + desc: '解绑自定义网关名.', |
| 110 | + handler: (customDomain, envId, options) => __awaiter(void 0, void 0, void 0, function* () { |
| 111 | + const { configFile } = options.parent; |
| 112 | + const ctx = yield getGatewayContext(envId, configFile); |
| 113 | + yield domain_unbind_1.unbindGwDomain(ctx, customDomain); |
| 114 | + }) |
| 115 | + }, |
| 116 | + { |
| 117 | + cmd: 'gateway:domain:list [envId]', |
| 118 | + options: [ |
| 119 | + { |
| 120 | + flags: '-d, --domain <domain>', |
| 121 | + desc: '域名' |
| 122 | + } |
| 123 | + ], |
| 124 | + desc: '查询自定义网关域名.', |
| 125 | + handler: (envId, options) => __awaiter(void 0, void 0, void 0, function* () { |
| 126 | + const { configFile } = options.parent; |
| 127 | + const ctx = yield getGatewayContext(envId, configFile); |
| 128 | + yield domain_query_1.queryGwDomain(ctx, options); |
| 129 | + }) |
| 130 | + } |
| 131 | +]; |
| 132 | +commands.forEach(item => { |
| 133 | + let instance = commander_1.default.command(item.cmd); |
| 134 | + item.options.forEach(option => { |
| 135 | + instance = instance.option(option.flags, option.desc); |
| 136 | + }); |
| 137 | + instance.description(item.desc); |
| 138 | + instance.action(item.handler); |
| 139 | +}); |
0 commit comments