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

Commit 32be6b9

Browse files
committed
[update] optimize code
1 parent de554c1 commit 32be6b9

47 files changed

Lines changed: 470 additions & 372 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/tcb.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
#!/usr/bin/env node
2-
const logSymbols = require('log-symbols')
3-
4-
console.log(logSymbols.error + ' ' + '[tcb] 命令已废弃,请使用 [cloudbase] 命令!')
2+
require('./cloudbase')

lib/commands/env/base.js

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ commander_1.default
2323
.action(function () {
2424
return __awaiter(this, void 0, void 0, function* () {
2525
const data = yield env_1.listEnvs();
26-
const head = [
27-
'Alias',
28-
'EnvId',
29-
'PackageName',
30-
'Source',
31-
'CreateTime',
32-
'Status'
33-
];
26+
const head = ['Alias', 'EnvId', 'PackageName', 'Source', 'CreateTime', 'Status'];
3427
const sortData = data.sort((prev, next) => {
3528
if (prev.Alias > next.Alias) {
3629
return 1;
@@ -43,7 +36,7 @@ commander_1.default
4336
const tableData = sortData.map(item => [
4437
item.Alias,
4538
item.EnvId,
46-
item.PackageName,
39+
item.PackageName || '空',
4740
item.Source === 'miniapp' ? '小程序' : '云开发',
4841
item.CreateTime,
4942
item.Status === 'NORMAL' ? '正常' : '不可用'
@@ -55,55 +48,6 @@ commander_1.default
5548
}
5649
});
5750
});
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-
});
10751
commander_1.default
10852
.command('env:rename <name>')
10953
.option('-e, --envId <envId>', '环境 Id')

lib/commands/env/create.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 inquirer_1 = __importDefault(require("inquirer"));
17+
const env_1 = require("../../env");
18+
const utils_1 = require("../../utils");
19+
const error_1 = require("../../error");
20+
commander_1.default
21+
.command('env:create <alias>')
22+
.description('创建云开发免费环境')
23+
.action(function (alias) {
24+
return __awaiter(this, void 0, void 0, function* () {
25+
if (!alias) {
26+
throw new error_1.CloudBaseError('环境名称不能为空!');
27+
}
28+
const loading = utils_1.loadingFactory();
29+
loading.start('检查中...');
30+
const { CurrentFreeEnvNum, MaxFreeEnvNum, CurrentEnvNum, MaxEnvNum } = yield env_1.getEnvLimit();
31+
loading.stop();
32+
if (+CurrentFreeEnvNum >= +MaxFreeEnvNum) {
33+
const link = utils_1.genClickableLink('https://console.cloud.tencent.com/tcb');
34+
throw new error_1.CloudBaseError(`免费环境数量已达上限,无法创建免费的环境,请到云开发-控制台中创建付费环境\n👉 ${link}`);
35+
}
36+
if (+CurrentEnvNum >= +MaxEnvNum) {
37+
throw new error_1.CloudBaseError('环境数量已达上限,无法创建新的环境!');
38+
}
39+
const { payment } = yield inquirer_1.default.prompt({
40+
type: 'list',
41+
name: 'payment',
42+
choices: [
43+
{
44+
name: '按量计费',
45+
value: 'postpay'
46+
},
47+
{
48+
name: '包年包月',
49+
value: 'prepay'
50+
}
51+
],
52+
message: '请选择环境计费模式:',
53+
default: 'postpay'
54+
});
55+
loading.start('环境创建中...');
56+
try {
57+
const res = yield env_1.createEnv({
58+
alias,
59+
paymentMode: payment
60+
});
61+
}
62+
catch (e) {
63+
if (e.code === 'ResourceInsufficient') {
64+
throw new error_1.CloudBaseError('环境数量已达上限,无法创建新的环境!');
65+
}
66+
throw e;
67+
}
68+
loading.succeed('创建环境成功,初始化预计需要花费 3 分钟!');
69+
});
70+
});

lib/commands/env/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
require("./base");
44
require("./login");
55
require("./domain");
6+
require("./create");

lib/commands/functions/code-download.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const path_1 = __importDefault(require("path"));
1616
const fs_extra_1 = __importDefault(require("fs-extra"));
1717
const inquirer_1 = __importDefault(require("inquirer"));
1818
const error_1 = require("../../error");
19-
const code_1 = require("../../function/code");
19+
const function_1 = require("../../function");
2020
const utils_1 = require("../../utils");
2121
function codeDownload(ctx, dest, options) {
2222
return __awaiter(this, void 0, void 0, function* () {
@@ -25,6 +25,19 @@ function codeDownload(ctx, dest, options) {
2525
if (!name) {
2626
throw new error_1.CloudBaseError('请指定云函数名称!');
2727
}
28+
try {
29+
yield function_1.getFunctionDetail({
30+
envId,
31+
codeSecret,
32+
functionName: name
33+
});
34+
}
35+
catch (e) {
36+
if (e.code === 'ResourceNotFound.FunctionName') {
37+
throw new error_1.CloudBaseError(`云函数 [${name}] 不存在!`);
38+
}
39+
return;
40+
}
2841
let destPath = dest;
2942
if (!destPath) {
3043
destPath = path_1.default.resolve(config.functionRoot, name);
@@ -44,7 +57,7 @@ function codeDownload(ctx, dest, options) {
4457
}
4558
const loading = utils_1.loadingFactory();
4659
loading.start('文件下载中...');
47-
yield code_1.downloadFunctionCode({
60+
yield function_1.downloadFunctionCode({
4861
envId,
4962
functionName: name,
5063
destPath: destPath,

lib/commands/gateway/create.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
88
step((generator = generator.apply(thisArg, _arguments || [])).next());
99
});
1010
};
11-
var __importDefault = (this && this.__importDefault) || function (mod) {
12-
return (mod && mod.__esModule) ? mod : { "default": mod };
13-
};
1411
Object.defineProperty(exports, "__esModule", { value: true });
15-
const chalk_1 = __importDefault(require("chalk"));
1612
const error_1 = require("../../error");
1713
const gateway_1 = require("../../gateway");
1814
const function_1 = require("../../function");
@@ -41,8 +37,8 @@ function createGw(ctx, commandOptions) {
4137
path: servicePath,
4238
functionName
4339
});
44-
const endpoint = `https://${envId}.service.tcloudbase.com${servicePath}`;
45-
loading.succeed(`云函数 HTTP service 创建成功!\n${chalk_1.default.bold.underline(endpoint)}`);
40+
const link = utils_1.genClickableLink(`https://${envId}.service.tcloudbase.com${servicePath}`);
41+
loading.succeed(`云函数 HTTP service 创建成功!\n点击访问> ${link})}`);
4642
}
4743
catch (e) {
4844
loading.stop();

lib/commands/hosting.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1212
return (mod && mod.__esModule) ? mod : { "default": mod };
1313
};
1414
Object.defineProperty(exports, "__esModule", { value: true });
15-
const chalk_1 = __importDefault(require("chalk"));
1615
const commander_1 = __importDefault(require("commander"));
1716
const inquirer_1 = __importDefault(require("inquirer"));
1817
const hosting_1 = require("../hosting");
@@ -38,23 +37,24 @@ commander_1.default
3837
const res = yield hosting_1.getHostingInfo({ envId: assignEnvId });
3938
const website = res.data && res.data[0];
4039
if (!website) {
41-
throw new error_1.CloudBaseError('您还没有开启静态网站服务,请先到云开发控制台开启静态网站服务!\n 👉 https://console.cloud.tencent.com/tcb');
40+
const link = utils_1.genClickableLink('https://console.cloud.tencent.com/tcb');
41+
throw new error_1.CloudBaseError(`您还没有开启静态网站服务,请先到云开发控制台开启静态网站服务!\n 👉 ${link}`);
4242
}
43-
const url = `https://${website.cdnDomain}`;
43+
const link = utils_1.genClickableLink(`https://${website.cdnDomain}`);
4444
if (website.status !== 'offline') {
45-
console.log(`静态网站域名:${chalk_1.default.bold.underline(url)}`);
45+
console.log(`静态网站域名:${link}`);
4646
}
4747
console.log(`静态网站状态:【${HostingStatusMap[website.status]}】`);
4848
}));
4949
commander_1.default
5050
.command('hosting:deploy [filePath] [cloudPath]')
5151
.option('-e, --envId <envId>', '环境 Id')
5252
.description('部署静态网站文件')
53-
.action((filePath, cloudPath = '', options) => __awaiter(void 0, void 0, void 0, function* () {
53+
.action((filePath = '.', cloudPath = '', options) => __awaiter(void 0, void 0, void 0, function* () {
5454
const { parent: { configFile }, envId } = options;
5555
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
5656
const isDir = utils_1.isDirectory(filePath);
57-
console.log('文件部署中...');
57+
console.log('> 文件部署中...');
5858
try {
5959
const onProgress = utils_1.createOnProgressBar(() => {
6060
logger_1.successLog('文件部署成功!');

lib/commands/init.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ function copyServerTemplate(projectPath) {
5050
function initSuccessOutput(projectName) {
5151
logger_1.successLog(`创建项目 ${projectName} 成功!\n`);
5252
const command = chalk_1.default.bold.cyan(`cd ${projectName}`);
53-
console.log(`👉 运行 ${command} 开始您的项目!\n`);
54-
console.log('🎉 欢迎贡献你的模板 👉 https://github.com/TencentCloudBase/cloudbase-templates');
53+
console.log(`👉 执行命令 ${command} 进入项目文件夹!\n`);
54+
console.log(`👉 执行命令 ${chalk_1.default.bold.cyan('cloudbase functions:deploy app')} 部署云函数\n`);
55+
const link = utils_1.genClickableLink('https://github.com/TencentCloudBase/cloudbase-templates');
56+
console.log(`🎉 欢迎贡献你的模板 👉 ${link}`);
5557
}
5658
commander_1.default
5759
.command('init')
@@ -71,13 +73,14 @@ commander_1.default
7173
}
7274
loading.stop();
7375
const envs = envData
76+
.filter(item => item.Status === 'NORMAL')
7477
.map(item => ({
75-
name: `${item.Alias} - [${item.EnvId}:${item.PackageName}]`,
78+
name: `${item.Alias} - [${item.EnvId}:${item.PackageName || '空'}]`,
7679
value: item.EnvId
7780
}))
7881
.sort();
7982
if (!envs.length) {
80-
throw new error_1.CloudBaseError('没有可以使用的环境,请先开通云开发服务并创建环境(https://console.cloud.tencent.com/tcb)');
83+
throw new error_1.CloudBaseError('没有可以使用的环境,请使用 cloudbase env:create $name 命令创建免费环境!');
8184
}
8285
const { env } = yield inquirer_1.default.prompt({
8386
type: 'list',
@@ -94,7 +97,7 @@ commander_1.default
9497
const { lang } = yield inquirer_1.default.prompt({
9598
type: 'list',
9699
name: 'lang',
97-
message: '选择模板语言',
100+
message: '选择开发语言',
98101
choices: ['PHP', 'Java', 'Node']
99102
});
100103
loading.start('拉取云开发模板列表中');

lib/commands/login.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const logger_1 = require("../logger");
2222
commander_1.default
2323
.command('login')
2424
.option('-k, --key', '使用永久密钥登录')
25-
.option('--skey', '使用永久密钥 + skey 登录')
2625
.description('登录腾讯云账号')
2726
.action(function (options) {
2827
return __awaiter(this, void 0, void 0, function* () {
@@ -36,8 +35,7 @@ commander_1.default
3635
else {
3736
loading.stop();
3837
}
39-
let skey;
40-
if (options.key || options.skey) {
38+
if (options.key) {
4139
const { secretId } = yield inquirer_1.default.prompt({
4240
type: 'input',
4341
name: 'secretId',
@@ -48,14 +46,6 @@ commander_1.default
4846
name: 'secretKey',
4947
message: '请输入腾讯云 SecretKey:'
5048
});
51-
if (options.skey) {
52-
const { skey: _skey } = yield inquirer_1.default.prompt({
53-
type: 'input',
54-
name: 'skey',
55-
message: '请输入腾讯云 skey(选填):'
56-
});
57-
skey = _skey;
58-
}
5949
if (!secretId || !secretKey) {
6050
throw new error_1.CloudBaseError('SecretID 或 SecretKey 不能为空');
6151
}
@@ -88,14 +78,12 @@ commander_1.default
8878
try {
8979
const envs = yield env_1.listEnvs();
9080
if (!envs.length) {
91-
logger_1.warnLog('你还没有可用的环境,请使用 cloudbase env:create alias 创建环境');
81+
logger_1.warnLog('您还没有可用的环境,请使用 cloudbase env:create $name 创建环境');
9282
}
9383
}
9484
catch (e) {
9585
if (e.code === 'ResourceNotFound.UserNotExists') {
96-
loading.start('初始化云开发服务');
97-
yield env_1.initTcb(skey);
98-
loading.succeed('初始化成功!');
86+
logger_1.errorLog('您还没有初始化云开发服务!');
9987
}
10088
else {
10189
throw e;

0 commit comments

Comments
 (0)