-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.js
37 lines (30 loc) · 1004 Bytes
/
watch.js
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
35
36
37
const { basename, join } = require("path");
const { readFile, writeFile } = require("fs");
const { watch } = require("chokidar");
function watcher(path) {
readFile(path, "utf8", (err, scriptContent) => {
if (err) throw err;
let template = join("./node_modules/MyFirstCoffeeShop/src/templates", basename(path, ".js") + ".twig");
readFile(template, 'utf8', (err, templateContent) => {
if (err) throw err;
templateContent = templateContent.replace(/\n*<!-- start magic -->[\s\S]*?<!-- end magic -->/, "") + `
<!-- start magic -->
{% block footerJavaScript %}
<script>
${scriptContent}
</script>
{{ parent() }}
{% endblock %}
<!-- end magic -->`;
writeFile(template, templateContent, 'utf8', (err) => {
if (err) throw err;
console.log(`Successfully injected into ${basename(template)}.`)
});
});
});
}
watch("./src/*.js")
.on("add", watcher)
.on("change", watcher)
// .on("unlink", () => process.exit())
.on("error", console.error);