class FileHandlerManager

File handler manager for intelligent file updates

Examples

Basic usage with automatic handler detection

const manager = new FileHandlerManager();

// Check if a file has a handler
const handler = manager.getHandler("./deno.json");
if (handler) {
  console.log(`Found handler: ${handler.name}`);
}

// Update a file
const result = await manager.updateFile("./deno.json", "version", "1.2.3");
if (result.success) {
  console.log("File updated successfully");
}

Register custom handler for proprietary format

const customHandler: FileHandler = {
  id: "custom-config",
  name: "Custom Config",
  detector: (path) => path.endsWith(".custom"),
  patterns: { version: /version=(.+)/ },
  validate: (content) => {
    // Ensure file has required structure
    if (!content.includes("version=")) {
      return { valid: false, error: "Missing version field" };
    }
    return { valid: true };
  }
};

manager.registerHandler(customHandler);

Preview changes before applying

const manager = new FileHandlerManager();
const content = await Deno.readTextFile("./package.json");

const preview = manager.previewChanges("./package.json", content, {
  version: "2.0.0"
});

console.log("Changes to be made:");
preview.forEach(change => {
  console.log(`- Line ${change.line}: ${change.before} → ${change.after}`);
});

Constructors

new
FileHandlerManager()

Create a new FileHandlerManager instance

Properties

private
handlers: Map<string, FileHandler>

Map of registered handlers by ID

private
logger: Logger

Logger instance

Methods

getAllHandlerIds(): string[]

Get all registered handler IDs

getHandler(filePath: string): FileHandler | undefined

Get handler for a specific file path

hasHandler(filePath: string): boolean

Check if a handler exists for a file

previewChanges(
filePath: string,
key: string,
newValue: string,
): Promise<FileChangePreview>

Preview what would change in a file

Register a custom handler

updateFile(
filePath: string,
key: string,
newValue: string,
customUpdateFn?: (
content: string,
) => string
,
): Promise<FileUpdateResult>

Update a file using the appropriate handler