1 | const fse = require('fs-extra');
|
2 | const path = require('path');
|
3 |
|
4 | const { validateName } = require('./nameUtil');
|
5 | const YMLUtil = require('./binarisYML');
|
6 |
|
7 | const templateDir = 'functionTemplates';
|
8 | const runtimeToTemplateDir = {
|
9 | node8: 'js',
|
10 | python2: 'py2',
|
11 | pypy2: 'py2',
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const create = async function create(functionName, functionPath, runtime) {
|
25 | const finalName = functionName;
|
26 | validateName(finalName);
|
27 | const template = runtimeToTemplateDir[runtime];
|
28 | if (!template) {
|
29 | throw new Error(`No template for runtime: ${runtime}`);
|
30 | }
|
31 |
|
32 | const templatePath = path.join(__dirname, templateDir, template);
|
33 | const binarisConf = await YMLUtil.loadBinarisConf(templatePath);
|
34 | const templateName = YMLUtil.getFuncName(binarisConf);
|
35 | const funcConf = { ...YMLUtil.getFuncConf(binarisConf, templateName), runtime };
|
36 |
|
37 | YMLUtil.delFuncConf(binarisConf, templateName);
|
38 | YMLUtil.addFuncConf(binarisConf, finalName, funcConf);
|
39 |
|
40 | const file = funcConf.file;
|
41 | await fse.copy(path.join(templatePath, file), path.join(functionPath, file));
|
42 | await YMLUtil.saveBinarisConf(functionPath, binarisConf);
|
43 | return finalName;
|
44 | };
|
45 |
|
46 | module.exports = create;
|