Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 35920ab

Browse files
committed
feat: support wechat secret bind & refactor function deploy build
1 parent 20b7aba commit 35920ab

27 files changed

Lines changed: 1237 additions & 726 deletions

docs/api.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ deleted: number
142142
functionName: string,
143143
force: boolean,
144144
// 函数代码 base64 编码形式
145-
zipFile: base64String,
145+
base64Code: base64String,
146146
func: {
147147
// 函数名
148148
name: 'app',
@@ -242,7 +242,6 @@ deleted: number
242242
Handler: 'index.main',
243243
CodeSize: 636,
244244
Timeout: 5,
245-
FunctionVersion: '$LATEST',
246245
MemorySize: 256,
247246
Runtime: 'Nodejs8.9',
248247
FunctionName: 'app',

lib/commands/env-login.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
const commander_1 = __importDefault(require("commander"));
7+
const inquirer_1 = __importDefault(require("inquirer"));
8+
const utils_1 = require("../utils");
9+
const logger_1 = require("../logger");
10+
const env_1 = require("../env");
11+
commander_1.default
12+
.command('env:login:list [envId]')
13+
.description('列出环境登录配置')
14+
.action(async function (envId) {
15+
const assignEnvId = await utils_1.getEnvId(envId);
16+
const configList = await env_1.getLoginConfigList({
17+
envId: assignEnvId
18+
});
19+
const platformMap = {
20+
'WECHAT-OPEN': '微信开放平台',
21+
'WECHAT-PUBLIC': '微信公众平台'
22+
};
23+
const head = ['Id', 'Platform', 'CreateTime', 'Status'];
24+
const tableData = configList.map(item => [
25+
item.Id,
26+
platformMap[item.Platform]
27+
? platformMap[item.Platform]
28+
: item.Platform,
29+
item.CreateTime,
30+
item.Status === 'ENABLE' ? '启用' : '禁用中'
31+
]);
32+
utils_1.printCliTable(head, tableData);
33+
});
34+
commander_1.default
35+
.command('env:login:config [envId]')
36+
.description('配置环境登录方式')
37+
.action(async function (envId) {
38+
const assignEnvId = await utils_1.getEnvId(envId);
39+
const configList = await env_1.getLoginConfigList({
40+
envId: assignEnvId
41+
});
42+
const { type, status } = await inquirer_1.default.prompt([
43+
{
44+
type: 'list',
45+
name: 'type',
46+
choices: ['微信公众平台', '微信开放平台'],
47+
message: '请选择登录方式:',
48+
default: '微信公众平台'
49+
},
50+
{
51+
type: 'list',
52+
name: 'status',
53+
choices: ['启用', '禁用'],
54+
message: '请选择登录方式状态:',
55+
default: '启用'
56+
}
57+
]);
58+
const platformMap = {
59+
微信开放平台: 'WECHAT-OPEN',
60+
微信公众平台: 'WECHAT-PUBLIC'
61+
};
62+
const platform = platformMap[type];
63+
const item = configList.find(item => item.Platform === platform);
64+
if (status === '禁用' && item) {
65+
console.log(item);
66+
await env_1.updateLoginConfig({
67+
status: status === '启用' ? 'ENABLE' : 'DISABLE',
68+
configId: item.Id,
69+
envId: assignEnvId
70+
});
71+
logger_1.successLog(`${type} 登录方式禁用成功!`);
72+
return;
73+
}
74+
const { appId, appSecret } = await inquirer_1.default.prompt([
75+
{
76+
type: 'input',
77+
name: 'appId',
78+
message: '请输入 AppId(配置状态时可不填):'
79+
},
80+
{
81+
type: 'input',
82+
name: 'appSecret',
83+
message: '请输入 AppSecret(配置状态时可不填):'
84+
}
85+
]);
86+
if (item && item.Id) {
87+
await env_1.updateLoginConfig({
88+
envId: assignEnvId,
89+
configId: item.Id,
90+
appId,
91+
appSecret
92+
});
93+
}
94+
else {
95+
await env_1.createLoginConfig({
96+
envId: assignEnvId,
97+
appId,
98+
appSecret,
99+
platform
100+
});
101+
}
102+
logger_1.successLog('配置环境登录方式成功!');
103+
});

lib/commands/functions.js

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
33
return (mod && mod.__esModule) ? mod : { "default": mod };
44
};
55
Object.defineProperty(exports, "__esModule", { value: true });
6+
const ora_1 = __importDefault(require("ora"));
67
const commander_1 = __importDefault(require("commander"));
78
const inquirer_1 = __importDefault(require("inquirer"));
89
const chalk_1 = __importDefault(require("chalk"));
910
const error_1 = require("../error");
1011
const function_1 = require("../function");
1112
const utils_1 = require("../utils");
1213
const logger_1 = require("../logger");
14+
const StatusMap = {
15+
Active: '部署完成',
16+
Creating: '创建中',
17+
CreateFailed: '创建失败',
18+
Updating: '更新中',
19+
UpdateFailed: '更新失败'
20+
};
1321
async function getConfigFunctions() {
1422
const config = await utils_1.resolveTcbrcConfig();
1523
if (!config.functions ||
@@ -60,22 +68,26 @@ commander_1.default
6068
functions,
6169
root: process.cwd(),
6270
envId: assignEnvId,
63-
force
71+
force,
72+
log: true
6473
});
6574
}
6675
const newFunction = functions.find(item => item.name === name);
6776
if (!newFunction || !newFunction.name) {
6877
throw new error_1.TcbError(`函数 ${name} 配置不存在`);
6978
}
79+
const createSpinner = ora_1.default('函数部署中...').start();
7080
try {
7181
await function_1.createFunction({
7282
func: newFunction,
7383
root: process.cwd(),
7484
envId: assignEnvId,
7585
force
7686
});
87+
createSpinner.succeed(`[${newFunction.name}] 函数部署成功!`);
7788
}
7889
catch (e) {
90+
createSpinner.stop();
7991
if (e.code === 'ResourceInUse.FunctionName') {
8092
const { force } = await inquirer_1.default.prompt({
8193
type: 'confirm',
@@ -84,12 +96,20 @@ commander_1.default
8496
default: false
8597
});
8698
if (force) {
87-
function_1.createFunction({
88-
func: newFunction,
89-
root: process.cwd(),
90-
envId: assignEnvId,
91-
force: true
92-
});
99+
createSpinner.start();
100+
try {
101+
await function_1.createFunction({
102+
func: newFunction,
103+
root: process.cwd(),
104+
envId: assignEnvId,
105+
force: true
106+
});
107+
createSpinner.succeed(`[${newFunction.name}] 函数部署成功!`);
108+
}
109+
catch (e) {
110+
createSpinner.stop();
111+
throw e;
112+
}
93113
return;
94114
}
95115
}
@@ -105,15 +125,23 @@ commander_1.default
105125
if (!name) {
106126
throw new error_1.TcbError('请指定函数名称!');
107127
}
108-
const newFunction = functions.find(item => item.name === name);
109-
if (!newFunction || !newFunction.name) {
128+
const func = functions.find(item => item.name === name);
129+
if (!func || !func.name) {
110130
throw new error_1.TcbError(`函数 ${name} 配置不存在`);
111131
}
112-
await function_1.updateFunctionCode({
113-
func: newFunction,
114-
root: process.cwd(),
115-
envId: assignEnvId
116-
});
132+
const spinner = ora_1.default(`[${func.name}] 函数代码更新中...`).start();
133+
try {
134+
await function_1.updateFunctionCode({
135+
func,
136+
root: process.cwd(),
137+
envId: assignEnvId
138+
});
139+
spinner.succeed(`[${func.name}] 函数代码更新成功!`);
140+
}
141+
catch (e) {
142+
spinner.stop();
143+
throw e;
144+
}
117145
});
118146
commander_1.default
119147
.command('functions:list [envId]')
@@ -136,11 +164,22 @@ commander_1.default
136164
offset: Number(offset),
137165
envId: assignEnvId
138166
});
139-
const head = ['Name', 'Runtime', 'AddTime', 'Description'];
140-
const tableData = data.map(item => {
141-
const { FunctionName, Runtime, AddTime, Description } = item;
142-
return [FunctionName, Runtime, AddTime, Description];
143-
});
167+
const head = [
168+
'Id',
169+
'Name',
170+
'Runtime',
171+
'AddTime',
172+
'ModTime',
173+
'Status'
174+
];
175+
const tableData = data.map(item => [
176+
item.FunctionId,
177+
item.FunctionName,
178+
item.Runtime,
179+
item.AddTime,
180+
item.ModTime,
181+
StatusMap[item.Status]
182+
]);
144183
utils_1.printCliTable(head, tableData);
145184
});
146185
commander_1.default
@@ -181,15 +220,15 @@ commander_1.default
181220
functionName: name,
182221
envId: assignEnvId
183222
});
223+
logger_1.successLog(`删除函数 [${name}] 成功!`);
184224
});
185225
function logDetail(info, name) {
186226
const ResMap = {
187227
Status: '状态',
188-
CodeSize: '代码大小',
228+
CodeSize: '代码大小(B)',
189229
Description: '描述',
190230
Environment: '环境变量(key=value)',
191231
FunctionName: '函数名称',
192-
FunctionVersion: '函数版本',
193232
Handler: '执行方法',
194233
MemorySize: '内存配置(MB)',
195234
ModTime: '修改时间',
@@ -198,10 +237,13 @@ function logDetail(info, name) {
198237
Timeout: '超时时间(S)',
199238
VpcConfig: '网络配置',
200239
Triggers: '触发器',
201-
CodeInfo: '函数代码'
240+
CodeInfo: '函数代码(Java 函数以及入口大于 1 M 的函数不会显示)'
202241
};
203242
const funcInfo = Object.keys(ResMap)
204243
.map(key => {
244+
if (key === 'Status') {
245+
return `${ResMap[key]}${StatusMap[info[key]]} \n`;
246+
}
205247
if (key === 'Environment') {
206248
const data = info[key].Variables.map(item => `${item.Key}=${item.Value}`).join('; ');
207249
return `${ResMap[key]}${data} \n`;
@@ -349,9 +391,9 @@ commander_1.default
349391
if (isBathUpdate) {
350392
await function_1.batchUpdateFunctionConfig({
351393
functions,
352-
envId: assignEnvId
394+
envId: assignEnvId,
395+
log: true
353396
});
354-
logger_1.successLog('更新云函数配置成功!');
355397
return;
356398
}
357399
const functionItem = functions.find(item => item.name === name);
@@ -363,7 +405,7 @@ commander_1.default
363405
config: functionItem.config,
364406
envId: assignEnvId
365407
});
366-
logger_1.successLog('更新云函数配置成功!');
408+
logger_1.successLog(`[${name}] 更新云函数配置成功!`);
367409
});
368410
commander_1.default
369411
.command('functions:trigger:create [functionName] [envId]')
@@ -499,18 +541,21 @@ commander_1.default
499541
if (isBatchInvoke) {
500542
return await function_1.batchInvokeFunctions({
501543
functions,
502-
envId: assignEnvId
544+
envId: assignEnvId,
545+
log: true
503546
});
504547
}
505548
const func = functions.find(item => item.name === name);
506549
if (!func) {
507550
throw new error_1.TcbError('未找到相关函数配置,请检查函数名是否正确');
508551
}
509-
await function_1.invokeFunction({
552+
const result = await function_1.invokeFunction({
510553
functionName: name,
511554
envId: assignEnvId,
512555
params: params || func.params
513556
});
557+
logger_1.successLog(`[${name}] 调用成功\n响应结果:\n`);
558+
console.log(result);
514559
});
515560
commander_1.default
516561
.command('functions:copy <functionName> <newFunctionName> [envId] [targentEnvId]')

lib/commands/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ require("./login");
55
require("./logout");
66
require("./env");
77
require("./env-domain");
8+
require("./env-login");
89
require("./functions");
910
require("./server");

lib/env/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const utils_1 = require("../utils");
77
const error_1 = require("../error");
88
const utils_2 = require("../utils");
99
__export(require("./domain"));
10+
__export(require("./login"));
1011
const tcbService = new utils_2.CloudService('tcb', '2018-06-08');
1112
async function initTcb(skey) {
1213
const res = await tcbService.request('InitTcb', {

0 commit comments

Comments
 (0)