hotReload function
Performs a hot reload of the application by shutting down the current instance and restarting it with the same executable and arguments.
The function first shuts down the app gracefully using app.shutdown()
.
Then it starts a new process with the same Dart executable and script
arguments, inheriting the standard input/output streams. Finally, it
exits the current process with code 0, allowing the new process to take
over.
This function is typically used in conjunction with file watchers to automatically restart the application when source code changes are detected.
Implementation
void hotReload(App app) async {
await app.shutdown();
print("[dartcore] Hot reloading...");
print("[dartcore]");
var result = await Process.start(
Platform.executable,
Platform.executableArguments + [Platform.script.toFilePath()],
mode: ProcessStartMode.inheritStdio,
);
print("[dartcore] Server started in the background");
print("[dartcore] Your shell will inherit the new stdout/stderr streams.");
print("[dartcore] with PID: ${result.pid}");
exit(0);
}