forked from bongosway/insomnia-plugin-dotenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 940 Bytes
/
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
const untildify = require('untildify');
const fs = require('fs');
module.exports.templateTags = [
{
displayName: 'JSON config',
name: 'jsonconfig',
description: 'Pull data from JSON file',
args: [
{
displayName: 'Choose JSON File',
help: 'Path to JSON 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 rawData = fs.readFileSync(expandedPath);
const config = JSON.parse(rawData);
if (config[varName] === undefined) {
throw new Error('Variable not found!');
}
return config[varName];
}
}
];