-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
dev.ts
58 lines (50 loc) · 1.36 KB
/
dev.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import chokidar from 'chokidar'
import pc from 'picocolors'
import { resolveConfigFile } from '../config.js'
import { createContext } from '../context.js'
import { createServer } from '../server.js'
export interface DevOptions {
port: number
}
export async function devCommand (options: DevOptions) {
let stop: () => Promise<void>
async function start () {
const ctx = await createContext({
mode: 'dev',
})
const { server, close } = await createServer(ctx, { port: options.port })
server.printUrls()
// Histoire config watcher
let watcher: chokidar.FSWatcher
if (server.config.configFile) {
watcher = chokidar.watch(server.config.configFile, {
ignoreInitial: true,
})
watcher.on('change', () => {
restart('Vite')
})
}
return async () => {
await watcher?.close()
await close()
}
}
async function restart (source: string) {
if (stop) {
console.log(pc.blue(`${source} config changed, restarting...`))
await stop()
stop = null // Don't call stop again until new start() is done
stop = await start()
}
}
stop = await start()
const configFile = await resolveConfigFile()
if (configFile) {
const watcher = chokidar.watch(configFile, {
ignoreInitial: true,
})
watcher.on('change', () => {
restart('Histoire')
})
}
}