Skip to content

Commit

Permalink
feat: cleaning up start cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
iyifr committed Aug 27, 2024
1 parent c0b4ead commit c24c1c2
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions bin/start.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,44 @@ void main(List<String> 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
Expand All @@ -36,7 +66,9 @@ void main(List<String> 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(
Expand All @@ -51,37 +83,20 @@ void main(List<String> 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();
Console.setBold(false);
});

// 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');
Expand All @@ -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;
}
});
Expand Down

0 comments on commit c24c1c2

Please sign in to comment.