File size: 2,553 Bytes
4782147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as fs from "fs";
import * as path from "path";
import { pathToFileURL } from "node:url";
import "load-env";
import logger from "logger";
import { openaiCompatibleModelsSafeParse } from "lib/ai/create-openai-compatiable";

const ROOT = process.cwd();
const FILE_NAME = "openai-compatible.config.ts";
const CONFIG_PATH = pathToFileURL(path.join(ROOT, FILE_NAME)).href;

async function load() {
  try {
    const config = await import(CONFIG_PATH).then((m) => m.default);
    return openaiCompatibleModelsSafeParse(config);
  } catch (error) {
    logger.error(error);
    return [];
  }
}

/**
 * Reads a .env file, modifies a specific key's value, and writes it back.
 *
 * @param {string} envFilePath - The absolute path to the .env file.
 * @param {string} keyToModify - The key of the variable to add or edit (e.g., 'DATA').
 * @param {string} newValue - The new value for the variable.
 * @returns {boolean} - True if successful, false otherwise.
 */
function updateEnvVariable(
  envFilePath: string,
  keyToModify: string,
  newValue: string,
): boolean {
  try {
    let envContent = "";
    if (fs.existsSync(envFilePath)) {
      envContent = fs.readFileSync(envFilePath, "utf8");
    }

    const envVars: { [key: string]: string } = {};
    const lines = envContent.split("\n");

    lines.forEach((line) => {
      const trimmedLine = line.trim();
      if (trimmedLine.startsWith("#") || trimmedLine === "") {
        return;
      }

      const parts = trimmedLine.split("=");
      if (parts.length >= 2) {
        const key = parts[0];
        const value = parts.slice(1).join("=");
        envVars[key] = value;
      }
    });

    envVars[keyToModify] = newValue;

    let newEnvContent = "";
    for (const key in envVars) {
      if (Object.prototype.hasOwnProperty.call(envVars, key)) {
        newEnvContent += `${key}=${envVars[key]}\n`;
      }
    }

    newEnvContent = newEnvContent.trim();

    fs.writeFileSync(envFilePath, newEnvContent, "utf8");
    console.log(
      `Successfully updated ${keyToModify} in ${envFilePath} to: \n\n${newValue}\n`,
    );
    return true;
  } catch (error) {
    console.error(`Error updating .env file: ${error}`);
    return false;
  }
}

const envPath = path.join(ROOT, ".env");

const openaiCompatibleProviders = await load();

const success = updateEnvVariable(
  envPath,
  "OPENAI_COMPATIBLE_DATA",
  JSON.stringify(openaiCompatibleProviders),
);

if (success) {
  console.log("Operation completed. Check your .env file!");
} else {
  console.log("Operation failed.");
}