@traversets/code-extractor
Version:
The TypeScript Code Extractor and Analyzer can be handy for RAG (Retrieval-Augmented Generation) systems for codebases. It provides a detailed and structured representation of the codebase that can be converted into embeddings, enabling more effective adv
141 lines • 5.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvManager = void 0;
const dotenv_1 = __importDefault(require("dotenv"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const logger_1 = require("../logger");
class EnvManager {
envFilePath;
envVariables = {};
logger;
constructor(envPath) {
this.envFilePath = path_1.default.resolve(process.cwd(), envPath);
this.logger = new logger_1.ApplicationLogger();
this.initializeEnv();
}
/**
* Initializes the environment variables by creating a new env file if it doesn't exist
* and loading the variables from the file.
* @throws {Error} If an error occurs while initializing the env file
*/
initializeEnv() {
try {
if (!fs_1.default.existsSync(this.envFilePath)) {
this.logger.warn({ method: "initializeEnv" }, `Env file not found at ${this.envFilePath}. Creating a new one.`);
fs_1.default.writeFile(this.envFilePath, "# Environment Variables\n", (err) => {
if (err) {
throw err;
}
});
}
dotenv_1.default.config({ path: this.envFilePath });
this.loadEnvVariables();
}
catch (error) {
const context = { method: "initializeEnv" };
const message = "Error occurred while initializing the env file";
this.logger.error(context, message, error);
throw error;
}
}
/**
* Loads environment variables from a file.
* Reads the file contents, splits them into key-value pairs, and stores them in the envVariables object.
* @throws {Error} If there's an error reading the file, it will be logged and re-thrown.
*/
loadEnvVariables() {
try {
const envContent = fs_1.default.readFileSync(this.envFilePath, "utf8");
const lines = envContent.split("\n");
for (const line of lines) {
const [key, value] = line.split("=");
if (key && value) {
this.envVariables[key.trim()] = value.trim();
}
}
}
catch (error) {
const context = { method: "loadEnvVariables" };
const message = "Error occurred while reading the env file";
this.logger.error(context, message, error);
throw error;
}
}
/**
* Retrieves an environment variable value by its key.
* Falls back to a default value if the variable is not set.
* @param key - The key of the environment variable to retrieve
* @param defaultValue - The default value to return if the variable is not set (optional)
* @returns The parsed value of the environment variable, or the default value if it's not set
*/
get(key, defaultValue) {
const value = process.env[key] ?? this.envVariables[key];
if (value === undefined) {
return defaultValue;
}
return this.parseValue(value, defaultValue);
}
/**
* Sets an environment variable with the given key and value.
* This method updates both the process.env object and the internal envVariables map.
* It also updates the environment file to persist the changes.
* @param key - The key of the environment variable to set
* @param value - The value of the environment variable to set
* */
set(key, value) {
const stringValue = String(value);
process.env[key] = stringValue;
this.envVariables[key] = stringValue;
this.updateEnvFile();
}
/**
* Removes an environment variable from the system and updates the env file.
* This method is used to delete sensitive configuration variables after use.
* @param key - The key of the environment variable to remove
*/
remove(key) {
delete process.env[key];
delete this.envVariables[key];
this.updateEnvFile();
}
updateEnvFile() {
try {
let envContent = "";
for (const [k, v] of Object.entries(this.envVariables)) {
envContent += `${k}=${v}\n`;
}
fs_1.default.writeFile(this.envFilePath, envContent.trim() + "\n", (err) => {
if (err) {
throw err;
}
});
}
catch (error) {
const context = { method: "updateEnvFile" };
const message = "Error occurred while updating the env file";
this.logger.error(context, message, error);
throw error;
}
}
/**
* Updates the environment file with the current environment variables.
* This method iterates over the envVariables object, constructs the env file content,
* and writes it to the file. If any error occurs during the process, it logs the error
* and re-throws it.
* */
parseValue(value, defaultValue) {
if (typeof defaultValue === "boolean") {
return (value.toLowerCase() === "true");
}
else if (typeof defaultValue === "number") {
return Number(value);
}
return value;
}
}
exports.EnvManager = EnvManager;
//# sourceMappingURL=env-manager.js.map