@iqai/adk-cli
Version:
CLI tool for creating, running, and testing ADK-TS agents
99 lines ⢠4.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvUtils = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const find_project_root_1 = require("../../../common/find-project-root");
class EnvUtils {
logger;
quiet;
constructor(logger, quiet = false) {
this.logger = logger;
this.quiet = quiet;
}
/**
* Find which .env files exist in the project
*/
findExistingEnvFiles(projectRoot) {
const envFiles = [
".env.local",
".env.development.local",
".env.production.local",
".env.development",
".env.production",
".env",
];
return envFiles.filter((file) => (0, node_fs_1.existsSync)((0, node_path_1.join)(projectRoot, file)));
}
/**
* Generate helpful error message for missing environment variables
*/
generateEnvErrorMessage(projectRoot, varName, allMissing) {
const existingEnvFiles = this.findExistingEnvFiles(projectRoot);
const missingVars = allMissing || (varName ? [varName] : []);
let message = `\nā MISSING ENVIRONMENT VARIABLE${missingVars.length > 1 ? "S" : ""}\n\n`;
if (missingVars.length > 0) {
message += `Required: ${missingVars.join(", ")}\n`;
}
message += `Project: ${projectRoot}\n\n`;
if (existingEnvFiles.length > 0) {
message += `š Found: ${existingEnvFiles.join(", ")}\n`;
message += `š” Add missing variable${missingVars.length > 1 ? "s" : ""} to one of these files\n\n`;
}
else {
message += "š” Create a .env file with:\n";
for (const v of missingVars)
message += ` ${v}=your_value_here\n`;
message += "\n";
}
message += "Tip: Use .env.local (git-ignored) for sensitive values\n";
return message;
}
/**
* Load environment variables from prioritized .env files
*/
loadEnvironmentVariables(agentFilePath) {
const normalizedAgentPath = (0, node_path_1.normalize)((0, node_path_1.resolve)(agentFilePath));
const projectRoot = (0, find_project_root_1.findProjectRoot)((0, node_path_1.dirname)(normalizedAgentPath));
const envFiles = [
".env.local",
".env.development.local",
".env.production.local",
".env.development",
".env.production",
".env",
];
let loadedAny = false;
for (const envFile of envFiles) {
const envPath = (0, node_path_1.join)(projectRoot, envFile);
if ((0, node_fs_1.existsSync)(envPath)) {
try {
const envContent = (0, node_fs_1.readFileSync)(envPath, "utf8");
const lines = envContent.split("\n");
for (const line of lines) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith("#")) {
const [key, ...valueParts] = trimmed.split("=");
if (key && valueParts.length > 0) {
const value = valueParts.join("=").replace(/^"(.*)"$/, "$1");
if (!process.env[key.trim()]) {
process.env[key.trim()] = value.trim();
}
}
}
}
loadedAny = true;
}
catch (err) {
this.logger.warn(`Warning: Could not load ${envFile} file: ${err instanceof Error ? err.message : String(err)}`);
}
}
}
if (!loadedAny && !this.quiet) {
this.logger.warn(`No .env files found in project root: ${projectRoot}\n` +
"If your agent requires environment variables, please create a .env file.");
}
}
}
exports.EnvUtils = EnvUtils;
//# sourceMappingURL=env-utils.js.map