|
1 | 1 | #!/usr/bin/env bun |
2 | 2 | import type { CrosswindConfig } from '../src/types' |
3 | | -import { existsSync, watch } from 'node:fs' |
| 3 | +import { existsSync, statSync, watch } from 'node:fs' |
4 | 4 | import { unlink } from 'node:fs/promises' |
5 | | -import { resolve } from 'node:path' |
| 5 | +import { dirname, resolve } from 'node:path' |
6 | 6 | import process from 'node:process' |
7 | 7 | import { CLI } from '@stacksjs/clapp' |
8 | 8 | import { version } from '../package.json' |
@@ -143,20 +143,56 @@ async function runBuild(buildConfig: CrosswindConfig, options: BuildOptions): Pr |
143 | 143 | function setupWatch(buildConfig: CrosswindConfig, options: BuildOptions): void { |
144 | 144 | console.log('👀 Watching for changes...') |
145 | 145 |
|
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. |
147 | 149 | const watchDirs = new Set<string>() |
148 | 150 | 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) |
151 | 183 | } |
152 | 184 |
|
153 | 185 | 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 | + } |
160 | 196 | } |
161 | 197 |
|
162 | 198 | console.log(`\n👀 Watching: ${Array.from(watchDirs).join(', ')}`) |
|
0 commit comments