Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async deploy () {
const data = this.deployData;
const loadResult = await loadTs(data.filename, { tmp: true });
const func = loadResult.module;
if (!func) {
throw Error(`Func load failed: ${data.filename}`);
}
if (func.config) {
data.config = deepMerge(data.config, func.config);
}
data.dependencies = deepMerge(loadResult.dependencies, func.dependencies);
// 按类型分类插件
const includedCloudFunction = [];
for (let i = 0; i < func.plugins.length; i++) {
const plugin = func.plugins[i as number];
// 删除 require 缓存
if (!this.opts.cache && require.cache[cache.file as string]) {
delete require.cache[cache.file as string];
}
// 直接 require ts 文件
// eslint-disable-next-line security/detect-non-literal-require
func = require(cache.file).default;
} catch (error) {
this.logger.error(error);
// 删除 require 缓存
if (require.cache[cache.file + '.tmp.js' as string]) {
delete require.cache[cache.file + '.tmp.js' as string];
}
// 载入 ts 文件
try {
const ts = await loadTs(cache.file, { tmp: true });
func = ts.module;
} catch (error) {
this.logger.error(error);
res.statusCode = 500;
res.write(error.message);
res.end();
return reject(error);
}
}
}
try {
if (!cache.handler) {
// 读取云函数配置并写入缓存
func.config = loadConfig(this.root, path).development;
// eslint-disable-next-line require-atomic-updates
cache.handler = func.export().handler;
env: data.env,
dependencies: data.dependencies,
tmp: data.tmp,
// cos 参数
Bucket: `scf-${config.provider.config.appId}`,
FilePath: `${data.tmp}deploy.zip`,
CosObjectName: config.config.FunctionName + '/' + data.version + '.zip'
});
this.logger.debug('完成参数处理 %o', config);
this.logger.info('开始构建代码包');
this.logger.debug('生成 index.js');
await loadTs(config.config.filename, {
output: {
file: config.config.tmp + '/index.js',
format: 'cjs',
name: 'index',
banner: `/**
* @name ${config.config.name}
* @author ${process.env.LOGNAME}
* @build ${config.config.version}
* @staging ${config.config.env}
* @dependencies ${JSON.stringify(config.config.dependencies)}
*/`,
footer: `
const main = module.exports;
main.config = ${JSON.stringify(data.config, null, 2)};
module.exports = main.export();`
}