/
refresh-on-content-change.ts
41 lines (38 loc) · 1.1 KB
/
refresh-on-content-change.ts
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
import chokidar from 'chokidar'
import path from 'path'
import fs from 'fs'
import { postJSON } from './utils'
const watchPath = path.resolve(process.cwd(), 'content')
const refreshPath = path.resolve(process.cwd(), 'app', 'refresh.ignore.js')
const watcher = chokidar.watch(watchPath)
watcher.on('ready', () => {
watcher.on('all', (_eventName, changePath) => {
const relativeChangePath = changePath.replace(
`${path.resolve(process.cwd(), 'content')}/`,
'',
)
console.log('🛠 content changed', relativeChangePath)
postJSON({
http: require('http'),
postData: {
paths: [relativeChangePath],
},
overrideOptions: {
hostname: 'localhost',
port: 3000,
path: `/_content/update-content?${new URLSearchParams([
['_data', 'routes/_content/update-content'],
])}`,
},
})
.then(() => {
console.log('🚀 Finished updating content')
setTimeout(() => {
fs.writeFileSync(refreshPath, `// ${new Date()}`)
}, 250)
})
.catch(err => {
console.error(err)
})
})
})