class FileHandlerManager

Manages built-in and custom file handlers, providing automatic detection and updating of version strings in project files.

Built-in Handlers

  • JSON: deno.json, package.json, jsr.json
  • TypeScript: version.ts, constants.ts
  • Markdown: README.md and other .md files
  • YAML: .yaml and .yml configuration files
  • Language-specific: Cargo.toml, pyproject.toml, setup.py

Handler Resolution

  1. Exact filename match (e.g., "deno.json")
  2. Extension match (e.g., ".json", ".yaml")
  3. Pattern match (e.g., "version.ts", "constants.ts")

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

FileHandlerManager()

Initializes with all built-in handlers

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