execute method

  1. @override
void execute(
  1. List<String> arguments
)
override

Implementation

@override
void execute(List<String> arguments) async {
  DirectoryWatcher watcher = DirectoryWatcher(Directory.current.path);

  String? vmService;
  if (arguments.isNotEmpty && arguments[0].toLowerCase() == '--vm') {
    vmService = '--enable-vm-service';
  }

  Process? process = await _serve(vmService);

  print('Vania run key commands');
  print('R Hot restart');
  print('c Clear the screen');
  print('q Quit (terminate the application)');

  /// save process info on `.dartTool` folder for upcoming serve features
  /// like down, up, etc
  _updateDartToolVaniaConfig(process);

  watcher.events.listen((event) async {
    if (path.extension(event.path) == '.dart') {
      stdout.write('\x1B[2J\x1B[0;0H');
      print("\x1B[32m File changed: ${path.basename(event.path)} \x1B[0m");
      print("Restarting the server....");

      Future.delayed(Duration(milliseconds: 100), () async {
        try {
          process?.kill();
          int? exitCode = await process?.exitCode;
          if (exitCode.toString().isNotEmpty) {
            process = await _serve(vmService);
          }
        } catch (e) {
          print("\x1B[31mAn error occurred: $e\x1B[0m");
          throw ('Error');
        }
      });
    }
  });

  ProcessSignal.sigint.watch().listen((signal) {
    print('Stopping the server...');
    Future.delayed(Duration(milliseconds: 100), () async {
      process?.kill();
      print('Server down');
      exit(0);
    });
  });

  stdin.echoMode = false;
  stdin.lineMode = false;
  stdin.listen((List<int> event) async {
    if (event.isNotEmpty && event[0] == 'R'.codeUnitAt(0)) {
      stdout.write('\x1B[2J\x1B[0;0H');
      stdout.write('Performing hot restart...\n');

      Future.delayed(Duration(milliseconds: 100), () async {
        try {
          process?.kill();
          int? exitCode = await process?.exitCode;
          if (exitCode.toString().isNotEmpty) {
            process = await _serve(vmService);
          }
        } catch (e) {
          print("\x1B[31mAn error occurred: $e\x1B[0m");
          throw ('Error');
        }
      });
    } else if (event.isNotEmpty && event[0] == 'q'.codeUnitAt(0)) {
      print('Stopping the server...');
      Future.delayed(Duration(milliseconds: 100), () async {
        process?.kill();
        print('Server down');
        exit(0);
      });
    } else if (event.isNotEmpty && event[0] == 'c'.codeUnitAt(0)) {
      stdout.write('\x1B[2J\x1B[0;0H');
    }
  });
}