22
33import 'dart:io' ;
44
5- import 'package:server_nano/server_nano.dart' ;
5+ import 'package:dartx/dartx_io.dart' ;
6+ import 'package:hot_restart_timeline/server.dart' ;
67
7- Future <void > main () async {
8- ProcessSignal .sigint.watch ().listen ((event) {
9- exit (0 );
10- });
8+ // Platform.script points to bin/main.dart
9+ final packageRoot = Directory (Platform .script.path).parent.parent;
10+ final spotPackageRoot = packageRoot.parent;
1111
12+ Future <void > main () async {
1213 // Watch for changes in lib/ and then call compile_js.dart
13- final libDir = Directory ('lib' );
14+ final libDir = spotPackageRoot. directory ('lib' );
1415
1516 final spotLibWatcher = libDir.watch (recursive: true );
1617 spotLibWatcher.listen ((event) {
@@ -21,7 +22,7 @@ Future<void> main() async {
2122 rebuildHtml ();
2223 });
2324
24- final timelineHotReloadDir = Directory ('build/timeline/' );
25+ final timelineHotReloadDir = spotPackageRoot. directory ('build/timeline/' );
2526 if (! timelineHotReloadDir.existsSync ()) {
2627 timelineHotReloadDir.createSync (recursive: true );
2728 }
@@ -36,31 +37,8 @@ Future<void> main() async {
3637 rebuildJs ();
3738 rebuildHtml ();
3839
39- final server = Server ();
40- server.static ('build/timeline/' );
41- server.get ('/' , (req, resp) {
42- final timelines = timelineHotReloadDir
43- .listSync (recursive: true )
44- .where ((file) => file.path.endsWith ('.html' ))
45- .map ((file) {
46- final relative = file.path.split ('build/timeline/' ).last;
47- return '<li><a href="/$relative ">$relative </a></li>\n ' ;
48- }).join ('\n ' );
49- resp.sendHtmlText (
50- '<h1>Spot timelines</h1>\n\n '
51- '<ul>\n $timelines </ul>' ,
52- );
53- });
54-
55- server.listen (port: 5907 );
56- final timelineFiles = timelineHotReloadDir.listSync (recursive: true );
57- for (final file in timelineFiles) {
58- if (! file.path.endsWith ('.html' )) {
59- continue ;
60- }
61- final relative = file.path.split ('build/timeline/' ).last;
62- print ('http://localhost:5907/$relative ' );
63- }
40+ startServer (timelineHotReloadDir);
41+ print ('http://localhost:5907/' );
6442}
6543
6644bool _rebuildingJs = false ;
@@ -79,7 +57,11 @@ Future<void> rebuildJs() async {
7957 final timestamp = DateTime .now ().toIso8601String ().substring (11 , 19 );
8058 print ('$timestamp Recompiling...' );
8159 try {
82- final result = await Process .run (dartExecutable, ['tool/compile_js.dart' ]);
60+ final result = await Process .run (
61+ dartExecutable,
62+ ['tool/compile_js.dart' ],
63+ workingDirectory: spotPackageRoot.path,
64+ );
8365 if (result.exitCode != 0 ) {
8466 print ('Compilation failed' );
8567 print (result.stdout);
@@ -110,7 +92,11 @@ Future<void> rebuildHtml() async {
11092 // start a new process so that it picks up the changes in the jaspr code
11193 final stopwatch = Stopwatch ()..start ();
11294 try {
113- final result = await Process .run (dartExecutable, ['tool/render_html.dart' ]);
95+ final result = await Process .run (
96+ dartExecutable,
97+ ['tool/render_html.dart' ],
98+ workingDirectory: packageRoot.path,
99+ );
114100 if (result.exitCode != 0 ) {
115101 print ('Render failed' );
116102 print (result.stdout);
0 commit comments