1 |
|
2 | const fs = require('mz/fs');
|
3 |
|
4 | const path = require('path');
|
5 |
|
6 | const yaml = require('js-yaml');
|
7 |
|
8 | const binarisConfFile = 'binaris.yml';
|
9 | const funcStr = 'functions';
|
10 | const entryStr = 'entrypoint';
|
11 | const runtimeStr = 'runtime';
|
12 | const fileStr = 'file';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const loadBinarisConf = async function loadBinarisConf(funcDirPath) {
|
19 | const fullYAMLPath = path.join(funcDirPath, binarisConfFile);
|
20 | return yaml.safeLoad(await fs.readFile(fullYAMLPath, 'utf8'));
|
21 | };
|
22 |
|
23 | const saveBinarisConf = async function saveBinarisConf(funcDirPath, binarisConf) {
|
24 | const fullYAMLPath = path.join(funcDirPath, binarisConfFile);
|
25 | const confString = yaml.dump(binarisConf);
|
26 | await fs.writeFile(fullYAMLPath, confString, 'utf8');
|
27 | };
|
28 |
|
29 | const getFunctionsSection = function getFunctionsSection(binarisConf) {
|
30 |
|
31 | if (!Object.prototype.hasOwnProperty.call(binarisConf, funcStr)) {
|
32 | throw new Error(`Your ${binarisConfFile} did not contain a require field: <${funcStr}>`);
|
33 | }
|
34 | const funcSection = binarisConf[funcStr];
|
35 | return funcSection;
|
36 | };
|
37 |
|
38 |
|
39 | const getFuncName = function getFuncName(binarisConf) {
|
40 | const funcSection = getFunctionsSection(binarisConf);
|
41 | const funcKeys = Object.keys(funcSection);
|
42 |
|
43 |
|
44 | if (funcKeys.length !== 1) {
|
45 | throw new Error(`${binarisConfFile}: ${funcStr}, only one function supported!`);
|
46 | }
|
47 | const funcName = funcKeys[0];
|
48 | return funcName;
|
49 | };
|
50 |
|
51 | const checkFuncConf = async function checkFuncConf(funcConf, funcDirPath) {
|
52 | for (const section of [fileStr, entryStr, runtimeStr]) {
|
53 | if (!funcConf[section]) {
|
54 | throw new Error(`${binarisConfFile}: missing field <${section}>`);
|
55 | }
|
56 | if (typeof funcConf[section] !== 'string') {
|
57 | throw new Error(`${binarisConfFile}: field: <${section}> should be a string`);
|
58 | }
|
59 | }
|
60 | const fullPath = path.join(funcDirPath, funcConf[fileStr]);
|
61 | await fs.readFile(fullPath, 'utf8');
|
62 | };
|
63 |
|
64 |
|
65 | const getFuncConf = function getFuncConf(binarisConf, funcName) {
|
66 | const funcSection = getFunctionsSection(binarisConf);
|
67 |
|
68 | if (!Object.prototype.hasOwnProperty.call(funcSection, funcName)) {
|
69 | throw new Error(`Cannot find function '${funcName}' in ${binarisConfFile}`);
|
70 | }
|
71 | const funcConf = funcSection[funcName];
|
72 | return funcConf;
|
73 | };
|
74 |
|
75 | const addFuncConf = function addFuncConf(binarisConf, funcName, funcConf) {
|
76 | const funcSection = getFunctionsSection(binarisConf);
|
77 | if (Object.prototype.hasOwnProperty.call(funcSection, funcName)) {
|
78 | throw new Error(`${binarisConfFile}: function already exists: ${funcName}`);
|
79 | }
|
80 | funcSection[funcName] = JSON.parse(JSON.stringify(funcConf));
|
81 | };
|
82 |
|
83 | const delFuncConf = function delFuncConf(binarisConf, funcName) {
|
84 | const funcSection = getFunctionsSection(binarisConf);
|
85 | if (!Object.prototype.hasOwnProperty.call(funcSection, funcName)) {
|
86 | throw new Error(`${binarisConfFile}: missing function: ${funcName}`);
|
87 | }
|
88 | delete funcSection[funcName];
|
89 | };
|
90 |
|
91 | module.exports = {
|
92 | loadBinarisConf,
|
93 | saveBinarisConf,
|
94 | getFuncName,
|
95 | getFuncConf,
|
96 | checkFuncConf,
|
97 | addFuncConf,
|
98 | delFuncConf,
|
99 | };
|