-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.mjs
58 lines (49 loc) · 1.67 KB
/
bundle.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
import { watch } from 'chokidar'
import { spawn } from 'node:child_process'
// JOHNNY monitor `tailwind.config.mjs` and `tsconfig.distribute.json`
const pathToWatch = './src'
// Function to handle the execution of the build command
function runBuildCommand() {
const buildProcess = spawn('pnpm', ['build'])
buildProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`)
})
buildProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`)
})
buildProcess.on('error', (error) => {
console.error(`Error: ${error.message}`)
})
buildProcess.on('close', (code) => {
console.log(`Child process exited with code ${code}`)
})
}
const watcher = watch(pathToWatch, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
})
// Add event listeners.
let IS_READY = false
watcher
.on('add', (path) => {
IS_READY && console.log(`File ${path} has been added`)
IS_READY && runBuildCommand()
})
.on('change', (path) => {
IS_READY && console.log(`File ${path} has been changed`)
IS_READY && runBuildCommand()
})
.on('unlink', (path) => {
IS_READY && console.log(`File ${path} has been removed`)
IS_READY && runBuildCommand()
})
// More possible events.
watcher
// .on('addDir', (path) => console.log(`Directory ${path} has been added`))
// .on('unlinkDir', (path) => console.log(`Directory ${path} has been removed`))
.on('error', (error) => console.log(`Watcher error: ${error}`))
.on('ready', () => {
console.log('Monitoring ', pathToWatch, 'for changes')
IS_READY = true
runBuildCommand()
})