Skip to content

Commit

Permalink
compile-all now runs in watch a nonwatch modes
Browse files Browse the repository at this point in the history
  • Loading branch information
azizghuloum committed Dec 28, 2024
1 parent 2fd4234 commit 7e4582a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "vitest",
"watch": "tsc -b --watch --noEmit",
"rts-watch-with-deno": "deno --watch --unstable-sloppy-imports --allow-all ./rtsc/watch.ts",
"compile-all": "deno --watch --unstable-sloppy-imports --allow-all ./rtsc/compile-all.ts",
"compile-all": "deno run --watch --unstable-sloppy-imports --allow-all ./rtsc/compile-all.ts --watch",
"rts-watch": "vite-node --watch ./rtsc/watch.ts",
"coverage": "vitest run --coverage",
"build": "tsc -b && vite build",
Expand All @@ -23,6 +23,7 @@
"@types/node": "^22.10.2",
"@uiw/codemirror-theme-abcdef": "^4.23.7",
"@uiw/react-codemirror": "^4.23.7",
"commander": "^12.1.0",
"ignore": "^6.0.2",
"index-to-position": "^1.0.0",
"json-stable-stringify": "^1.2.1",
Expand Down
32 changes: 27 additions & 5 deletions rtsc/compile-all.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { watch } from "node:fs";
import { watch as node_watch } from "node:fs";
import fs from "node:fs/promises";
import process from "node:process";
import path, { basename, dirname, join } from "node:path";
import ignore from "ignore";
import * as commander from "commander";

import { LibraryManager } from "../src/library-manager.ts";
import { get_globals } from "../src/global-module.ts";
import { core_patterns } from "../src/syntax-core-patterns.ts";
import { parse } from "../src/parse.ts";
import { assert } from "../src/assert.ts";

type watch_proc = (
path: string,
callback: (file: string) => void,
) => {
close(): void;
};

class DirWatcher {
private dir: string;
private callbacks: { [filename: string]: (path: string) => void } = {};
Expand Down Expand Up @@ -39,7 +47,9 @@ class DirWatcher {
class WatchFS {
dir_watchers: { [path: string]: DirWatcher } = {};
onRTSFile: (path: string) => void;
constructor(onRTSFile: (path: string) => void) {
watch: watch_proc;
constructor(watch: watch_proc, onRTSFile: (path: string) => void) {
this.watch = watch;
this.onRTSFile = onRTSFile;
this.init();
}
Expand All @@ -55,8 +65,7 @@ class WatchFS {
if (existing) return existing;
const watcher = new DirWatcher(abspath);
this.dir_watchers[abspath] = watcher;
watch(abspath, { encoding: "utf8", recursive: false }, (_event, file) => {
assert(file !== null);
this.watch(abspath, (file) => {
watcher.processEvent(file);
});
return watcher;
Expand Down Expand Up @@ -121,4 +130,17 @@ function check_path(rts_file: string) {
.then(([pkg, rel]) => library_manager.ensureUpToDate(pkg, rel, rts_file));
}

const FS = new WatchFS(check_path);
const program = new commander.Command();
program.option("-w, --watch", "watch files and directories for changes");
program.parse();
const options = program.opts();

const watch_proc: watch_proc = options.watch
? (path, callback) =>
node_watch(path, { encoding: "utf8", recursive: false }, (_event, file) => {
assert(file !== null);
callback(file);
})
: (path, callback) => ({ close() {} });

const FS = new WatchFS(watch_proc, check_path);

0 comments on commit 7e4582a

Please sign in to comment.