startWatching method

Future<void> startWatching({
  1. void onBuildComplete(
    1. GenStats stats
    )?,
  2. void onError(
    1. String error
    )?,
})

Start watching for file changes

Implementation

Future<void> startWatching({
  void Function(GenStats stats)? onBuildComplete,
  void Function(String error)? onError,
}) async {
  final sourceDir = p.join(workingDir, config.sourceDir);

  if (!Directory(sourceDir).existsSync()) {
    throw FileSystemException(
      'Source directory does not exist: ${config.sourceDir}',
      sourceDir,
    );
  }

  // Create watcher
  _watcher = DirectoryWatcher(sourceDir);

  info('Watch mode started');
  info('Watching: $sourceDir');
  info('Press Ctrl+C to stop');
  print('');

  // Do initial build
  await _doBuild(onBuildComplete, onError, isInitial: true);

  // Start listening for changes
  _subscription = _watcher!.events.listen(
    (WatchEvent event) => _onFileChange(event, onBuildComplete, onError),
    onError: (Object e) {
      error('Watch error: $e');
      onError?.call(e.toString());
    },
  );

  // Keep running until cancelled
  await _watcher!.ready;
}