envilder
Version:
A CLI that securely centralizes your environment variables from AWS SSM as a single source of truth
100 lines • 4.89 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
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';
import { inject, injectable } from 'inversify';
import { DependencyMissingError, EnvironmentFileError, } from '../../domain/errors/DomainErrors.js';
import { TYPES } from '../../types.js';
let FileVariableStore = class FileVariableStore {
constructor(logger) {
if (!logger) {
throw new DependencyMissingError('Logger must be specified');
}
this.logger = logger;
}
getMapping(source) {
return __awaiter(this, void 0, void 0, function* () {
try {
const content = yield fs.readFile(source, 'utf-8');
try {
return JSON.parse(content);
}
catch (_err) {
this.logger.error(`Error parsing JSON from ${source}`);
throw new EnvironmentFileError(`Invalid JSON in parameter map file: ${source}`);
}
}
catch (error) {
if (error instanceof EnvironmentFileError) {
throw error;
}
throw new EnvironmentFileError(`Failed to read map file: ${source}`);
}
});
}
getEnvironment(source) {
return __awaiter(this, void 0, void 0, function* () {
const envVariables = {};
try {
yield fs.access(source);
}
catch (_a) {
return envVariables;
}
const existingEnvContent = yield fs.readFile(source, 'utf-8');
const parsedEnv = dotenv.parse(existingEnvContent) || {};
Object.assign(envVariables, parsedEnv);
return envVariables;
});
}
saveEnvironment(destination, envVariables) {
return __awaiter(this, void 0, void 0, function* () {
const envContent = Object.entries(envVariables)
.map(([key, value]) => `${key}=${this.escapeEnvValue(value)}`)
.join('\n');
try {
yield fs.writeFile(destination, envContent);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
this.logger.error(`Failed to write environment file: ${errorMessage}`);
throw new EnvironmentFileError(`Failed to write environment file: ${errorMessage}`);
}
});
}
escapeEnvValue(value) {
// codeql[js/incomplete-sanitization]
// CodeQL flags this as incomplete sanitization because we don't escape backslashes
// before newlines. However, this is intentional: the dotenv library does NOT
// interpret escape sequences (it treats \n literally as backslash+n, not as a newline).
// Therefore, escaping backslashes would actually break the functionality by
// doubling them when read back by dotenv. This is not a security issue in this context.
return value.replace(/(\r\n|\n|\r)/g, '\\n');
}
};
FileVariableStore = __decorate([
injectable(),
__param(0, inject(TYPES.ILogger)),
__metadata("design:paramtypes", [Object])
], FileVariableStore);
export { FileVariableStore };
//# sourceMappingURL=FileVariableStore.js.map