Skip to content

Commit 6722253

Browse files
committedDec 11, 2024··
feat: watch config file & package.json
1 parent cd64612 commit 6722253

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed
 

‎src/features/watch.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ import { logger } from '../utils/logger'
44
import type { ResolvedOptions } from '../options'
55
import type { FSWatcher } from 'chokidar'
66

7-
// TODO watch config files
7+
const endsWithPackageJson = /[\\/]package\.json$/
8+
89
export async function watchBuild(
910
options: ResolvedOptions,
11+
configFile: string | undefined,
1012
rebuild: () => void,
13+
restart: () => void,
1114
): Promise<FSWatcher> {
1215
const { watch } = await import('chokidar')
1316
const debouncedRebuild = debounce(rebuild, 100)
1417

15-
const files =
16-
typeof options.watch === 'boolean' ? process.cwd() : options.watch
17-
logger.info(`Watching for changes in ${toArray(files).join(', ')}`)
18+
const files = toArray(
19+
typeof options.watch === 'boolean' ? process.cwd() : options.watch,
20+
)
21+
logger.info(`Watching for changes in ${files.join(', ')}`)
22+
if (configFile) files.push(configFile)
1823

1924
const watcher = watch(files, {
2025
ignoreInitial: true,
@@ -25,7 +30,13 @@ export async function watchBuild(
2530
},
2631
})
2732

28-
watcher.on('all', (type, file) => {
33+
watcher.on('all', (type: string, file: string) => {
34+
if (endsWithPackageJson.test(file) || configFile === file) {
35+
logger.info(`Reload config: ${file}`)
36+
restart()
37+
return
38+
}
39+
2940
logger.info(`Change detected: ${type} ${file}`)
3041
debouncedRebuild()
3142
})

‎src/index.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ export async function build(
4040
const rebuild = rebuilds[i]
4141
if (!rebuild) continue
4242

43-
const watcher = await watchBuild(resolved, rebuild)
43+
const watcher = await watchBuild(resolved, configFile, rebuild, restart)
4444
cleanCbs.push(() => watcher.close())
4545
}
4646

4747
if (cleanCbs.length) {
48-
shortcuts(async () => {
49-
for (const clean of cleanCbs) {
50-
await clean()
51-
}
52-
build(userOptions)
53-
})
48+
shortcuts(restart)
49+
}
50+
51+
async function restart() {
52+
for (const clean of cleanCbs) {
53+
await clean()
54+
}
55+
build(userOptions)
5456
}
5557
}
5658

0 commit comments

Comments
 (0)
Please sign in to comment.