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

Commit 73c905b

Browse files
committed
1. refactor: change tcb command to cloudbase
2. optimize: add types defination files
1 parent 42c88cc commit 73c905b

66 files changed

Lines changed: 642 additions & 165 deletions

Some content is hidden

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

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CloudBase 命令行工具 ![npm (tag)](https://img.shields.io/npm/v/@cloudbase/cli)
22

3-
CloudBase CLI 是一个开源的命令行界面交互工具,用于帮助用户快速、方便的部署项目,管理 TCB 资源
3+
CloudBase CLI 是一个开源的命令行界面交互工具,用于帮助用户快速、方便的部署项目,管理云开发资源
44

55
## 安装 CloudBase CLI
66

@@ -16,7 +16,7 @@ npm install -g @cloudbase/cli
1616
yarn global add @cloudbase/cli
1717
```
1818

19-
安装完成后,你可以使用 `tcb -V` 验证是否安装成功,如果输出了类似下面的版本号,则表明 CloudBase CLI 被成功安装到您的计算机中。
19+
安装完成后,你可以使用 `cloudbase -V` 验证是否安装成功,如果输出了类似下面的版本号,则表明 CloudBase CLI 被成功安装到您的计算机中。
2020

2121
```text
2222
0.1.5

bin/cloudbase.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env node
2+
const program = require('commander')
3+
const chalk = require('chalk')
4+
const logSymbols = require('log-symbols')
5+
const updateNotifier = require('update-notifier')
6+
const pkg = require('../package.json')
7+
8+
const isBeta = pkg.version.indexOf('-') > -1
9+
10+
// 检查更新
11+
const ONE_DAY = 86400000
12+
// Beta 版 1 个小时检查一次,稳定版 1 天检查一次
13+
const CheckInterval = isBeta ? 3600000 : ONE_DAY
14+
15+
const notifier = updateNotifier({
16+
pkg,
17+
distTag: isBeta ? 'beta' : 'latest',
18+
// 检查更新间隔 1 天
19+
updateCheckInterval: CheckInterval
20+
})
21+
22+
notifier.notify({
23+
isGlobal: true
24+
})
25+
26+
// 注册命令
27+
require('../lib')
28+
29+
program.version(pkg.version)
30+
31+
// 处理无效命令
32+
program.action(cmd => {
33+
console.log(chalk.bold.red('Error: ') + `${cmd} 不是有效的命令!`)
34+
console.log(`使用 ${chalk.bold('cloudbase -h')} 查看所有命令~`)
35+
})
36+
37+
program.on('--help', function() {
38+
const tips = `\nTips:
39+
40+
${chalk.gray('–')} 登录
41+
42+
${chalk.cyan('$ cloudbase login')}
43+
44+
${chalk.gray('–')} 初始化云开发项目
45+
46+
${chalk.cyan('$ cloudbase init')}
47+
48+
${chalk.gray('–')} 部署云函数
49+
50+
${chalk.cyan('$ cloudbase functions:deploy')}`
51+
console.log(tips)
52+
})
53+
54+
// 当没有输入任何命令时,显示帮助信息
55+
if (process.argv.length < 3) {
56+
program.outputHelp()
57+
}
58+
59+
program.parse(process.argv)
60+
61+
function errorHandler(err) {
62+
const stackIngoreErrors = ['TencentCloudSDKHttpException', 'TcbError']
63+
// 忽略自定义错误的错误栈
64+
if (err.stack && !stackIngoreErrors.includes(err.name)) {
65+
console.log(err.stack)
66+
}
67+
// 3 空格,兼容中文字符编码长度问题
68+
console.log(logSymbols.error + ' ' + err.message)
69+
}
70+
71+
process.on('uncaughtException', errorHandler)
72+
process.on('unhandledRejection', errorHandler)

bin/tcb.js

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,4 @@
11
#!/usr/bin/env node
2-
const ora = require('ora')
3-
const program = require('commander')
4-
const chalk = require('chalk')
52
const logSymbols = require('log-symbols')
6-
const updateNotifier = require('update-notifier')
7-
const pkg = require('../package.json')
83

9-
const isBeta = pkg.version.indexOf('-') > -1
10-
11-
// 检查更新
12-
const ONE_DAY = 86400000
13-
// Beta 版 1 个小时检查一次,稳定版 1 天检查一次
14-
const CheckInterval = isBeta ? 3600000 : ONE_DAY
15-
16-
const notifier = updateNotifier({
17-
pkg,
18-
distTag: isBeta ? 'beta' : 'latest',
19-
// 检查更新间隔 1 天
20-
updateCheckInterval: CheckInterval
21-
})
22-
23-
notifier.notify({
24-
isGlobal: true
25-
})
26-
27-
// 注册命令
28-
require('../lib')
29-
30-
program.version(pkg.version)
31-
32-
// 处理无效命令
33-
program.action(cmd => {
34-
console.log(chalk.bold.red('Error: ') + `${cmd} 不是有效的命令!`)
35-
console.log(`使用 ${chalk.bold('tcb -h')} 查看所有命令~`)
36-
})
37-
38-
program.on('--help', function() {
39-
const tips = `\nTips:
40-
41-
${chalk.gray('–')} 登录
42-
43-
${chalk.cyan('$ tcb login')}
44-
45-
${chalk.gray('–')} 列出环境列表
46-
47-
${chalk.cyan('$ tcb env:list')}
48-
49-
${chalk.gray('–')} 部署云函数
50-
51-
${chalk.cyan('$ tcb functions:deploy')}`
52-
console.log(tips)
53-
})
54-
55-
// 当没有输入任何命令时,显示帮助信息
56-
if (process.argv.length < 3) {
57-
program.outputHelp()
58-
}
59-
60-
program.parse(process.argv)
61-
62-
function errorHandler(err) {
63-
ora().stop()
64-
const stackIngoreErrors = ['TencentCloudSDKHttpException', 'TcbError']
65-
// 忽略自定义错误的错误栈
66-
if (err.stack && !stackIngoreErrors.includes(err.name)) {
67-
console.log(err.stack)
68-
}
69-
// 3 空格,兼容中文字符编码长度问题
70-
console.log(logSymbols.error + ' ' + err.message)
71-
}
72-
73-
process.on('uncaughtException', errorHandler)
74-
process.on('unhandledRejection', errorHandler)
4+
console.log(logSymbols.error + ' ' + '[tcb] 命令已废弃,请使用 [cloudbase] 命令!')

lib/commands/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async function checkEnvAvailability(envId) {
5050
retry++;
5151
}
5252
if (retry > MAX_TRY) {
53-
reject(new error_1.TcbError('环境初始化查询超时,请稍后通过 tcb env:list 查看环境状态'));
53+
reject(new error_1.TcbError('环境初始化查询超时,请稍后通过 cloudbase env:list 查看环境状态'));
5454
}
5555
}, 1000);
5656
});

lib/commands/functions.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const StatusMap = {
2020
UpdateFailed: '更新失败'
2121
};
2222
async function getConfigFunctions() {
23-
const config = await utils_1.resolveTcbrcConfig();
23+
const config = await utils_1.resolveCloudBaseConfig();
2424
if (!config.functions ||
2525
!Array.isArray(config.functions) ||
2626
!config.functions.length) {
@@ -49,7 +49,7 @@ commander_1.default
4949
.description('创建云函数')
5050
.action(async function (name, envId, options) {
5151
const assignEnvId = await utils_1.getEnvId(envId);
52-
const config = await utils_1.resolveTcbrcConfig();
52+
const config = await utils_1.resolveCloudBaseConfig();
5353
const functions = await getConfigFunctions();
5454
const { force } = options;
5555
let isBatchCreating = false;
@@ -123,7 +123,7 @@ commander_1.default
123123
.description('创建云函数')
124124
.action(async function (name, envId) {
125125
const assignEnvId = await utils_1.getEnvId(envId);
126-
const config = await utils_1.resolveTcbrcConfig();
126+
const config = await utils_1.resolveCloudBaseConfig();
127127
const functions = await getConfigFunctions();
128128
if (!name) {
129129
throw new error_1.TcbError('请指定函数名称!');
@@ -563,7 +563,7 @@ commander_1.default
563563
commander_1.default
564564
.command('functions:copy <functionName> <newFunctionName> [envId] [targentEnvId]')
565565
.option('--force', '如果目标环境下存在同名函数,覆盖原函数')
566-
.description('创建云函数')
566+
.description('拷贝云函数')
567567
.action(async function (functionName, newFunctionName, envId, targentEnvId, options) {
568568
const assignEnvId = await utils_1.getEnvId(envId);
569569
const { force } = options;

lib/commands/init.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ commander_1.default
1818
.description('创建并初始化一个新的项目')
1919
.action(async function (cmd) {
2020
const load = ora_1.default('拉取环境列表').start();
21-
const envData = (await env_1.listEnvs()) || [];
21+
let envData = [];
22+
try {
23+
envData = (await env_1.listEnvs()) || [];
24+
}
25+
catch (e) {
26+
load.stop();
27+
throw e;
28+
}
2229
load.succeed('获取环境列表成功');
2330
const envs = envData.map(item => `${item.EnvId}:${item.PackageName}`);
2431
if (!envs.length) {
@@ -34,7 +41,7 @@ commander_1.default
3441
type: 'input',
3542
name: 'name',
3643
message: '请输入项目名称',
37-
default: 'tcb-demo'
44+
default: 'cloudbase-demo'
3845
});
3946
const templatePath = path_1.default.resolve(__dirname, '../../templates', cmd.server ? 'server/node' : 'faas');
4047
const projectPath = path_1.default.join(process.cwd(), name);
@@ -54,7 +61,9 @@ commander_1.default
5461
}
5562
fs_extra_1.default.copySync(templatePath, projectPath);
5663
fs_1.default.renameSync(path_1.default.join(projectPath, '_gitignore'), path_1.default.join(projectPath, '.gitignore'));
57-
const configFilePath = path_1.default.join(projectPath, 'tcbrc.json');
64+
const configFileJSONPath = path_1.default.join(projectPath, 'cloudbaserc.json');
65+
const configFileJSPath = path_1.default.join(projectPath, 'cloudbaserc.js');
66+
const configFilePath = [configFileJSPath, configFileJSONPath].find(item => fs_1.default.existsSync(item));
5867
const configContent = fs_1.default.readFileSync(configFilePath).toString();
5968
fs_1.default.writeFileSync(configFilePath, configContent.replace('{{envId}}', env.split(':')[0]));
6069
logger_1.successLog(`创建项目 ${name} 成功`);

lib/commands/login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ commander_1.default
8585
try {
8686
const envs = await env_1.listEnvs();
8787
if (!envs.length) {
88-
logger_1.warnLog('你还没有可用的环境,请使用 tcb env:create alias 创建环境');
88+
logger_1.warnLog('你还没有可用的环境,请使用 cloudbase env:create alias 创建环境');
8989
}
9090
}
9191
catch (e) {
9292
if (e.code === 'ResourceNotFound.UserNotExists') {
93-
const initSpin = ora_1.default('初始化 TCB 服务').start();
93+
const initSpin = ora_1.default('初始化云开发服务').start();
9494
await env_1.initTcb(skey);
9595
initSpin.succeed('初始化成功!');
9696
}

lib/commands/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function checkServers(servers) {
2727
});
2828
}
2929
async function getServers(name) {
30-
const config = await utils_1.resolveTcbrcConfig();
30+
const config = await utils_1.resolveCloudBaseConfig();
3131
if (!config.servers || !Array.isArray(config.servers)) {
3232
throw new Error('服务配置错误');
3333
}

lib/function/_packer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FunctionPacker {
2727
}
2828
async getFileCode() {
2929
this.validPath(this.funcPath);
30-
this.tmpPath = path_1.default.join(this.root, '.tcb_tmp');
30+
this.tmpPath = path_1.default.join(this.root, '.cloudbase_tmp');
3131
this.funcDistPath = path_1.default.join(this.tmpPath, this.name);
3232
this.clean();
3333
await make_dir_1.default(this.funcDistPath);

lib/utils/index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async function getCredential() {
9090
};
9191
}
9292
}
93-
throw new error_1.TcbError('无有效身份信息,请使用 tcb login 登录');
93+
throw new error_1.TcbError('无有效身份信息,请使用 cloudbase login 登录');
9494
}
9595
exports.getCredential = getCredential;
9696
function getSSHConfig() {
@@ -121,25 +121,31 @@ async function getSSH() {
121121
return sshConfig;
122122
}
123123
exports.getSSH = getSSH;
124-
function getTcbConfig() {
124+
function getCloudBaseConfig() {
125125
return configstore_1.configStore.all();
126126
}
127-
exports.getTcbConfig = getTcbConfig;
128-
async function resolveTcbrcConfig() {
127+
exports.getCloudBaseConfig = getCloudBaseConfig;
128+
async function resolveCloudBaseConfig() {
129129
const tcbrcPath = path_1.default.join(process.cwd(), 'tcbrc.json');
130-
if (!fs_1.default.existsSync(tcbrcPath)) {
130+
if (fs_1.default.existsSync(tcbrcPath)) {
131+
throw new error_1.TcbError('tcrbrc.josn 配置文件已废弃,请使用 cloudbaserc.json 或 cloudbaserc.js 配置文件!');
132+
}
133+
const cloudbaseJSONPath = path_1.default.join(process.cwd(), 'cloudbaserc.json');
134+
const cloudbaseJSPath = path_1.default.join(process.cwd(), 'cloudbaserc.js');
135+
const cloudbasePath = [cloudbaseJSPath, cloudbaseJSONPath].find(item => fs_1.default.existsSync(item));
136+
if (!cloudbasePath || !fs_1.default.existsSync(cloudbasePath)) {
131137
return {};
132138
}
133-
const tcbrc = await Promise.resolve().then(() => __importStar(require(tcbrcPath)));
134-
if (!tcbrc.envId) {
139+
const cloudbaseConfig = await Promise.resolve().then(() => __importStar(require(cloudbasePath)));
140+
if (!cloudbaseConfig.envId) {
135141
throw new error_1.TcbError('配置文件无效,配置文件必须包含含环境 Id');
136142
}
137-
return tcbrc;
143+
return cloudbaseConfig;
138144
}
139-
exports.resolveTcbrcConfig = resolveTcbrcConfig;
145+
exports.resolveCloudBaseConfig = resolveCloudBaseConfig;
140146
async function getEnvId(envId) {
141-
const tcbrc = await resolveTcbrcConfig();
142-
const assignEnvId = envId || tcbrc.envId;
147+
const cloudbaseConfig = await resolveCloudBaseConfig();
148+
const assignEnvId = envId || cloudbaseConfig.envId;
143149
if (!assignEnvId) {
144150
throw new error_1.TcbError('未识别到有效的环境 Id 变量,请在项目根目录进行操作或通过 envId 参数指定环境 Id');
145151
}

0 commit comments

Comments
 (0)