@coji/journal-mcp
Version:
MCP server for journal entries with web viewer
71 lines • 1.63 kB
JavaScript
import { promises as fs } from 'node:fs';
import { dirname } from 'node:path';
/**
* Ensure directory exists, creating it if necessary
*/
export async function ensureDir(dirPath) {
try {
await fs.access(dirPath);
}
catch {
await fs.mkdir(dirPath, { recursive: true });
}
}
/**
* Check if file exists
*/
export async function fileExists(filePath) {
try {
await fs.access(filePath);
return true;
}
catch {
return false;
}
}
/**
* Read file with error handling
*/
export async function readFileIfExists(filePath) {
try {
return await fs.readFile(filePath, 'utf-8');
}
catch {
return null;
}
}
/**
* Write file with directory creation
*/
export async function writeFileWithDir(filePath, content) {
await ensureDir(dirname(filePath));
await fs.writeFile(filePath, content, 'utf-8');
}
/**
* Create backup of existing file
*/
export async function backupFile(filePath) {
if (!(await fileExists(filePath))) {
return null;
}
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupPath = `${filePath}.backup.${timestamp}`;
const content = await fs.readFile(filePath, 'utf-8');
await fs.writeFile(backupPath, content, 'utf-8');
return backupPath;
}
/**
* Delete a file if it exists
*/
export async function deleteFile(filePath) {
try {
await fs.unlink(filePath);
}
catch (error) {
// Ignore file not found errors
if (error.code !== 'ENOENT') {
throw error;
}
}
}
//# sourceMappingURL=files.js.map