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
- Exact filename match (e.g., "deno.json")
- Extension match (e.g., ".json", ".yaml")
- Pattern match (e.g., "version.ts", "constants.ts")
Basic usage with automatic handler detection
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
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
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}`); });
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
registerHandler(handler: FileHandler): void
Register a custom handler
updateFile(filePath: string,key: string,newValue: string,customUpdateFn?: (content: string,data: TemplateData) => string): Promise<FileUpdateResult>
Update a file using the appropriate handler