2
2
3
3
import 'dart:io' ;
4
4
5
- import 'package:server_nano/server_nano.dart' ;
5
+ import 'package:dartx/dartx_io.dart' ;
6
+ import 'package:hot_restart_timeline/server.dart' ;
6
7
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;
11
11
12
+ Future <void > main () async {
12
13
// Watch for changes in lib/ and then call compile_js.dart
13
- final libDir = Directory ('lib' );
14
+ final libDir = spotPackageRoot. directory ('lib' );
14
15
15
16
final spotLibWatcher = libDir.watch (recursive: true );
16
17
spotLibWatcher.listen ((event) {
@@ -21,7 +22,7 @@ Future<void> main() async {
21
22
rebuildHtml ();
22
23
});
23
24
24
- final timelineHotReloadDir = Directory ('build/timeline/' );
25
+ final timelineHotReloadDir = spotPackageRoot. directory ('build/timeline/' );
25
26
if (! timelineHotReloadDir.existsSync ()) {
26
27
timelineHotReloadDir.createSync (recursive: true );
27
28
}
@@ -36,31 +37,8 @@ Future<void> main() async {
36
37
rebuildJs ();
37
38
rebuildHtml ();
38
39
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/' );
64
42
}
65
43
66
44
bool _rebuildingJs = false ;
@@ -79,7 +57,11 @@ Future<void> rebuildJs() async {
79
57
final timestamp = DateTime .now ().toIso8601String ().substring (11 , 19 );
80
58
print ('$timestamp Recompiling...' );
81
59
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
+ );
83
65
if (result.exitCode != 0 ) {
84
66
print ('Compilation failed' );
85
67
print (result.stdout);
@@ -110,7 +92,11 @@ Future<void> rebuildHtml() async {
110
92
// start a new process so that it picks up the changes in the jaspr code
111
93
final stopwatch = Stopwatch ()..start ();
112
94
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
+ );
114
100
if (result.exitCode != 0 ) {
115
101
print ('Render failed' );
116
102
print (result.stdout);
0 commit comments