-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
59 lines (50 loc) · 1.37 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
57
58
59
const dotenv = require('dotenv');
const untildify = require('untildify');
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: 'Pull data from .env',
args: [
{
displayName: 'Choose .Env File',
help: 'Path to .env file',
type: 'file'
},
{
displayName: 'Variable Name',
description: 'Name of the variable',
type: 'string'
}
],
run(context, path, varName) {
const expandedPath = untildify(path);
fs.stat(expandedPath, function(err) {
if (err && err.code === 'ENOENT')
console.log('File or directory not found');
});
const config = dotenv.parse(fs.readFileSync(expandedPath));
dotenvExpand( parse(config) );
if (!config || config.error) {
throw new Error('We could not parse the config..what have you done 😃', config.error);
}
if (config[varName] === undefined) {
throw new Error('Variable not found!');
}
return config[varName];
}
}
];