-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcreate_env.js
70 lines (60 loc) · 1.78 KB
/
create_env.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
60
61
62
63
64
65
66
67
68
69
70
const fs = require("fs");
const path = require("path");
const readline = require("readline");
require("dotenv").config();
async function main() {
const envFilePath = path.join(process.cwd(), ".env");
const envExampleFilePath = path.join(process.cwd(), ".env.example");
const envExampleContent = fs.readFileSync(envExampleFilePath, "utf-8");
const envExampleLines = envExampleContent.split("\n");
const env = {};
for (const line of envExampleLines) {
const trimmedLine = line.trim();
if (trimmedLine.includes("=")) {
const [keyName, defaultValue = ""] = trimmedLine.split("=");
let currentValue = process.env[keyName];
if (
currentValue === undefined &&
defaultValue.includes("<") &&
defaultValue.includes(">")
) {
const new_value = await prompt(
`Enter value for ${keyName} (default: ${defaultValue}): `
);
currentValue = new_value === "" ? "" : new_value;
} else {
currentValue = currentValue !== undefined ? currentValue : defaultValue;
}
env[keyName] = currentValue;
}
}
saveEnvFile(env, envFilePath);
const browserChatEnvFilePath = path.join(
process.cwd(),
"apps",
"interactor",
".env"
);
saveEnvFile(env, browserChatEnvFilePath);
}
function saveEnvFile(env, filePath) {
const content = Object.entries(env)
.map(([key, value]) => `${key}=${value}`)
.join("\n");
fs.writeFileSync(filePath, content);
}
function prompt(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) =>
rl.question(query, (ans) => {
rl.close();
resolve(ans);
})
);
}
main()
.then(() => console.log("Environment setup complete."))
.catch(console.error);