gather-ts
Version:
A powerful code analysis and packaging tool designed for creating AI-friendly code representations for javascript and typescript projects.
176 lines • 5.6 kB
JavaScript
;
// src/utils/filesystem/FileSystem.ts
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSystem = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const errors_1 = require("@/errors");
const services_1 = require("@/types/services");
class FileSystem extends services_1.BaseService {
constructor(deps, options = {}) {
super();
this.deps = deps;
this.debug = options.debug || false;
}
async initialize() {
await super.initialize();
this.logDebug("FileSystem service initialized");
}
cleanup() {
this.logDebug("FileSystem service cleanup");
super.cleanup();
}
logDebug(message) {
if (this.debug) {
this.deps.logger.debug(message);
}
}
handleError(error, operation, filePath) {
const message = error instanceof Error ? error.message : String(error);
const fsError = new errors_1.FileSystemError(`Failed to ${operation} file: ${message}`, filePath, operation);
this.deps.logger.error(fsError.message);
throw fsError;
}
statSync(path) {
try {
const stats = fs_1.default.statSync(path);
return {
mtime: stats.mtime,
};
}
catch (error) {
this.handleError(error, "stat", path);
}
}
readFileSync(filePath, encoding = "utf8") {
this.checkInitialized();
this.logDebug(`Reading file synchronously: ${filePath}`);
try {
return fs_1.default.readFileSync(filePath, encoding);
}
catch (error) {
this.handleError(error, "read", filePath);
}
}
writeFileSync(filePath, data, options) {
this.logDebug(`Writing file synchronously: ${filePath}`);
try {
const dir = this.getDirName(filePath);
if (!this.exists(dir)) {
fs_1.default.mkdirSync(dir, { recursive: true });
}
fs_1.default.writeFileSync(filePath, data, options);
}
catch (error) {
this.handleError(error, "write", filePath);
}
}
exists(path) {
return fs_1.default.existsSync(path);
}
isReadable(filePath) {
try {
fs_1.default.accessSync(filePath, fs_1.default.constants.R_OK);
return true;
}
catch {
return false;
}
}
isWritable(filePath) {
try {
fs_1.default.accessSync(filePath, fs_1.default.constants.W_OK);
return true;
}
catch {
return false;
}
}
async readFile(filePath, options) {
this.logDebug(`Reading file: ${filePath}`);
try {
const buffer = await fs_1.default.promises.readFile(filePath, options);
return buffer.toString(options?.encoding || "utf8");
}
catch (error) {
this.handleError(error, "read", filePath);
}
}
async writeFile(filePath, data, options) {
this.logDebug(`Writing file: ${filePath}`);
try {
const dir = this.getDirName(filePath);
await fs_1.default.promises.mkdir(dir, { recursive: true });
await fs_1.default.promises.writeFile(filePath, data, options);
}
catch (error) {
this.handleError(error, "write", filePath);
}
}
async deleteFile(filePath) {
this.logDebug(`Deleting file: ${filePath}`);
try {
await fs_1.default.promises.unlink(filePath);
}
catch (error) {
this.handleError(error, "delete", filePath);
}
}
async createDirectory(dirPath, recursive = true) {
this.logDebug(`Creating directory: ${dirPath}`);
try {
await fs_1.default.promises.mkdir(dirPath, { recursive });
}
catch (error) {
this.handleError(error, "create", dirPath);
}
}
resolvePath(...paths) {
return path_1.default.resolve(...paths);
}
joinPath(...paths) {
return path_1.default.join(...paths);
}
getRelativePath(from, to) {
return path_1.default.relative(from, to);
}
getDirName(filePath) {
return path_1.default.dirname(filePath);
}
getBaseName(filePath) {
return path_1.default.basename(filePath);
}
getExtension(filePath) {
return path_1.default.extname(filePath);
}
isAbsolute(filePath) {
return path_1.default.isAbsolute(filePath);
}
// Alternative method names that do the same thing
dirname(filePath) {
return this.getDirName(filePath);
}
basename(filePath) {
return this.getBaseName(filePath);
}
extname(filePath) {
return this.getExtension(filePath);
}
validatePath(filePath, checkExists = true) {
if (!filePath) {
const err = new errors_1.ValidationError("Empty file path provided");
this.deps.logger.error(err.message);
throw err;
}
if (checkExists && !this.exists(filePath)) {
const err = new errors_1.ValidationError("File does not exist", { filePath });
this.deps.logger.error(err.message);
throw err;
}
}
}
exports.FileSystem = FileSystem;
//# sourceMappingURL=FileSystem.js.map