diff --git a/bin/start.dart b/bin/start.dart index 5d1a49a..f8a0ddb 100644 --- a/bin/start.dart +++ b/bin/start.dart @@ -16,14 +16,44 @@ void main(List args) async { Process? serverProcess; // Function to handle cleanup and exit - void cleanup() { + void cleanup({bool exitNow = true}) { // ignore: unnecessary_null_comparison if (serverProcess != null) { Console.resetAll(); - print('Terminating server process...'); - serverProcess.kill(); + exitNow ? print('Terminating server process...') : null; + serverProcess!.kill(); } - exit(0); + exitNow ? exit(0) : print('Restarting the server process...'); + } + + // Start the server as a child process + if (results.flag('dev')) { + watchDirectory('lib', handleFileChange: () async { + cleanup(exitNow: false); + serverProcess = await Process.start('dart', ['run', 'lib/index.dart']); + // print('Server started with PID: ${serverProcess.pid}'); + Console.setBackgroundColor(7, bright: true); + Console.setTextColor(3, bright: true); + Console.write('\n ^-^ H4 CLI -- \n'.toUpperCase()); + Console.resetBackgroundColor(); + + // Handle server process stdout + serverProcess!.stdout.transform(utf8.decoder).listen((data) { + Console.write(data); + }).onDone(() { + Console.resetTextColor(); + Console.setBold(false); + }); + + // Handle server process stderr + serverProcess!.stderr.transform(utf8.decoder).listen((data) { + print(data); + }); + + // Wait for the server process to exit + final exitCode = await serverProcess!.exitCode; + print('Server process exited with code: $exitCode'); + }); } // Set up signal handling for graceful shutdown @@ -36,7 +66,9 @@ void main(List args) async { ProcessSignal.sigterm.watch().listen((_) => cleanup()); } - runH4App({required Process? serverProcess, required File file}) async { + try { + var file = File("lib/index.dart"); + // await runH4App(serverProcess: serverProcess, file: file); if (!file.existsSync()) { print('ERROR: Could not find your server at lib/index.dart'); print( @@ -51,7 +83,7 @@ void main(List args) async { Console.resetBackgroundColor(); // Handle server process stdout - serverProcess.stdout.transform(utf8.decoder).listen((data) { + serverProcess!.stdout.transform(utf8.decoder).listen((data) { Console.write('\n$data'); }).onDone(() { Console.resetTextColor(); @@ -59,29 +91,12 @@ void main(List args) async { }); // Handle server process stderr - serverProcess.stderr.transform(utf8.decoder).listen((data) { + serverProcess!.stderr.transform(utf8.decoder).listen((data) { print(data); }); // Wait for the server process to exit - final exitCode = await serverProcess.exitCode; - print('Server process exited with code: $exitCode'); - } - } - - try { - var file = File("lib/index.dart"); - await runH4App(serverProcess: serverProcess, file: file); - - // Start the server as a child process - if (results.flag('dev')) { - watchDirectory('lib', handleFileChange: () async { - if (serverProcess is Process) { - serverProcess.kill(); - } - Console.write('\nChange detected - restarting server \n'); - await runH4App(serverProcess: serverProcess, file: file); - }); + await serverProcess!.exitCode; } } catch (e) { print('Error starting server: $e'); @@ -94,19 +109,12 @@ void watchDirectory(String dirPath, {required handleFileChange}) { print('Watching directory: $dirPath'); - watcher.events.listen((event) { - final relativePath = path.relative(event.path, from: dirPath); + watcher.events.listen((event) async { + // final relativePath = path.relative(event.path, from: dirPath); switch (event.type) { - case ChangeType.ADD: - print('File added: $relativePath'); - break; case ChangeType.MODIFY: - handleFileChange(); - print('File modified: $relativePath'); - break; - case ChangeType.REMOVE: - print('File removed: $relativePath'); + await handleFileChange(); break; } });