| ... | ... | @@ -3,11 +3,11 @@ |
| 3 | 3 | const debounceMilliseconds = 25;
|
| 4 | 4 |
|
| 5 | 5 | export async function main() {
|
| 6 | | // Catch up state
|
| 6 | // Catch up state by running a main build.
|
| 7 | 7 | const { incr } = await generate.main();
|
| 8 | |
|
| 9 | | // Initialize a watch
|
| 8 | // ...and watch the files that cause invals.
|
| 10 | 9 | const watch = new Watch(rebuild);
|
| 10 | watch.add(...incr.invals.keys());
|
| 11 | 11 | statusLine();
|
| 12 | 12 |
|
| 13 | 13 | function rebuild(files: string[]) {
|
| ... | ... | @@ -21,7 +21,7 @@ export async function main() { |
| 21 | 21 | return;
|
| 22 | 22 | }
|
| 23 | 23 | withSpinner<Record<string, unknown>, any>({
|
| 24 | | text: "Recovering State",
|
| 24 | text: "Rebuilding",
|
| 25 | 25 | successText: generate.successText,
|
| 26 | 26 | failureText: () => "sitegen FAIL",
|
| 27 | 27 | }, async (spinner) => {
|
| ... | ... | @@ -34,6 +34,10 @@ export async function main() { |
| 34 | 34 | );
|
| 35 | 35 | const result = await generate.sitegen(spinner, incr);
|
| 36 | 36 | incr.toDisk(); // Allows picking up this state again
|
| 37 | for (const file of watch.files) {
|
| 38 | const relative = path.relative(hot.projectRoot, file);
|
| 39 | if (!incr.invals.has(file)) watch.remove(file);
|
| 40 | }
|
| 37 | 41 | return result;
|
| 38 | 42 | }).catch((err) => {
|
| 39 | 43 | console.error(util.inspect(err));
|
| ... | ... | @@ -41,7 +45,6 @@ export async function main() { |
| 41 | 45 | }
|
| 42 | 46 |
|
| 43 | 47 | function statusLine() {
|
| 44 | | watch.add(...incr.invals.keys());
|
| 45 | 48 | console.info(
|
| 46 | 49 | `Watching ${incr.invals.size} files \x1b[36m[last change: ${
|
| 47 | 50 | new Date().toLocaleTimeString()
|
| ... | ... | @@ -104,6 +107,22 @@ class Watch { |
| 104 | 107 | }
|
| 105 | 108 | }
|
| 106 | 109 |
|
| 110 | remove(...files: string[]) {
|
| 111 | for (const file of files) this.files.delete(path.resolve(file));
|
| 112 | // Find watches that are covering no files
|
| 113 | const { roots, watchers } = this;
|
| 114 | const existingFiles = Array.from(this.files);
|
| 115 | let i = roots.length;
|
| 116 | while (i > 0) {
|
| 117 | i -= 1;
|
| 118 | const root = roots[i];
|
| 119 | if (!existingFiles.some((file) => file.startsWith(root))) {
|
| 120 | watchers.splice(i, 1)[0].close();
|
| 121 | roots.splice(i, 1);
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 107 | 126 | stop() {
|
| 108 | 127 | for (const w of this.watchers) w.close();
|
| 109 | 128 | }
|