diff --git a/cli/commands/server.zig b/cli/commands/server.zig index b257a33..915677d 100644 --- a/cli/commands/server.zig +++ b/cli/commands/server.zig @@ -63,12 +63,15 @@ pub fn run( ); while (true) { - // TODO: Catch this error instead of propagating - try util.runCommand( + util.runCommand( allocator, realpath, &[_][]const u8{ "zig", "build", "-Djetzig_runner=true", "install", "--color", "on" }, - ); + ) catch { + std.debug.print("Build failed, waiting for file change...\n", .{}); + try awaitFileChange(allocator, cwd, &mtime); + continue; + }; const exe_path = try util.locateExecutable(allocator, cwd, .{}); if (exe_path == null) { @@ -98,22 +101,22 @@ pub fn run( std.process.exit(term.Exited); } - while (true) { - if (process.term) |_| { - _ = try process.wait(); - std.debug.print("Server exited, restarting...\n", .{}); - } + // HACK: This currenly doesn't restart the server when it exits, maybe that + // could be implemented in the future. - std.time.sleep(watch_changes_pause_duration); - - const new_mtime = try totalMtime(allocator, cwd, "src"); + awaitFileChange(allocator, cwd, mtime); + std.debug.print("Changes detected, restarting server...\n", .{}); + _ = try process.kill(); + } +} - if (new_mtime > mtime) { - std.debug.print("Changes detected, restarting server...\n", .{}); - _ = try process.kill(); - mtime = new_mtime; - break; - } +fn awaitFileChange(allocator: std.mem.Allocator, cwd: std.fs.Dir, mtime: *i128) !void { + while (true) { + std.time.sleep(watch_changes_pause_duration); + const new_mtime = try totalMtime(allocator, cwd, "src"); + if (new_mtime > mtime.*) { + mtime.* = new_mtime; + return; } } }