-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (48 loc) · 1.25 KB
/
index.js
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
49
50
51
52
53
54
55
56
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');
const fs = require('fs');
const parse = function( config ) {
var newObj = {
ignoreProcessEnv: false,
parsed: {}
}
Object.keys(config).forEach(function (key) {
process.env[key] = config[key]
})
newObj.parsed = config
return newObj;
}
module.exports.templateTags = [
{
displayName: 'dotenv',
name: 'dotenv',
description: 'Get data from .env',
args: [
{
displayName: 'Choose .env file',
help: 'Path to .env file',
type: 'string'
},
{
displayName: 'Variable Name',
description: 'Name of the variable',
type: 'string'
}
],
run(context, path, varName) {
fs.stat(path, function(err) {
if (err && err.code === 'ENOENT')
console.log('File or directory not found');
});
const config = dotenv.parse(fs.readFileSync(path));
dotenvExpand.expand( parse(config) );
if (!config || config.error) {
throw new Error('Configuration cannot be parsed.', config.error);
}
if (config[varName] === undefined) {
throw new Error('Variable not found.');
}
return config[varName];
}
}
];