@clipwhisperer/common
Version:
ClipWhisperer Common - Shared library providing core utilities, database schemas, authentication, bucket management, and common functionality across all ClipWhisperer microservices
86 lines (85 loc) • 2.32 kB
JavaScript
;
/**
* File System Utilities
* Centralized file system operations used across services
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureDirectoryExists = ensureDirectoryExists;
exports.checkDirectoryExists = checkDirectoryExists;
exports.getFileSize = getFileSize;
exports.fileExists = fileExists;
exports.deleteFileIfExists = deleteFileIfExists;
exports.createProjectPath = createProjectPath;
exports.sanitizeFilename = sanitizeFilename;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
* Ensure a directory exists, create it if it doesn't
*/
function ensureDirectoryExists(directory) {
const exists = fs_1.default.existsSync(directory);
if (!exists) {
fs_1.default.mkdirSync(directory, { recursive: true });
}
return exists;
}
/**
* Check if a directory exists and create it if it doesn't
* @param directory - The path to the directory to check
* @returns {boolean} True if the directory existed, false if it was created
*/
function checkDirectoryExists(directory) {
const exists = fs_1.default.existsSync(directory);
if (!exists) {
fs_1.default.mkdirSync(directory, { recursive: true });
}
return exists;
}
/**
* Get file size in bytes
*/
function getFileSize(filePath) {
try {
const stats = fs_1.default.statSync(filePath);
return stats.size;
}
catch (error) {
return 0;
}
}
/**
* Check if file exists
*/
function fileExists(filePath) {
return fs_1.default.existsSync(filePath);
}
/**
* Delete file if it exists
*/
function deleteFileIfExists(filePath) {
try {
if (fs_1.default.existsSync(filePath)) {
fs_1.default.unlinkSync(filePath);
return true;
}
return false;
}
catch (error) {
return false;
}
}
/**
* Create file path within project directory
*/
function createProjectPath(...segments) {
return path_1.default.join(process.cwd(), ...segments);
}
/**
* Sanitize filename for safe file system usage
*/
function sanitizeFilename(filename) {
return filename.replace(/[^a-z0-9.-]/gi, '_');
}