Skip to content

Commit

Permalink
show warning if file watching is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasstrehle committed Oct 16, 2024
1 parent 8afc49e commit 9437c8d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 32 deletions.
24 changes: 15 additions & 9 deletions src/app/backend-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,21 @@ export class BackendManager {

async watchFiles(){
let handling = false;
for await (const event of Deno.watchFs(this.#scope.normal_pathname, {recursive: true})) {
if (handling) continue;

handling = true;
for (const path of event.paths) {
this.handleFileUpdate(Path.File(path));
}
this.handleUpdate();
setTimeout(() => handling = false, 500)
try {
for await (const event of Deno.watchFs(this.#scope.normal_pathname, {recursive: true})) {
if (handling) continue;

handling = true;
for (const path of event.paths) {
this.handleFileUpdate(Path.File(path));
}
this.handleUpdate();
setTimeout(() => handling = false, 500)
}
}
catch (e) {
if (e.message?.includes("os error 38")) logger.warn("Watching for file changes is not supported");
else throw e;
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/runners/run-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,14 @@ export async function runLocal(params: runParams, root_path: URL, options: norma
else if (isWatching) {
console.log("waiting until files are updated...");
// error - wait until a file was modified before restart
for await (const _event of Deno.watchFs(new Path(root_path).normal_pathname, {recursive: true})) {
break;
try {
for await (const _event of Deno.watchFs(new Path(root_path).normal_pathname, {recursive: true})) {
break;
}
}
catch (e) {
if (e.message?.includes("os error 38")) logger.warn("Watching for file changes is not supported");
else throw e;
}
await run();
}
Expand Down
53 changes: 32 additions & 21 deletions src/server/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,22 @@ export class Transpiler {
if (!this.#src_dir) throw new Error("no src directory set");

// logger.info("file watcher activated for " + this.src_dir.pathname);

for await (const event of this.#main_fs_watcher = Deno.watchFs(this.src_dir.normal_pathname, {recursive: true})) {
try {
for (const path of event.paths) {
this.handleFileWatchUpdate(path);
}
}
catch (e) {
console.log("file update error:",e);
try {
for await (const event of this.#main_fs_watcher = Deno.watchFs(this.src_dir.normal_pathname, {recursive: true})) {
try {
for (const path of event.paths) {
this.handleFileWatchUpdate(path);
}
}
catch (e) {
console.log("file update error:",e);
}
}
}
catch (e) {
if (e.message?.includes("os error 38")) logger.warn("Watching for file changes is not supported");
else throw e;
}
}

// "debounce" multiple file updates
Expand Down Expand Up @@ -512,22 +517,28 @@ export class Transpiler {


private async syncVirtualFile(virtual_path:Path.File, src_path:Path.File){
const watcher = Deno.watchFs(src_path.pathname);
this.#fs_watchers.set(virtual_path.toString(), watcher);
try {
const watcher = Deno.watchFs(src_path.pathname);
this.#fs_watchers.set(virtual_path.toString(), watcher);

for await (const event of watcher) {
try {
for (const path of event.paths) {
const src_path = new Path(path);
for await (const event of watcher) {
try {
for (const path of event.paths) {
const src_path = new Path(path);

logger.info("#color(grey)file update: " + src_path.getAsRelativeFrom(this.src_dir.parent_dir).replace(/^\.\//, ''));
await this.updateVirtualFile(virtual_path, await Deno.readFile(src_path.normal_pathname));
}
}
catch (e) {
console.log("file update error:",e);
logger.info("#color(grey)file update: " + src_path.getAsRelativeFrom(this.src_dir.parent_dir).replace(/^\.\//, ''));
await this.updateVirtualFile(virtual_path, await Deno.readFile(src_path.normal_pathname));
}
}
catch (e) {
console.log("file update error:",e);
}
}
}
catch (e) {
if (e.message?.includes("os error 38")) logger.warn("Watching for file changes is not supported");
else throw e;
}
}

public updateVirtualFile(virtual_path:Path.File|string, content:string|Uint8Array) {
Expand Down

0 comments on commit 9437c8d

Please sign in to comment.