envilder
Version:
A CLI that securely centralizes your environment variables from AWS SSM as a single source of truth
52 lines • 2.24 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as fs from 'node:fs/promises';
import * as dotenv from 'dotenv';
export class EnvFileManager {
loadMapFile(mapPath) {
return __awaiter(this, void 0, void 0, function* () {
const content = yield fs.readFile(mapPath, 'utf-8');
try {
return JSON.parse(content);
}
catch (err) {
console.error(`Error parsing JSON from ${mapPath}`);
throw new Error(`Invalid JSON in parameter map file: ${mapPath}`);
}
});
}
loadEnvFile(envFilePath) {
return __awaiter(this, void 0, void 0, function* () {
const envVariables = {};
try {
yield fs.access(envFilePath);
}
catch (_a) {
return envVariables;
}
const existingEnvContent = yield fs.readFile(envFilePath, 'utf-8');
const parsedEnv = dotenv.parse(existingEnvContent);
Object.assign(envVariables, parsedEnv);
return envVariables;
});
}
saveEnvFile(envFilePath, envVariables) {
return __awaiter(this, void 0, void 0, function* () {
const envContent = Object.entries(envVariables)
.map(([key, value]) => `${key}=${this.escapeEnvValue(value)}`)
.join('\n');
yield fs.writeFile(envFilePath, envContent);
});
}
escapeEnvValue(value) {
return value.replace(/(\r\n|\n|\r)/g, '\\n');
}
}
//# sourceMappingURL=EnvFileManager.js.map