@bucketeer/docs-local-mcp-server
Version:
Local MCP Server to query Bucketeer documentation
60 lines (59 loc) • 1.57 kB
JavaScript
import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
import * as path from 'node:path';
export async function fileExists(filePath) {
try {
await fsp.access(filePath, fs.constants.F_OK);
return true;
}
catch {
return false;
}
}
export async function directoryExists(dirPath) {
try {
const stats = await fsp.stat(dirPath);
return stats.isDirectory();
}
catch (error) {
if (error.code === 'ENOENT') {
return false;
}
throw error;
}
}
export async function ensureDirectoryExists(dirPath) {
try {
await fsp.mkdir(dirPath, { recursive: true });
}
catch (error) {
if (error.code !== 'EEXIST') {
throw error;
}
}
}
export async function readFile(filePath) {
return fsp.readFile(filePath, 'utf-8');
}
export async function writeFile(filePath, content) {
await ensureDirectoryExists(path.dirname(filePath));
await fsp.writeFile(filePath, content, 'utf-8');
}
export async function listFiles(directory, extension) {
try {
const files = await fsp.readdir(directory);
if (extension) {
return files.filter((file) => file.endsWith(extension));
}
return files;
}
catch (error) {
if (error.code === 'ENOENT') {
return []; // Directory doesn't exist, return empty list
}
throw error;
}
}
export async function removeDirectory(dirPath) {
await fsp.rm(dirPath, { recursive: true, force: true });
}