镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 05b9f7d

Browse files
committed
fix: stop watch mode crashing on non-** content patterns and debounce rebuilds
setupWatch derived watch directories by splitting on '**', so a pattern like src/*.html or an explicit file entry handed fs.watch a glob string or file path and the uncaught ENOENT killed the whole watcher. Directories now derive from the first glob metacharacter, drop filename remainders, walk up to the nearest existing directory, and skip missing ones with a warning; watch() itself is guarded. Rebuilds are also debounced — one save fires multiple fs events and each previously triggered its own concurrent rebuild.
1 parent 202a5d2 commit 05b9f7d

1 file changed

Lines changed: 47 additions & 11 deletions

File tree

‎packages/crosswind/bin/cli.ts‎

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bun
22
import type { CrosswindConfig } from '../src/types'
3-
import { existsSync, watch } from 'node:fs'
3+
import { existsSync, statSync, watch } from 'node:fs'
44
import { unlink } from 'node:fs/promises'
5-
import { resolve } from 'node:path'
5+
import { dirname, resolve } from 'node:path'
66
import process from 'node:process'
77
import { CLI } from '@stacksjs/clapp'
88
import { version } from '../package.json'
@@ -143,20 +143,56 @@ async function runBuild(buildConfig: CrosswindConfig, options: BuildOptions): Pr
143143
function setupWatch(buildConfig: CrosswindConfig, options: BuildOptions): void {
144144
console.log('👀 Watching for changes...')
145145

146-
// Watch content directories
146+
// Derive a real directory per content pattern. Splitting on '**' alone
147+
// handed fs.watch glob strings ('src/*.html') or file paths, which throw
148+
// ENOENT and killed the whole watcher on the first non-** pattern.
147149
const watchDirs = new Set<string>()
148150
for (const pattern of buildConfig.content) {
149-
const dir = pattern.split('**')[0] || '.'
150-
watchDirs.add(dir)
151+
// Cut at the first glob metacharacter, then drop any filename remainder
152+
const metaIdx = pattern.search(/[*?[{]/)
153+
let dir = metaIdx === -1 ? pattern : pattern.slice(0, metaIdx)
154+
if (metaIdx !== -1 || !existsSync(dir) || !statSync(dir).isDirectory()) {
155+
dir = dir.slice(0, dir.lastIndexOf('/') + 1) || '.'
156+
}
157+
// Walk up to the nearest existing directory
158+
while (dir !== '.' && dir !== '/' && !existsSync(dir)) {
159+
const parent = dirname(dir)
160+
if (parent === dir)
161+
break
162+
dir = parent
163+
}
164+
if (existsSync(dir)) {
165+
watchDirs.add(dir)
166+
}
167+
else {
168+
console.warn(`⚠️ Skipping watch for missing path: ${pattern}`)
169+
}
170+
}
171+
172+
// Debounce: one save fires multiple fs events (rename + change); without
173+
// coalescing each triggered its own concurrent rebuild.
174+
let rebuildTimer: ReturnType<typeof setTimeout> | null = null
175+
const scheduleRebuild = (filename: string): void => {
176+
if (rebuildTimer)
177+
clearTimeout(rebuildTimer)
178+
rebuildTimer = setTimeout(async () => {
179+
rebuildTimer = null
180+
console.log(`\n📝 ${filename} changed, rebuilding...`)
181+
await runBuild(buildConfig, options)
182+
}, 50)
151183
}
152184

153185
for (const dir of watchDirs) {
154-
watch(dir, { recursive: true }, async (_eventType, filename) => {
155-
if (filename && /\.(?:html|js|ts|jsx|tsx|stx)$/.test(filename)) {
156-
console.log(`\n📝 ${filename} changed, rebuilding...`)
157-
await runBuild(buildConfig, options)
158-
}
159-
})
186+
try {
187+
watch(dir, { recursive: true }, (_eventType, filename) => {
188+
if (filename && /\.(?:html|js|ts|jsx|tsx|stx)$/.test(filename)) {
189+
scheduleRebuild(filename)
190+
}
191+
})
192+
}
193+
catch (error) {
194+
console.warn(`⚠️ Could not watch ${dir}:`, error instanceof Error ? error.message : error)
195+
}
160196
}
161197

162198
console.log(`\n👀 Watching: ${Array.from(watchDirs).join(', ')}`)

0 commit comments

Comments
 (0)