-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.js
99 lines (87 loc) · 2.42 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { readdir, stat, readFile, mkdir, copyFile, existsSync } from 'fs';
import { join, extname } from 'path';
import { exec } from 'child_process';
const watchDirectories = ['./compiled', './src-tauri'];
const command = 'npm run tauri';
const fileStates = {};
function runCommand(cmd) {
exec(cmd, (error, stdout, stderr) => {
if (error) console.error(`[watcher] Error: ${error}`);
console.log(stdout);
console.log(stderr);
});
}
function addCheck(directory, file) {
const filePath = join(directory, file);
stat(filePath, (err, stats) => {
if (err) {
console.error(`[watcher] Error getting file stats for ${filePath}: ${err}`);
return;
}
if (stats.isFile()) {
readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`[watcher] Error reading file ${filePath}: ${err}`);
return;
}
const prevState = fileStates[filePath] || {};
if (prevState.content !== data) {
const ext = extname(filePath);
if (ext === '.less') {
runCommand("npm run less");
}
runCommand(command);
fileStates[filePath] = { content: data, mtime: stats.mtime };
}
});
}
});
}
function checkFileChanges() {
addCheck('./src', 'style.less');
for (const directory of watchDirectories) {
readdir(directory, (err, files) => {
if (err) {
console.error(`[watcher] Error reading directory: ${directory}: ${err}`);
return;
}
files.forEach((file) => {
addCheck(directory, file)
});
});
}
}
function compiledDirAndFont() {
const directory = './compiled';
const srcFile = './src/manrope.woff2';
const destFile = join(directory, 'manrope.woff2');
if (!existsSync(directory)) {
mkdir(directory, (err) => {
if (err) console.error(`[watcher] Error creating directory: ${directory}: ${err}`);
else copyFontFile(srcFile, destFile);
});
} else {
copyFontFile(srcFile, destFile);
}
}
function copyFontFile(src, dest) {
copyFile(src, dest, (err) => {
if (err) console.error(`[watcher] Error copying file: ${err}`);
else console.log(`[watcher] Copied ${src} to ${dest}`);
});
}
const args = process.argv.slice(2);
if (args.includes('-once')) {
runCommand('npm run pug');
runCommand('npm run cs');
runCommand('npm run less');
runCommand('npm run tauri');
compiledDirAndFont();
} else {
runCommand('npm run pug');
runCommand('npm run cs');
runCommand('npm run less');
runCommand('npm run tauri');
compiledDirAndFont();
setInterval(checkFileChanges, 1000);
}