-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (38 loc) · 1.21 KB
/
index.js
File metadata and controls
48 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const argparse = require('argparse').ArgumentParser;
const getFlagMap = require('./libs/getFlagMap');
const ERROR = {
notAnInstance: Error('First argument must be an instance of argparse'),
};
const INFO = {
noConfig: 'No config passed. Skipping.',
};
function hasEqualInterface(parserInstance) {
const keys = (o) => Object.keys(o);
const instanceProps = keys(parserInstance);
const argParseProps = keys(argparse);
return argParseProps.every((k) => instanceProps.includes(k));
}
/**
* Adds env keys as flags in the passed argparse instance
* @param {object} parserInstance - Instance of argparse
* @param {object} dotenvconfig - parsed object of dotenv.config()
* @returns {object} - Instance of argparse with added flags from dotenv keys
*
*/
function main(parserInstance, dotenvConfig = {}) {
if (!hasEqualInterface(parserInstance)) {
throw ERROR.notAnInstance;
}
if (Object.keys(dotenvConfig).length < 1) {
console.info(INFO.noconfig);
return parserInstance;
}
const flagMaps = getFlagMap(dotenvConfig);
for (let [_, v] of Object.entries(flagMaps)) {
parserInstance.addArgument(v.flag, {
defaultValue: v.value,
});
}
return parserInstance;
}
module.exports = main;