-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.js
55 lines (45 loc) · 1.11 KB
/
data.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
const dotenv = require("dotenv");
const yaml = require("js-yaml");
const fs = require("fs");
const path = require("path");
const cwd = process.cwd();
const paramsPath = path.join(cwd, "params.yml");
const envPath = path.join(cwd, ".env");
/**
* Load params configuration from the current working directory.
*/
exports.loadParams = function loadParams() {
const raw = fs.readFileSync(paramsPath, "utf8");
const params = yaml.safeLoad(raw);
for (const param of params) {
if (!Object.prototype.hasOwnProperty.call(params, "inputType")) {
param["inputType"] = "input";
}
}
return params;
};
/**
* Load .env from the current working directory
*/
exports.loadEnv = function loadEnv() {
let raw = "";
try {
raw = fs.readFileSync(envPath, "utf8");
} catch {
raw = "";
}
const env = dotenv.parse(raw);
return env;
};
/**
* Serialize and object from { k: v } to k=v and write it
* out to .env
*
* @param {Object} env
*/
exports.saveEnv = function saveEnv(env) {
const raw = Object.keys(env)
.map((key) => `${key}=${env[key]}`)
.join("\n");
fs.writeFileSync(envPath, raw);
};