mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
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:
parent
19c04f1482
commit
f01abb86f5
@ -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");
|
||||
|
||||
if (new_mtime > mtime) {
|
||||
awaitFileChange(allocator, cwd, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user