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

Commit 28a2b2b

Browse files
committed
support local function run
1 parent 86a4902 commit 28a2b2b

15 files changed

Lines changed: 229 additions & 137 deletions

File tree

bin/cloudbase.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ ${chalk.gray('–')} 初始化云开发项目
116116
117117
${chalk.gray('–')} 部署云函数
118118
119-
${chalk.cyan('$ cloudbase functions:deploy')}`
119+
${chalk.cyan('$ cloudbase functions:deploy')}
120+
121+
${chalk.gray('–')} 查看命令使用介绍
122+
123+
${chalk.cyan('$ cloudbase functions:log -h')}`
120124
console.log(tips)
121125
})
122126

lib/commands/functions/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const trigger_delete_1 = require("./trigger-delete");
2727
const invoke_1 = require("./invoke");
2828
const copy_1 = require("./copy");
2929
const code_download_1 = require("./code-download");
30-
const debug_1 = require("./debug");
30+
const run_1 = require("./run");
3131
function getFunctionContext(name, envId, configPath) {
3232
return __awaiter(this, void 0, void 0, function* () {
3333
const cloudBaseConfig = yield utils_1.resolveCloudBaseConfig(configPath);
@@ -122,16 +122,6 @@ const commands = [
122122
yield detail_1.detail(ctx, options);
123123
})
124124
},
125-
{
126-
cmd: 'functions:invoke [functionName] [params] [envId]',
127-
options: [],
128-
desc: '触发云函数',
129-
handler: (name, jsonStringParams, envId, options) => __awaiter(void 0, void 0, void 0, function* () {
130-
const { configFile } = options.parent;
131-
const ctx = yield getFunctionContext(name, envId, configFile);
132-
yield invoke_1.invoke(ctx, jsonStringParams);
133-
})
134-
},
135125
{
136126
cmd: 'functions:code:update <functionName> [envId]',
137127
options: [
@@ -235,7 +225,17 @@ const commands = [
235225
})
236226
},
237227
{
238-
cmd: 'functions:invoke:local',
228+
cmd: 'functions:invoke [functionName] [params] [envId]',
229+
options: [],
230+
desc: '触发云端部署的云函数',
231+
handler: (name, jsonStringParams, envId, options) => __awaiter(void 0, void 0, void 0, function* () {
232+
const { configFile } = options.parent;
233+
const ctx = yield getFunctionContext(name, envId, configFile);
234+
yield invoke_1.invoke(ctx, jsonStringParams);
235+
})
236+
},
237+
{
238+
cmd: 'functions:run',
239239
options: [
240240
{
241241
flags: '--path <path>',
@@ -258,17 +258,17 @@ const commands = [
258258
desc: '启动调试模式'
259259
}
260260
],
261-
desc: '本地运行云函数',
261+
desc: '本地运行云函数(当前仅支持 Node)',
262262
handler: (options) => __awaiter(void 0, void 0, void 0, function* () {
263263
const { path } = options;
264264
if (path) {
265-
yield debug_1.debugFunctionByPath(path, options);
265+
yield run_1.debugFunctionByPath(path, options);
266266
}
267267
else {
268268
const { name } = options;
269269
const { configFile } = options.parent;
270270
const ctx = yield getFunctionContext(name, '', configFile);
271-
yield debug_1.debugByConfig(ctx, options);
271+
yield run_1.debugByConfig(ctx, options);
272272
}
273273
})
274274
}
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ function checkJSON(data) {
2525
throw new error_1.CloudBaseError('非法的 JSON 字符串');
2626
}
2727
}
28+
function errorLog(msg, debug) {
29+
throw new error_1.CloudBaseError(msg, {
30+
meta: { debug }
31+
});
32+
}
2833
function getDebugArgs(port = 9229) {
2934
return [
3035
`--inspect-brk=0.0.0.0:${port}`,
@@ -61,30 +66,30 @@ function debugFunctionByPath(functionPath, options) {
6166
const { params, debug, port } = options;
6267
params && checkJSON(params);
6368
const filePath = path_1.default.resolve(functionPath);
64-
utils_1.checkPathExist(filePath, true);
69+
utils_1.checkPathExist(filePath);
6570
let debugDirname;
6671
if (utils_1.isDirectory(filePath)) {
6772
const exists = utils_1.checkPathExist(path_1.default.join(filePath, 'index.js'));
6873
if (!exists) {
69-
throw new error_1.CloudBaseError('index.js 文件不存在!');
74+
errorLog('index.js 文件不存在!', debug);
7075
}
7176
debugDirname = filePath;
7277
}
7378
else {
7479
const { base, dir } = path_1.default.parse(filePath);
7580
if (base !== 'index.js') {
76-
throw new error_1.CloudBaseError('index.js 文件不存在!');
81+
errorLog('index.js 文件不存在!', debug);
7782
}
7883
debugDirname = dir;
7984
}
8085
try {
8186
const fileExports = require(path_1.default.join(debugDirname, 'index.js'));
8287
if (!fileExports.main) {
83-
throw new error_1.CloudBaseError('main 方法不存在!');
88+
errorLog('main 方法不存在!', debug);
8489
}
8590
}
8691
catch (e) {
87-
throw new error_1.CloudBaseError(`导入云函数异常:${e.message}`);
92+
errorLog(`导入云函数异常:${e.message}`, debug);
8893
}
8994
const debugArgs = getDebugArgs(port);
9095
const args = debug ? [...debugArgs, bootstrapFilePath] : [bootstrapFilePath];
@@ -102,7 +107,7 @@ function debugByConfig(ctx, options) {
102107
params && checkJSON(params);
103108
let functionPath = path_1.default.resolve(config.functionRoot, name);
104109
const filePath = path_1.default.resolve(functionPath);
105-
utils_1.checkPathExist(filePath, true);
110+
utils_1.checkPathExist(filePath, !debug);
106111
let debugDirname;
107112
const funcConfig = utils_1.findAndFlattenFunConfig(config, name);
108113
const handlers = (funcConfig.handler || 'index.js').split('.');
@@ -112,25 +117,25 @@ function debugByConfig(ctx, options) {
112117
if (utils_1.isDirectory(filePath)) {
113118
const exists = utils_1.checkPathExist(path_1.default.join(filePath, indexFile));
114119
if (!exists) {
115-
throw new error_1.CloudBaseError(`${indexFile} 文件不存在!`);
120+
errorLog(`${indexFile} 文件不存在!`, debug);
116121
}
117122
debugDirname = filePath;
118123
}
119124
else {
120125
const { base, dir } = path_1.default.parse(filePath);
121126
if (base !== indexFile) {
122-
throw new error_1.CloudBaseError(`${indexFile} 文件不存在!`);
127+
errorLog(`${indexFile} 文件不存在!`, debug);
123128
}
124129
debugDirname = dir;
125130
}
126131
try {
127132
const fileExports = require(path_1.default.join(debugDirname, indexFile));
128133
if (!fileExports[mainFunction]) {
129-
throw new error_1.CloudBaseError(`handler 中的 ${mainFunction} 方法不存在,请检查你的配置!`);
134+
errorLog(`handler 中的 ${mainFunction} 方法不存在,请检查你的配置!`, debug);
130135
}
131136
}
132137
catch (e) {
133-
throw new error_1.CloudBaseError(`导入云函数异常:${e.message}`);
138+
errorLog(`导入云函数异常:${e.message}`, debug);
134139
}
135140
const debugArgs = getDebugArgs(port);
136141
const args = debug ? [...debugArgs, bootstrapFilePath] : [bootstrapFilePath];

lib/commands/hosting.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ const HostingStatusMap = {
2727
offline: '下线'
2828
};
2929
commander_1.default
30-
.command('hosting:enable [envId]')
30+
.command('hosting:enable')
31+
.option('-e, --envId', '环境 Id')
3132
.description('开启静态网站服务')
32-
.action((envId, options) => __awaiter(void 0, void 0, void 0, function* () {
33-
const { configFile } = options.parent;
33+
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
34+
const { parent: { configFile }, envId } = options;
3435
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
3536
const res = yield hosting_1.enableHosting({ envId: assignEnvId });
3637
if (res.code === 0) {
@@ -41,10 +42,11 @@ commander_1.default
4142
}
4243
}));
4344
commander_1.default
44-
.command('hosting:detail [envId]')
45+
.command('hosting:detail')
46+
.option('-e, --envId', '环境 Id')
4547
.description('查看静态网站服务信息')
46-
.action((envId, options) => __awaiter(void 0, void 0, void 0, function* () {
47-
const { configFile } = options.parent;
48+
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
49+
const { parent: { configFile }, envId } = options;
4850
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
4951
const res = yield hosting_1.getHostingInfo({ envId: assignEnvId });
5052
const website = res.data && res.data[0];
@@ -55,10 +57,11 @@ commander_1.default
5557
console.log(`静态网站域名:${chalk_1.default.bold.underline(url)}\n静态网站状态:${HostingStatusMap[website.Status]}`);
5658
}));
5759
commander_1.default
58-
.command('hosting:destroy [envId]')
60+
.command('hosting:destroy')
61+
.option('-e, --envId', '环境 Id')
5962
.description('关闭静态网站服务')
60-
.action((envId, options) => __awaiter(void 0, void 0, void 0, function* () {
61-
const { configFile } = options.parent;
63+
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
64+
const { parent: { configFile }, envId } = options;
6265
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
6366
const { confirm } = yield inquirer_1.default.prompt({
6467
type: 'confirm',
@@ -80,10 +83,11 @@ commander_1.default
8083
}
8184
}));
8285
commander_1.default
83-
.command('hosting:deploy [filePath] [cloudPath] [envId]')
86+
.command('hosting:deploy [filePath] [cloudPath]')
87+
.option('-e, --envId', '环境 Id')
8488
.description('部署静态网站文件')
85-
.action((filePath, cloudPath = '', envId, options) => __awaiter(void 0, void 0, void 0, function* () {
86-
const { configFile } = options.parent;
89+
.action((filePath, cloudPath = '', options) => __awaiter(void 0, void 0, void 0, function* () {
90+
const { parent: { configFile }, envId } = options;
8791
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
8892
const isDir = utils_1.isDirectory(filePath);
8993
const loading = utils_1.loadingFactory();
@@ -102,11 +106,12 @@ commander_1.default
102106
}
103107
}));
104108
commander_1.default
105-
.command('hosting:delete [cloudPath] [envId]')
109+
.command('hosting:delete [cloudPath]')
110+
.option('-e, --envId', '环境 Id')
106111
.option('-d, --dir', '删除文件夹')
107112
.description('删除静态网站文件/文件夹')
108-
.action((cloudPath = '', envId, options) => __awaiter(void 0, void 0, void 0, function* () {
109-
const { configFile } = options.parent;
113+
.action((cloudPath = '', options) => __awaiter(void 0, void 0, void 0, function* () {
114+
const { parent: { configFile }, envId } = options;
110115
const { dir } = options;
111116
const fileText = dir ? '文件夹' : '文件';
112117
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
@@ -125,10 +130,11 @@ commander_1.default
125130
}
126131
}));
127132
commander_1.default
128-
.command('hosting:list [envId]')
133+
.command('hosting:list')
134+
.option('-e, --envId', '环境 Id')
129135
.description('展示文件列表')
130-
.action((envId, options) => __awaiter(void 0, void 0, void 0, function* () {
131-
const { configFile } = options.parent;
136+
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
137+
const { parent: { configFile }, envId } = options;
132138
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
133139
const loading = utils_1.loadingFactory();
134140
loading.start('获取文件列表中...');

lib/commands/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const utils_1 = require("../utils");
2626
const listUrl = 'https://service-lqbcazn1-1252710547.ap-shanghai.apigateway.myqcloud.com/release/';
2727
function extractTemplate(projectPath, templatePath) {
2828
return __awaiter(this, void 0, void 0, function* () {
29-
const url = `https://6261-base-830cab-1252710547.tcb.qcloud.la/cloudbase-examples/${templatePath}.tar.gz`;
29+
const url = `https://6261-base-830cab-1252710547.tcb.qcloud.la/cloudbase-templates/${templatePath}.tar.gz`;
3030
return utils_1.fetchStream(url).then((res) => __awaiter(this, void 0, void 0, function* () {
3131
if (res.status !== 200) {
3232
throw new error_1.CloudBaseError('未找到文件');

0 commit comments

Comments
 (0)