feat: support dotenv (#18)

This commit is contained in:
sigoden
2024-06-06 08:52:40 +08:00
committed by GitHub
parent bfb7d75fe4
commit 7d5b14bca1
4 changed files with 62 additions and 5 deletions
+20 -1
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env node
const path = require("path");
const fs = require('fs');
function parseArgv() {
let func_file = process.argv[1];
@@ -22,7 +23,7 @@ function parseArgv() {
}
function loadFunc(func_file) {
const func_path = path.resolve(__dirname, `../tools/${func_file}`)
const func_path = path.resolve(process.env["LLM_FUNCTIONS_DIR"], `tools/${func_file}`)
try {
return require(func_path);
} catch {
@@ -31,6 +32,24 @@ function loadFunc(func_file) {
}
}
function loadEnv(filePath) {
try {
const data = fs.readFileSync(filePath, 'utf-8');
const lines = data.split('\n');
lines.forEach(line => {
if (line.trim().startsWith('#') || line.trim() === '') return;
const [key, ...value] = line.split('=');
process.env[key.trim()] = value.join('=').trim();
});
} catch {}
}
process.env["LLM_FUNCTIONS_DIR"] = path.resolve(__dirname, "..");
loadEnv(path.resolve(process.env["LLM_FUNCTIONS_DIR"], ".env"));
const [func_file, func_data] = parseArgv();
if (process.env["LLM_FUNCTION_ACTION"] == "declarate") {