import { normalize } from 'node:path' import type { Plugin } from 'vite' import { routeGenerator } from './fs-routing/generator' const ROUTES_DIRECTORY_PATH = './src/routes' let lock = false export function TuonoReactPlugin(): Plugin { const generate = async (): Promise => { if (lock) return lock = true try { await routeGenerator() } catch (err) { console.error(err) } finally { lock = false } } const handleFile = async (file: string): Promise => { const filePath = normalize(file) if (filePath.startsWith(ROUTES_DIRECTORY_PATH)) { await generate() } } return { name: 'vite-plugin-tuono-react', configResolved: async (): Promise => { await generate() }, watchChange: async ( file: string, context: { event: string }, ): Promise => { if (['create', 'update', 'delete'].includes(context.event)) { await handleFile(file) } }, } }