Skip to content

Instantly share code, notes, and snippets.

@code-shoily
Created December 23, 2018 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-shoily/42b8b0760dda50a3b51ec72c52fe4705 to your computer and use it in GitHub Desktop.
Save code-shoily/42b8b0760dda50a3b51ec72c52fe4705 to your computer and use it in GitHub Desktop.
Restart Aqueduct devserver upon file change [ Attemp #1 ]. Replace `bin/main.dart` with this.
/// Tried to autoreloading aqueduct on code change. First attempt off my head.
/// Dependency: `watcher: ^0.9.7+10`, maybe this whole thing is a bad idea?
///
/// TODO:
/// 1. Refactor so that it has minimal code change from the generated `bin/main.dart`
/// 2. Reload only once if lots of files change (i.e. scaffolding while devserver was running)
/// 3. Take hints from configuration file
/// 4. What to do if error occurs?
import "package:watcher/watcher.dart";
import 'package:backend/backend.dart';
Future _getApp([List<String> infoMessages]) async {
final app = Application<BackendChannel>()
..options.configurationFilePath = "config.yaml"
..options.port = 8888;
final count = Platform.numberOfProcessors ~/ 2;
await app.start(numberOfInstances: count > 0 ? count : 1);
print(infoMessages.join("\n"));
return app;
}
Future runApp() async {
var app = await _getApp(["App started on port: 8888", "CTRL+C to Exit"]);
final watcher =
DirectoryWatcher("lib"); // From config, and do some path-fu here?
watcher.events.listen((event) async {
final bool shouldReload = // From config? Maybe throttling here?
[ChangeType.REMOVE, ChangeType.MODIFY].contains(event.type) ||
event.path.endsWith(".dart");
if (shouldReload) {
await app.stop();
app = await _getApp(["File Changed: ${event}, reload app"]);
}
});
}
Future main() async {
await runApp();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment