UNPKG

agentlang

Version:

The easiest way to build the most reliable AI agents - enterprise-grade teams of AI agents that collaborate with each other and humans

169 lines 4.72 kB
/** * Node.js filesystem implementation */ import * as fs from 'node:fs/promises'; import * as path from 'node:path'; /** * Convert Node.js fs.Stats to our FileStat interface */ function convertStats(stats) { return { isFile: () => stats.isFile(), isDirectory: () => stats.isDirectory(), isSymbolicLink: () => stats.isSymbolicLink(), size: stats.size, mtime: stats.mtime, }; } /** * Node.js filesystem implementation */ export class NodeFileSystem { /** * Read a file as text * @param filePath Path to the file * @returns Promise resolving to file content as string */ async readFile(filePath) { return fs.readFile(filePath, 'utf8'); } /** * Read a file as binary * @param filePath Path to the file * @returns Promise resolving to file content as Buffer */ async readFileBuffer(filePath) { return fs.readFile(filePath); } /** * Write content to a file * @param filePath Path to the file * @param data Content to write (string or Buffer) * @returns Promise that resolves when write is complete */ async writeFile(filePath, data) { // Ensure the directory exists const dir = path.dirname(filePath); await this.ensureDir(dir); return fs.writeFile(filePath, data); } /** * Check if a file or directory exists * @param filePath Path to check * @returns Promise resolving to boolean which indicates existence */ async exists(filePath) { try { await fs.access(filePath); return true; } catch (_a) { return false; } } /** * Create a directory * @param dirPath Directory path to create * @returns Promise that resolves when directory is created */ async mkdir(dirPath) { try { await fs.mkdir(dirPath); } catch (err) { // Ignore if the directory already exists if (err.code !== 'EEXIST') { throw err; } } } /** * List files in a directory * @param dirPath Directory path * @returns Promise resolving to an array of file names */ async readdir(dirPath) { return fs.readdir(dirPath); } /** * Get stats for a file or directory * @param filePath Path to check * @returns Promise resolving to stats object */ async stat(filePath) { const stats = await fs.stat(filePath); return convertStats(stats); } /** * Remove a file * @param filePath Path to the file * @returns Promise that resolves when a file is removed */ async unlink(filePath) { return fs.unlink(filePath); } /** * Remove a directory * @param dirPath Path to the directory * @returns Promise that resolves when directory is removed */ async rmdir(dirPath) { return fs.rmdir(dirPath); } /** * Copy a file * @param src Source path * @param dest Destination path * @returns Promise that resolves when copy is complete */ async copyFile(src, dest) { // Ensure destination directory exists const destDir = path.dirname(dest); await this.ensureDir(destDir); return fs.copyFile(src, dest); } /** * Move a file * @param src Source path * @param dest Destination path * @returns Promise that resolves when move is complete */ async moveFile(src, dest) { // Ensure destination directory exists const destDir = path.dirname(dest); await this.ensureDir(destDir); return fs.rename(src, dest); } /** * Ensure a directory exists, creating it and any parent directories if needed * @param dirPath Directory path * @returns Promise that resolves when directory exists */ async ensureDir(dirPath) { try { await fs.mkdir(dirPath, { recursive: true }); } catch (err) { // Ignore if the directory already exists if (err.code !== 'EEXIST') { throw err; } } } /** * Remove a directory and all its contents recursively * @param dirPath Directory path * @returns Promise that resolves when directory is removed */ async removeDir(dirPath) { return fs.rm(dirPath, { recursive: true, force: true }); } } /** * Create a new Node.js filesystem instance * @returns NodeFileSystem instance */ export function createNodeFS() { return new NodeFileSystem(); } //# sourceMappingURL=node-fs.js.map