-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch-and-copy.js
75 lines (65 loc) · 2.29 KB
/
watch-and-copy.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
64
65
66
67
68
69
70
71
72
73
74
75
/*
This script exists to make developing locally easier. When run it sets up a watcher that outputs files into the given dir (which you should set to the foundry dir ofc)
*/
const fs = require("fs");
const chokidar = require("chokidar");
const path = require("path");
// Read command line arguments for source and destination directories
const [, , srcDir = "./", destDir] = process.argv;
if (!destDir) {
console.error(
"Please provide a destination directory. This should be the directory inside your local foundry modules. eg. C://Users/{YOUR NAME}/AppData/Local/FoundryVTT/Data/modules/maru-combat-themes"
);
process.exit(1);
}
if (!fs.existsSync(srcDir)) {
console.error(`Didn't find the src directory ${srcDir}. Ensure its valid!`);
process.exit(1);
}
if (!fs.existsSync(destDir)) {
console.error(
`Didn't find the destination directory ${destDir}. Ensure its valid!`
);
process.exit(1);
}
const watcher = chokidar.watch(srcDir, { ignoreInitial: true });
watcher.on("add", (filePath) => {
const fileName = path.basename(filePath);
const destPath = path.join(destDir, filePath);
console.log(`File ${filePath} added, copying to ${destPath}...`);
fs.copyFile(filePath, destPath, (err) => {
if (err) {
console.error(`Error copying file ${fileName}: ${err}`);
} else {
console.log(`File ${fileName} copied successfully.`);
}
});
});
watcher.on("change", (filePath) => {
const fileName = path.basename(filePath);
const destPath = path.join(destDir, filePath);
console.log(`File ${filePath} changed, copying to ${destPath}...`);
fs.copyFile(filePath, destPath, (err) => {
if (err) {
console.error(`Error copying file ${fileName}: ${err}`);
} else {
console.log(`File ${fileName} copied successfully.`);
}
});
});
watcher.on("unlink", (filePath) => {
const fileName = path.basename(filePath);
const destPath = path.join(destDir, filePath);
console.log(
`File ${filePath} removed from watch, deleting from ${destPath}...`
);
fs.unlink(destPath, (err) => {
if (err) {
console.error(`Error deleting file ${fileName}: ${err}`);
} else {
console.log(`File ${fileName} deleted successfully.`);
}
});
});
console.log(`Watching for changes to files in ${srcDir}...`);
console.log(`Copying files to ${destDir}...`);