UNPKG

1.31 kBJavaScriptView Raw
1'use strict';
2
3
4const fs = require('fs');
5const util = require('util');
6const path = require('path');
7
8const yaml = require('js-yaml');
9const debug = require('debug')('fun:conf');
10const getProfile = require('./profile').getProfile;
11
12const readFile = util.promisify(fs.readFile);
13const exists = util.promisify(fs.exists);
14
15async function getConf(rootDir) {
16 const profile = await getProfile();
17
18 var confPath = path.join(rootDir, 'faas.yml');
19 var isexists = await exists(confPath);
20
21 if (!isexists) {
22 // try faas.yaml
23 confPath = path.join(rootDir, 'faas.yaml');
24 isexists = await exists(confPath);
25 }
26
27 if (!isexists) {
28 throw new Error('Current folder not a Faas project\nThe folder must contains faas.yml or faas.yaml');
29 }
30
31 const confContent = await readFile(confPath, 'utf8');
32 const conf = yaml.safeLoad(confContent);
33
34 if (!conf.accountid) {
35 debug('try to get accountId from profile');
36 conf.accountid = profile.accountId;
37 }
38
39 if (!conf.accessKeyId) {
40 debug('try to get accessKeyId from profile');
41 conf.accessKeyId = profile.accessKeyId;
42 }
43
44 if (!conf.accessKeySecret) {
45 debug('try to get accessKeySecret from profile');
46 conf.accessKeySecret = profile.accessKeySecret;
47 }
48
49 debug('exitst config: %j', conf);
50 return conf;
51}
52
53module.exports = getConf;