|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | +import "load-env"; |
| 4 | +import logger from "logger"; |
| 5 | +import { openaiCompatibleModelsSafeParse } from "lib/ai/create-openai-compatiable"; |
| 6 | + |
| 7 | +const ROOT = process.cwd(); |
| 8 | +const FILE_NAME = "openai-compatible.config.ts"; |
| 9 | +const CONFIG_PATH = path.join(ROOT, FILE_NAME); |
| 10 | + |
| 11 | +async function load() { |
| 12 | + try { |
| 13 | + const config = await import(CONFIG_PATH).then((m) => m.default); |
| 14 | + return openaiCompatibleModelsSafeParse(config); |
| 15 | + } catch (error) { |
| 16 | + logger.error(error); |
| 17 | + return []; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Reads a .env file, modifies a specific key's value, and writes it back. |
| 23 | + * |
| 24 | + * @param {string} envFilePath - The absolute path to the .env file. |
| 25 | + * @param {string} keyToModify - The key of the variable to add or edit (e.g., 'DATA'). |
| 26 | + * @param {string} newValue - The new value for the variable. |
| 27 | + * @returns {boolean} - True if successful, false otherwise. |
| 28 | + */ |
| 29 | +function updateEnvVariable( |
| 30 | + envFilePath: string, |
| 31 | + keyToModify: string, |
| 32 | + newValue: string, |
| 33 | +): boolean { |
| 34 | + try { |
| 35 | + let envContent = ""; |
| 36 | + if (fs.existsSync(envFilePath)) { |
| 37 | + envContent = fs.readFileSync(envFilePath, "utf8"); |
| 38 | + } |
| 39 | + |
| 40 | + const envVars: { [key: string]: string } = {}; |
| 41 | + const lines = envContent.split("\n"); |
| 42 | + |
| 43 | + lines.forEach((line) => { |
| 44 | + const trimmedLine = line.trim(); |
| 45 | + if (trimmedLine.startsWith("#") || trimmedLine === "") { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const parts = trimmedLine.split("="); |
| 50 | + if (parts.length >= 2) { |
| 51 | + const key = parts[0]; |
| 52 | + const value = parts.slice(1).join("="); |
| 53 | + envVars[key] = value; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + envVars[keyToModify] = newValue; |
| 58 | + |
| 59 | + let newEnvContent = ""; |
| 60 | + for (const key in envVars) { |
| 61 | + if (Object.prototype.hasOwnProperty.call(envVars, key)) { |
| 62 | + newEnvContent += `${key}=${envVars[key]}\n`; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + newEnvContent = newEnvContent.trim(); |
| 67 | + |
| 68 | + fs.writeFileSync(envFilePath, newEnvContent, "utf8"); |
| 69 | + console.log( |
| 70 | + `Successfully updated ${keyToModify} in ${envFilePath} to: \n\n${newValue}\n`, |
| 71 | + ); |
| 72 | + return true; |
| 73 | + } catch (error) { |
| 74 | + console.error(`Error updating .env file: ${error}`); |
| 75 | + return false; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +const envPath = path.join(ROOT, ".env"); |
| 80 | + |
| 81 | +const openaiCompatibleProviders = await load(); |
| 82 | + |
| 83 | +const success = updateEnvVariable( |
| 84 | + envPath, |
| 85 | + "OPENAI_COMPATIBLE_DATA", |
| 86 | + JSON.stringify(openaiCompatibleProviders), |
| 87 | +); |
| 88 | + |
| 89 | +if (success) { |
| 90 | + console.log("Operation completed. Check your .env file!"); |
| 91 | +} else { |
| 92 | + console.log("Operation failed."); |
| 93 | +} |
0 commit comments