-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_with_web_components_watch.js
63 lines (57 loc) · 2.3 KB
/
link_with_web_components_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* This file watches the original Unipept web components directory for changes and automatically applies these changes
* to the local copy of the Unipept web components (inside the node_modules folder).
*
* This script is only here for use during development of Unipept and should not be used in the production environment.
* The package Chokidar is required to run this file.
*/
const chokidar = require("chokidar");
const fs = require("fs");
const path = require("path");
const distDirectory = "./../unipept-web-components/dist";
const typesDirectory = "./../unipept-web-components/types";
const errHandler = function (err) {
if (err) {
console.error(err);
}
};
console.log("Watching file system for changes...");
// Watch for changes to the web components directory.
chokidar.watch(distDirectory)
.on("all", (event, currentPath) => {
if (!currentPath.includes("/node_modules") && !currentPath.includes("/.git")) {
const filteredPath = currentPath.replace("../unipept-web-components", "./node_modules/unipept-web-components");
const directory = path.dirname(filteredPath);
switch (event) {
case "add":
fs.mkdirSync(directory, {recursive: true}, errHandler);
fs.copyFile(currentPath, filteredPath, errHandler);
break;
case "change":
fs.copyFile(currentPath, filteredPath, errHandler);
break;
case "unlink":
fs.unlink(filteredPath, errHandler);
break;
}
console.log(event, currentPath);
}
});
chokidar.watch(typesDirectory)
.on("all", (event, currentPath) => {
const filteredPath = currentPath.replace("../unipept-web-components", "./node_modules/unipept-web-components");
const directory = path.dirname(filteredPath);
switch (event) {
case "add":
fs.mkdirSync(directory, {recursive: true}, errHandler);
fs.copyFile(currentPath, filteredPath, errHandler);
break;
case "change":
fs.copyFile(currentPath, filteredPath, errHandler);
break;
case "unlink":
fs.unlink(filteredPath, errHandler);
break;
}
console.log(event, currentPath);
});