UNPKG

@gati-framework/cli

Version:

CLI tool for Gati framework - create, develop, build and deploy cloud-native applications

85 lines 2.84 kB
/** * @module cli/utils/watcher * @description File watching utility for hot reload in development */ import chokidar, {} from 'chokidar'; import { resolve } from 'path'; import chalk from 'chalk'; /** * Create a file watcher for development hot reload */ export function createWatcher(cwd, options, onChange) { const { paths, ignored = ['**/node_modules/**', '**/dist/**', '**/.git/**', '**/coverage/**'], verbose = false, debounce = 300, } = options; if (verbose) { // eslint-disable-next-line no-console console.log(chalk.blue(`👁 Watching for file changes in:`)); paths.forEach((p) => { // eslint-disable-next-line no-console console.log(chalk.gray(` - ${resolve(cwd, p)}`)); }); } // Create watcher - use cwd and relative paths const watcher = chokidar.watch(paths, { cwd, // Set base directory ignored, persistent: true, ignoreInitial: true, awaitWriteFinish: { stabilityThreshold: debounce, pollInterval: 100, }, }); // Debounce helper let debounceTimer = null; const debouncedOnChange = (path, event) => { if (debounceTimer) { clearTimeout(debounceTimer); } debounceTimer = setTimeout(() => { void (async () => { try { await onChange(path, event); } catch (error) { // eslint-disable-next-line no-console console.error(chalk.red(`✖ Error handling file change: ${error instanceof Error ? error.message : 'Unknown error'}`)); } })(); }, debounce); }; // Register event handlers watcher .on('add', (path) => { if (verbose) { // eslint-disable-next-line no-console console.log(chalk.green(`➕ File added: ${path}`)); } debouncedOnChange(path, 'add'); }) .on('change', (path) => { if (verbose) { // eslint-disable-next-line no-console console.log(chalk.yellow(`📝 File changed: ${path}`)); } debouncedOnChange(path, 'change'); }) .on('unlink', (path) => { if (verbose) { // eslint-disable-next-line no-console console.log(chalk.red(`🗑 File removed: ${path}`)); } debouncedOnChange(path, 'unlink'); }) .on('error', (error) => { // eslint-disable-next-line no-console console.error(chalk.red(`✖ Watcher error: ${error instanceof Error ? error.message : 'Unknown error'}`)); }); return watcher; } /** * Stop a file watcher */ export async function stopWatcher(watcher) { await watcher.close(); } //# sourceMappingURL=watcher.js.map