UNPKG

3.52 kBJavaScriptView Raw
1// this is just a convenience/wrapper for the individual commands
2const fs = require('mz/fs');
3
4const path = require('path');
5
6const yaml = require('js-yaml');
7
8const binarisConfFile = 'binaris.yml';
9const funcStr = 'functions';
10const entryStr = 'entrypoint';
11const runtimeStr = 'runtime';
12const fileStr = 'file';
13
14// this loads the binaris.yml file from the users current
15// function directory. If it does not exist in the expected
16// location the object returned will have a false 'success'
17// field and a associated error field
18const loadBinarisConf = async function loadBinarisConf(funcDirPath) {
19 const fullYAMLPath = path.join(funcDirPath, binarisConfFile);
20 return yaml.safeLoad(await fs.readFile(fullYAMLPath, 'utf8'));
21};
22
23const 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
29const getFunctionsSection = function getFunctionsSection(binarisConf) {
30 // ensure configuration has the expected field
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// Assumes a single function.
39const getFuncName = function getFuncName(binarisConf) {
40 const funcSection = getFunctionsSection(binarisConf);
41 const funcKeys = Object.keys(funcSection);
42 // There's not yet support for multiple functions per yaml
43 // The first (and only) entry is used
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
51const 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// Assumes a single function.
65const getFuncConf = function getFuncConf(binarisConf, funcName) {
66 const funcSection = getFunctionsSection(binarisConf);
67 // ensure configuration has the correct function
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
75const 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
83const 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
91module.exports = {
92 loadBinarisConf,
93 saveBinarisConf,
94 getFuncName,
95 getFuncConf,
96 checkFuncConf,
97 addFuncConf,
98 delFuncConf,
99};