evsecrets
Version:
Detect environment variable secrets in your codebase before you push your code to GitHub.
149 lines • 3.98 kB
JavaScript
;
/**
* Utility class for local filesystem operations.
*
* To read huge text files, consider using a line-by-line streaming approach
* in your application code rather than using this class.
* Chris Joakim, 2025
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileUtil = void 0;
const fs = require('node:fs');
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
class FileUtil {
constructor() {
}
/**
*
* @see https://nodejs.org/api/fs.html#fsaccesspath-mode-callback
*/
fileExists(filename) {
try {
const stats = fs.statSync(filename);
return stats.isFile();
}
catch (err) {
return false;
}
}
/**
* Return the current directory where this node.js process is running.
*/
cwd() {
return process.cwd();
}
/**
* Return a list of files in the given directory.
*/
listFilesInDir(dir) {
return fs.readdirSync(dir);
}
/**
* Read the given filename and return its contents as a text string.
*/
readTextFileSync(infile) {
try {
let buf = fs.readFileSync(infile, 'utf8');
return buf.toString();
}
catch (error) {
console.log(error);
return null;
}
}
/**
* Read the given filename and return its contents an array
* of strings, one per line.
*/
readTextFileAsLinesSync(infile) {
try {
let text = this.readTextFileSync(infile);
if (text == null) {
return null;
}
else {
return text.split(os_1.default.EOL);
}
}
catch (error) {
console.log(error);
return null;
}
}
/**
* Write the given string/text content to the given filename.
* Return true if successful, else false.
*/
writeTextFileSync(outfile, data) {
try {
fs.writeFileSync(outfile, data);
return true;
}
catch (error) {
console.log(error);
return false;
}
}
/**
* Read the given JSON Array file and return its parsed contents.
* The file contents must begin and end with the '[' and ']' characters.
*/
readJsonArrayFile(infile) {
try {
let str = fs.readFileSync(infile, 'utf8');
return JSON.parse(str);
}
catch (error) {
console.log(error);
return null;
}
}
/**
* Read the given JSON Array file and return its parsed contents.
* The file contents must begin and end with the '{' and '}' characters.
*/
readJsonObjectFile(infile) {
try {
let str = fs.readFileSync(infile, 'utf8');
return JSON.parse(str);
}
catch (error) {
console.log(error);
return null;
}
}
deleteFile(fn) {
try {
fs.unlinkSync(fn);
}
catch (error) {
console.log(error);
}
return;
}
deleteFilesInDir(dir) {
try {
let filesList = this.listFilesInDir(dir);
for (let i = 0; i < filesList.length; i++) {
let fn = null;
if (dir.endsWith(path_1.default.sep)) {
fn = dir + filesList[i];
}
else {
fn = dir + path_1.default.sep + filesList[i];
}
fs.unlinkSync(fn);
}
}
catch (error) {
console.log(error);
}
return;
}
}
exports.FileUtil = FileUtil;
//# sourceMappingURL=FileUtil.js.map