-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatcher.mjs
104 lines (96 loc) · 2.42 KB
/
watcher.mjs
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
100
101
102
103
104
import path from 'path';
import chalk from 'chalk';
import chokidar from 'chokidar';
import { execa } from 'execa';
const systemRootPath = path.resolve('system');
let state;
let currentTimeout;
let triggeredFiles = [];
function setState(newState, filepath) {
if (filepath) {
triggeredFiles.push(filepath);
}
if (state === newState) {
return;
}
state = newState;
// eslint-disable-next-line no-console
console.log(chalk.bgCyan(` ${state} `));
if (newState === 'running') {
// eslint-disable-next-line no-console
console.log(
chalk.gray(
` Triggered from: \n\t${triggeredFiles
.map((f) => path.relative(systemRootPath, f))
.join('\n\t')}`
)
);
triggeredFiles = [];
}
// eslint-disable-next-line no-console
console.log('');
}
setState('waiting');
async function runBuild() {
setState('running');
await execa(
'lerna',
[
'run',
'build',
'--scope="@tablecheck/tablekit-react"',
'--scope="@tablecheck/tablekit-react-css"',
'--scope="@tablecheck/tablekit-react-datepicker"',
'--scope="@tablecheck/tablekit-react-select"'
],
{
cwd: path.join(systemRootPath, '..'),
stdio: 'inherit'
}
);
if (state === 'pending') {
runBuild();
} else {
setState('waiting');
}
}
function rebuild(filepath) {
if (state === 'starting') return;
if (state === 'running' || state === 'pending') {
setState('pending', filepath);
return;
}
setState('starting', filepath);
clearTimeout(currentTimeout);
currentTimeout = setTimeout(runBuild, 4000);
}
const watcher = chokidar.watch(
[
path.join(systemRootPath, 'core/src/**/*'),
path.join(systemRootPath, 'react/src/structuredComponents/**/*'),
path.join(systemRootPath, 'react/src/*'),
path.join(systemRootPath, 'react-css/src/structuredComponents/**/*'),
path.join(systemRootPath, 'react-css/src/*'),
path.join(systemRootPath, 'react-datepicker/src/**/*'),
path.join(systemRootPath, 'react-select/src/**/*')
],
{
ignored: /\/src\/(index|config)\.ts$|node_modules|(^|[/\\])\../,
ignoreInitial: true,
persistent: true,
awaitWriteFinish: true
}
);
watcher
.on('add', (filepath) => {
rebuild(filepath);
})
.on('change', (filepath) => {
rebuild(filepath);
})
.on('unlink', (filepath) => {
rebuild(filepath);
})
.on('error', (error) => {
console.error('Error happened', error);
});