UNPKG

1.75 kBJavaScriptView Raw
1const fse = require('fs-extra');
2const path = require('path');
3
4const { validateName } = require('./nameUtil');
5const YMLUtil = require('./binarisYML');
6
7const templateDir = 'functionTemplates';
8const runtimeToTemplateDir = {
9 node8: 'js',
10 python2: 'py2',
11 pypy2: 'py2',
12};
13
14/**
15 * Creates a Binaris function with the given name at the
16 * provided path. If a name is not provided one will be randomly
17 * generated.
18 *
19 * @param {string} functionName - the name of the function to create
20 * @param {string} functionPath - the path to create the function at
21 * @param {string} runtime - name of runtime to run the function
22 * @returns {string} - the final name selected for the function
23 */
24const 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 // parse the templated yml and make the necessary modifications
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 // replace the generic function name with the actual name
37 YMLUtil.delFuncConf(binarisConf, templateName);
38 YMLUtil.addFuncConf(binarisConf, finalName, funcConf);
39 // now write out all the files that have been modified
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
46module.exports = create;