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

Commit 4aa4e11

Browse files
committed
support install dependency config and deploy function ignore files
1 parent eb09767 commit 4aa4e11

12 files changed

Lines changed: 90 additions & 26 deletions

File tree

lib/function/_packer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ var CodeType;
1515
CodeType[CodeType["JavaFile"] = 1] = "JavaFile";
1616
})(CodeType = exports.CodeType || (exports.CodeType = {}));
1717
class FunctionPacker {
18-
constructor(root, name) {
18+
constructor(root, name, ignore) {
1919
this.name = name;
2020
this.root = root;
21+
this.ignore = ignore;
2122
this.funcPath = path_1.default.join(root, name);
2223
}
2324
validPath(path) {
@@ -32,7 +33,7 @@ class FunctionPacker {
3233
this.clean();
3334
await make_dir_1.default(this.funcDistPath);
3435
const zipPath = path_1.default.resolve(this.funcDistPath, 'dist.zip');
35-
await utils_1.zipDir(this.funcPath, zipPath);
36+
await utils_1.zipDir(this.funcPath, zipPath, this.ignore);
3637
const base64 = fs_1.default.readFileSync(zipPath).toString('base64');
3738
await this.clean();
3839
return base64;

lib/function/create.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function createFunction(options) {
2525
throw new error_1.CloudBaseError(`${funcName} Invalid runtime value:${func.config.runtime}. Now only support: ${validRuntime.join(', ')}`);
2626
}
2727
if (!base64Code) {
28-
packer = new _packer_1.FunctionPacker(functionRootPath, funcName);
28+
packer = new _packer_1.FunctionPacker(functionRootPath, funcName, func.ignore);
2929
const type = func.config.runtime === 'Java8' ? _packer_1.CodeType.JavaFile : _packer_1.CodeType.File;
3030
base64 = await packer.build(type);
3131
if (!base64) {
@@ -58,6 +58,10 @@ async function createFunction(options) {
5858
SubnetId: (config.vpc && config.vpc.subnetId) || '',
5959
VpcId: (config.vpc && config.vpc.vpcId) || ''
6060
};
61+
params.InstallDependency =
62+
typeof config.installDependency === 'undefined'
63+
? null
64+
: config.installDependency ? 'TRUE' : 'FALSE';
6165
try {
6266
await scfService.request('CreateFunction', params);
6367
await trigger_1.createFunctionTriggers({
@@ -123,7 +127,7 @@ async function updateFunctionCode(options) {
123127
throw new error_1.CloudBaseError(`${funcName} 非法的运行环境:${func.config.runtime},当前支持环境:${validRuntime.join(', ')}`);
124128
}
125129
if (!base64Code) {
126-
packer = new _packer_1.FunctionPacker(functionRootPath, funcName);
130+
packer = new _packer_1.FunctionPacker(functionRootPath, funcName, func.ignore);
127131
const type = func.config.runtime === 'Java8' ? _packer_1.CodeType.JavaFile : _packer_1.CodeType.File;
128132
base64 = await packer.build(type);
129133
if (!base64) {
@@ -133,12 +137,16 @@ async function updateFunctionCode(options) {
133137
else {
134138
base64 = base64Code;
135139
}
140+
const installDependency = typeof func.config.installDependency === 'undefined'
141+
? null
142+
: func.config.installDependency ? 'TRUE' : 'FALSE';
136143
const params = {
137144
FunctionName: funcName,
138145
Namespace: envId,
139146
ZipFile: base64,
140147
CodeSecret: codeSecret,
141-
Handler: func.handler || 'index.main'
148+
Handler: func.handler || 'index.main',
149+
InstallDependency: installDependency
142150
};
143151
try {
144152
await scfService.request('UpdateFunctionCode', params);

lib/function/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async function updateFunctionConfig(options) {
152152
Key: key,
153153
Value: config.envVariables[key]
154154
}));
155-
const l5Enable = typeof config.l5 === 'undefined' ? null : (config.l5 ? 'TRUE' : 'FALSE');
155+
const l5Enable = typeof config.l5 === 'undefined' ? null : config.l5 ? 'TRUE' : 'FALSE';
156156
const params = {
157157
FunctionName: functionName,
158158
Namespace: envId,
@@ -165,6 +165,10 @@ async function updateFunctionConfig(options) {
165165
SubnetId: (config.vpc && config.vpc.subnetId) || '',
166166
VpcId: (config.vpc && config.vpc.vpcId) || ''
167167
};
168+
params.InstallDependency =
169+
typeof config.installDependency === 'undefined'
170+
? null
171+
: config.installDependency ? 'TRUE' : 'FALSE';
168172
await scfService.request('UpdateFunctionConfiguration', params);
169173
}
170174
exports.updateFunctionConfig = updateFunctionConfig;

lib/utils/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exports.guid6 = uuid_1.guid6;
2828
__export(require("./qcloud-request"));
2929
__export(require("./http-request"));
3030
__export(require("./output"));
31-
async function zipDir(dirPath, outputPath) {
31+
async function zipDir(dirPath, outputPath, ignore) {
3232
return new Promise((resolve, reject) => {
3333
const output = fs_1.default.createWriteStream(outputPath);
3434
const archive = archiver_1.default('zip');
@@ -42,7 +42,10 @@ async function zipDir(dirPath, outputPath) {
4242
reject(err);
4343
});
4444
archive.pipe(output);
45-
archive.directory(dirPath, '');
45+
archive.glob('**/*', {
46+
cwd: dirPath,
47+
ignore: ignore
48+
});
4649
archive.finalize();
4750
});
4851
}

src/function/_packer.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ export class FunctionPacker {
2626
funcDistPath: string
2727
// 临时目录
2828
tmpPath: string
29+
// 忽略文件模式
30+
ignore: string | string[]
2931

30-
constructor(root: string, name: string) {
32+
constructor(root: string, name: string, ignore: string | string[]) {
3133
this.name = name
3234
this.root = root
35+
this.ignore = ignore
3336
this.funcPath = path.join(root, name)
3437
}
3538

@@ -46,10 +49,12 @@ export class FunctionPacker {
4649
this.funcDistPath = path.join(this.tmpPath, this.name)
4750
// 清除原打包文件
4851
this.clean()
49-
// 生成 zip 文件
52+
// 生成存放 zip 文件的文件夹
5053
await makeDir(this.funcDistPath)
5154
const zipPath = path.resolve(this.funcDistPath, 'dist.zip')
52-
await zipDir(this.funcPath, zipPath)
55+
// 生成 zip 文件
56+
57+
await zipDir(this.funcPath, zipPath, this.ignore)
5358
// 将 zip 文件转换成 base64
5459
const base64 = fs.readFileSync(zipPath).toString('base64')
5560
// 清除打包文件

src/function/create.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function createFunction(
4545

4646
// CLI 从本地读取
4747
if (!base64Code) {
48-
packer = new FunctionPacker(functionRootPath, funcName)
48+
packer = new FunctionPacker(functionRootPath, funcName, func.ignore)
4949
const type: CodeType =
5050
func.config.runtime === 'Java8' ? CodeType.JavaFile : CodeType.File
5151
base64 = await packer.build(type)
@@ -94,8 +94,11 @@ export async function createFunction(
9494
SubnetId: (config.vpc && config.vpc.subnetId) || '',
9595
VpcId: (config.vpc && config.vpc.vpcId) || ''
9696
}
97-
// Node 安装依赖
98-
// func.config.runtime === 'Nodejs8.9' && (params.InstallDependency = true)
97+
// 是否安装依赖
98+
params.InstallDependency =
99+
typeof config.installDependency === 'undefined'
100+
? null
101+
: config.installDependency ? 'TRUE' : 'FALSE'
99102

100103
try {
101104
// 创建云函数
@@ -200,7 +203,7 @@ export async function updateFunctionCode(options: ICreateFunctionOptions) {
200203

201204
// CLI 从本地读取
202205
if (!base64Code) {
203-
packer = new FunctionPacker(functionRootPath, funcName)
206+
packer = new FunctionPacker(functionRootPath, funcName, func.ignore)
204207
const type: CodeType =
205208
func.config.runtime === 'Java8' ? CodeType.JavaFile : CodeType.File
206209
base64 = await packer.build(type)
@@ -212,12 +215,18 @@ export async function updateFunctionCode(options: ICreateFunctionOptions) {
212215
base64 = base64Code
213216
}
214217

218+
const installDependency =
219+
typeof func.config.installDependency === 'undefined'
220+
? null
221+
: func.config.installDependency ? 'TRUE' : 'FALSE'
222+
215223
const params: any = {
216224
FunctionName: funcName,
217225
Namespace: envId,
218226
ZipFile: base64,
219227
CodeSecret: codeSecret,
220-
Handler: func.handler || 'index.main'
228+
Handler: func.handler || 'index.main',
229+
InstallDependency: installDependency
221230
}
222231

223232
try {

src/function/index.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ICopyFunctionOptions {
1919
newFunctionName: string
2020
targetEnvId: string
2121
force?: boolean
22-
copyConfig?: boolean,
22+
copyConfig?: boolean
2323
codeSecret?: string
2424
}
2525

@@ -70,7 +70,9 @@ export async function deleteFunction({ functionName, envId }): Promise<void> {
7070
Namespace: envId
7171
})
7272
} catch (e) {
73-
throw new CloudBaseError(`[${functionName}] 删除操作失败:${e.message}!`)
73+
throw new CloudBaseError(
74+
`[${functionName}] 删除操作失败:${e.message}!`
75+
)
7476
}
7577
}
7678

@@ -210,7 +212,8 @@ export async function updateFunctionConfig(
210212
}))
211213

212214
// 当不存在 L5 配置时,不修改 L5 状态,否则根据 true/false 进行修改
213-
const l5Enable = typeof config.l5 === 'undefined' ? null : (config.l5 ? 'TRUE' : 'FALSE')
215+
const l5Enable =
216+
typeof config.l5 === 'undefined' ? null : config.l5 ? 'TRUE' : 'FALSE'
214217

215218
const params: any = {
216219
FunctionName: functionName,
@@ -231,6 +234,12 @@ export async function updateFunctionConfig(
231234
VpcId: (config.vpc && config.vpc.vpcId) || ''
232235
}
233236

237+
// 自动安装依赖
238+
params.InstallDependency =
239+
typeof config.installDependency === 'undefined'
240+
? null
241+
: config.installDependency ? 'TRUE' : 'FALSE'
242+
234243
await scfService.request('UpdateFunctionConfiguration', params)
235244
}
236245

@@ -249,7 +258,9 @@ export async function batchUpdateFunctionConfig(
249258
})
250259
log && successLog(`[${func.name}] 更新云函数配置成功!`)
251260
} catch (e) {
252-
throw new CloudBaseError(`${func.name} 更新配置失败:${e.message}`)
261+
throw new CloudBaseError(
262+
`${func.name} 更新配置失败:${e.message}`
263+
)
253264
}
254265
})()
255266
)
@@ -294,7 +305,9 @@ export async function batchInvokeFunctions(options: IFunctionBatchOptions) {
294305
}
295306
return result
296307
} catch (e) {
297-
throw new CloudBaseError(`${func.name} 函数调用失败:${e.message}`)
308+
throw new CloudBaseError(
309+
`${func.name} 函数调用失败:${e.message}`
310+
)
298311
}
299312
})()
300313
)
@@ -304,7 +317,14 @@ export async function batchInvokeFunctions(options: IFunctionBatchOptions) {
304317

305318
// 复制云函数
306319
export async function copyFunction(options: ICopyFunctionOptions) {
307-
const { envId, functionName, newFunctionName, targetEnvId, force, codeSecret } = options
320+
const {
321+
envId,
322+
functionName,
323+
newFunctionName,
324+
targetEnvId,
325+
force,
326+
codeSecret
327+
} = options
308328

309329
if (!envId || !functionName || !newFunctionName) {
310330
throw new CloudBaseError('参数缺失')

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export interface ICloudFunctionConfig {
8282
runtime?: string
8383
vpc?: IFunctionVPC
8484
l5?: boolean
85+
installDependency: boolean
8586
}
8687

8788
export interface ICloudFunctionTrigger {
@@ -96,6 +97,7 @@ export interface ICloudFunction {
9697
triggers: ICloudFunctionTrigger[]
9798
params?: Record<string, string>
9899
handler?: string
100+
ignore?: string | string[]
99101
}
100102

101103
export interface ICreateFunctionOptions {

src/utils/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export * from './qcloud-request'
1414
export * from './http-request'
1515
export * from './output'
1616

17-
export async function zipDir(dirPath, outputPath) {
17+
export async function zipDir(
18+
dirPath: string,
19+
outputPath: string,
20+
ignore?: string | string[]
21+
) {
1822
return new Promise((resolve, reject) => {
1923
const output = fs.createWriteStream(outputPath)
2024
const archive = archiver('zip')
@@ -31,7 +35,12 @@ export async function zipDir(dirPath, outputPath) {
3135
})
3236

3337
archive.pipe(output)
34-
archive.directory(dirPath, '')
38+
// append files from a glob pattern
39+
archive.glob('**/*', {
40+
// 目标路径
41+
cwd: dirPath,
42+
ignore: ignore
43+
})
3544
archive.finalize()
3645
})
3746
}

types/function/_packer.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export declare class FunctionPacker {
99
funcPath: string;
1010
funcDistPath: string;
1111
tmpPath: string;
12-
constructor(root: string, name: string);
12+
ignore: string | string[];
13+
constructor(root: string, name: string, ignore: string | string[]);
1314
validPath(path: string): void;
1415
getFileCode(): Promise<string>;
1516
getJavaFileCode(): string;

0 commit comments

Comments
 (0)