From 7d5b14bca135ce521a48c00e54e2e34c7bb76773 Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 6 Jun 2024 08:52:40 +0800 Subject: feat: support dotenv (#18) --- run/tool.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'run/tool.js') diff --git a/run/tool.js b/run/tool.js index ad7c151..3c8e966 100755 --- a/run/tool.js +++ b/run/tool.js @@ -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") { -- cgit v1.2.3