Cli: Reload for fail build

When zig build fails, the `jetzig server` command also exits. This
should be an uninteneded behavior. This commit fixes this by catching
failed build and restart after file change is detected.
Fixes #33
This commit is contained in:
Froxcey 2024-03-25 16:55:12 +08:00
parent 19c04f1482
commit f01abb86f5
No known key found for this signature in database
GPG Key ID: 621379E1D6250388

View File

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