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

Commit 2fa83b8

Browse files
author
hengechang
committed
add function default options
2 parents e0c6d32 + f415eeb commit 2fa83b8

11 files changed

Lines changed: 77 additions & 18 deletions

File tree

lib/commands/functions/deploy.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,30 @@ async function deploy(ctx, commandOptions) {
3939
functionRootPath: path_1.default.join(process.cwd(), config.functionRoot)
4040
});
4141
}
42-
const newFunction = functions.find(item => item.name === name);
42+
let newFunction;
43+
if (functions && functions.length > 0) {
44+
newFunction = functions.find(item => item.name === name);
45+
}
4346
if (!newFunction || !newFunction.name) {
44-
throw new error_1.CloudBaseError(`函数 ${name} 配置不存在`);
47+
const { useDefaultFunctionDeployOptions } = await inquirer_1.default.prompt({
48+
type: 'confirm',
49+
name: 'useDefaultFunctionDeployOptions',
50+
message: '未找到函数发布配置,使用默认配置?',
51+
default: false
52+
});
53+
if (useDefaultFunctionDeployOptions) {
54+
newFunction = {
55+
name,
56+
config: {
57+
runtime: 'Nodejs8.9',
58+
installDependency: true
59+
},
60+
handler: 'index.main'
61+
};
62+
}
63+
else {
64+
throw new error_1.CloudBaseError(`函数 ${name} 配置不存在`);
65+
}
4566
}
4667
const createSpinner = ora_1.default('函数部署中...').start();
4768
try {

lib/commands/functions/invoke.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ async function invoke(ctx, jsonStringParams) {
4040
});
4141
}
4242
const func = functions.find(item => item.name === name);
43-
if (!func) {
44-
throw new error_1.CloudBaseError('未找到相关函数配置,请检查函数名是否正确');
45-
}
43+
const configParams = func && func.params ? func.params : undefined;
4644
const result = await function_1.invokeFunction({
4745
envId,
4846
functionName: name,
49-
params: params || func.params
47+
params: params || configParams
5048
});
5149
logger_1.successLog(`[${name}] 调用成功\n响应结果:\n`);
5250
console.log(result);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.default = {
4+
functionRoot: './functions',
5+
functions: []
6+
};

lib/utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const auth_1 = require("../auth/auth");
2121
const configstore_1 = require("./configstore");
2222
const constant_1 = require("../constant");
2323
const error_1 = require("../error");
24+
const default_cloudbase_config_1 = __importDefault(require("./default-cloudbase-config"));
2425
var cli_table_1 = require("./cli-table");
2526
exports.printCliTable = cli_table_1.printCliTable;
2627
var uuid_1 = require("./uuid");
@@ -152,7 +153,7 @@ async function resolveCloudBaseConfig(configPath = '') {
152153
if (!cloudbaseConfig.envId) {
153154
throw new error_1.CloudBaseError('配置文件无效,配置文件必须包含含环境 Id');
154155
}
155-
return cloudbaseConfig;
156+
return Object.assign({}, default_cloudbase_config_1.default, cloudbaseConfig);
156157
}
157158
exports.resolveCloudBaseConfig = resolveCloudBaseConfig;
158159
async function getEnvId(envId, configPath) {

src/commands/functions/deploy.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,30 @@ export async function deploy(ctx: FunctionContext, commandOptions) {
4444
})
4545
}
4646

47-
const newFunction = functions.find(item => item.name === name)
47+
let newFunction
48+
if (functions && functions.length > 0) {
49+
newFunction = functions.find(item => item.name === name)
50+
}
4851
if (!newFunction || !newFunction.name) {
49-
throw new CloudBaseError(`函数 ${name} 配置不存在`)
52+
const { useDefaultFunctionDeployOptions } = await inquirer.prompt({
53+
type: 'confirm',
54+
name: 'useDefaultFunctionDeployOptions',
55+
message: '未找到函数发布配置,使用默认配置?',
56+
default: false
57+
})
58+
59+
if (useDefaultFunctionDeployOptions) {
60+
newFunction = {
61+
name,
62+
config: {
63+
runtime: 'Nodejs8.9',
64+
installDependency: true
65+
},
66+
handler: 'index.main'
67+
}
68+
} else {
69+
throw new CloudBaseError(`函数 ${name} 配置不存在`)
70+
}
5071
}
5172

5273
const createSpinner = ora('函数部署中...').start()

src/commands/functions/invoke.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ export async function invoke(ctx: FunctionContext, jsonStringParams: string) {
4646

4747
const func = functions.find(item => item.name === name)
4848

49-
if (!func) {
50-
throw new CloudBaseError('未找到相关函数配置,请检查函数名是否正确')
51-
}
49+
const configParams = func && func.params ? func.params : undefined
5250

5351
const result = await invokeFunction({
5452
envId,
5553
functionName: name,
56-
params: params || func.params
54+
params: params || configParams
5755
})
5856
successLog(`[${name}] 调用成功\n响应结果:\n`)
5957
console.log(result)

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export interface ICloudFunctionTrigger {
9494
export interface ICloudFunction {
9595
name: string
9696
config: ICloudFunctionConfig
97-
triggers: ICloudFunctionTrigger[]
97+
triggers?: ICloudFunctionTrigger[]
9898
params?: Record<string, string>
9999
handler?: string
100100
ignore?: string | string[]
@@ -168,5 +168,5 @@ export interface FunctionContext {
168168
// 整体配置
169169
config: CloudBaseConfig
170170
// 配置文件中所有的函数
171-
functions: ICloudFunction[]
171+
functions?: ICloudFunction[]
172172
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
functionRoot: './functions',
3+
functions: []
4+
}

src/utils/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { configStore } from './configstore'
77
import { IConfig, Credential, AuthSecret, SSH, CloudBaseConfig } from '../types'
88
import { ConfigItems } from '../constant'
99
import { CloudBaseError } from '../error'
10+
import defaultCloudbaseConfig from './default-cloudbase-config'
1011

1112
export { printCliTable } from './cli-table'
1213
export { guid6 } from './uuid'
@@ -180,7 +181,11 @@ export async function resolveCloudBaseConfig(
180181
if (!cloudbaseConfig.envId) {
181182
throw new CloudBaseError('配置文件无效,配置文件必须包含含环境 Id')
182183
}
183-
return cloudbaseConfig
184+
185+
return {
186+
...defaultCloudbaseConfig,
187+
...cloudbaseConfig
188+
}
184189
}
185190

186191
// 从命令行和配置文件中获取 envId

types/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export interface ICloudFunctionTrigger {
7575
export interface ICloudFunction {
7676
name: string;
7777
config: ICloudFunctionConfig;
78-
triggers: ICloudFunctionTrigger[];
78+
triggers?: ICloudFunctionTrigger[];
7979
params?: Record<string, string>;
8080
handler?: string;
8181
ignore?: string | string[];
@@ -136,5 +136,5 @@ export interface FunctionContext {
136136
name: string;
137137
envId: string;
138138
config: CloudBaseConfig;
139-
functions: ICloudFunction[];
139+
functions?: ICloudFunction[];
140140
}

0 commit comments

Comments
 (0)