-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch-file.ts
More file actions
34 lines (30 loc) · 992 Bytes
/
Copy pathwatch-file.ts
File metadata and controls
34 lines (30 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const { watch } = require("node:fs/promises");
const file = process.argv[2];
function previewMustache(filepath: string) {
const { readFile } = require("node:fs/promises");
const { render } = require("mustache");
const { basename } = require("node:path");
return readFile(filepath, "utf8").then((template: string) => {
const filename = basename(filepath);
const rendered = render(template, { filename });
// write to a file
const { writeFile } = require("node:fs/promises");
const { join } = require("node:path");
const { dirname } = require("node:path");
return writeFile(join(dirname(filepath), `${filename}.html`), rendered);
});
}
(async () => {
try {
const watcher = watch(file);
for await (const event of watcher) {
console.log(event);
if (event.eventType === "change") {
await previewMustache(file);
}
}
} catch (err: any) {
if (err.name === "AbortError") return;
console.error(err);
}
})();