evsecrets
Version:
Detect environment variable secrets in your codebase before you push your code to GitHub.
78 lines • 2.96 kB
JavaScript
;
/**
* This class is used to read and process a ".env" file if it exists
* in your project. A .env file can be used in some programming
* ecosystems as an alternative location for environment variables,
* including secrets.
*
* See https://pypi.org/project/python-dotenv/
* See https://www.npmjs.com/package/dotenv
*
* Chris Joakim, 2025
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DotEnvReader = void 0;
const FileUtil_1 = require("./FileUtil");
class DotEnvReader {
constructor() {
this.exists = false;
this.envVars = {};
this.names = [];
try {
let fu = new FileUtil_1.FileUtil();
if (fu.fileExists(DotEnvReader.FILENAME)) {
this.exists = true;
this.parseFile(fu, DotEnvReader.FILENAME);
}
this.names = Object.keys(this.envVars).sort();
}
catch (err) {
console.log(err);
}
}
/**
* Parse the .env file into its names and values, and populate the
* this.envVars object with them.
*/
parseFile(fu, infile) {
try {
let lines = fu.readTextFileAsLinesSync(DotEnvReader.FILENAME);
for (let i = 0; i < lines.length; i++) {
let trimmed = lines[i].trim();
if (trimmed.length > 0) {
if ((trimmed.startsWith("#")) || (trimmed.startsWith("//"))) {
// ignore comment lines
}
else {
let eqIdx = trimmed.indexOf("=");
if (eqIdx > 0) {
let namePart = trimmed.substring(0, eqIdx).trim();
let valuePart = trimmed.substring(eqIdx + 1).trim();
// handle double-quoted values
if (valuePart.startsWith("\"")) {
valuePart = valuePart.substring(1);
if (valuePart.endsWith("\"")) {
valuePart = valuePart.substring(0, valuePart.length - 1);
}
}
// handle single-quoted values
if (valuePart.startsWith("'")) {
valuePart = valuePart.substring(1);
if (valuePart.endsWith("'")) {
valuePart = valuePart.substring(0, valuePart.length - 1);
}
}
this.envVars[namePart] = valuePart;
}
}
}
}
}
catch (err) {
console.log(err);
}
}
}
exports.DotEnvReader = DotEnvReader;
DotEnvReader.FILENAME = ".env";
//# sourceMappingURL=DotEnvReader.js.map