singularity-forge/scripts/watch-resources.js
Mikael Hugo d73a73d7f3 chore: node 24 native APIs, import.meta.dirname, parsers rename, dep updates
- Replace fileURLToPath(import.meta.url) with import.meta.dirname across
  scripts and extensions
- Rename parsers-legacy.ts → parsers.ts
- Remove deleted plan/spec docs (cicd-pipeline)
- Update package.json engines and deps across workspace packages
- Update web/package-lock.json

💘 Generated with Crush

Assisted-by: GLM-5.1 via Crush <crush@charm.land>
2026-05-02 06:18:25 +02:00

66 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Watch src/resources/ and sync changes to dist/resources/.
*
* Runs alongside `tsc --watch` to ensure non-TS resources (prompts, agents,
* skills, workflow files) are kept in sync with the build output.
*
* This solves the `npm link` branch-drift problem: without dist/resources/,
* `initResources()` reads from src/resources/ which changes with git branch
* switches, causing stale extensions to be synced to ~/.sf/agent/ for ALL
* projects using sf.
*/
import { watch } from 'node:fs'
import { cpSync, mkdirSync, rmSync } from 'node:fs'
import { resolve } from 'node:path'
const __dirname = import.meta.dirname
const src = resolve(__dirname, '..', 'src', 'resources')
const dest = resolve(__dirname, '..', 'dist', 'resources')
function sync() {
// Remove dest first to mirror deletions from src (prevents stale files)
rmSync(dest, { recursive: true, force: true })
mkdirSync(dest, { recursive: true })
cpSync(src, dest, { recursive: true, force: true })
}
// Initial sync
sync()
process.stderr.write(`[watch-resources] Initial sync done\n`)
// Watch for changes — recursive, debounced.
// fs.watch({ recursive: true }) is supported on macOS and Windows.
// On Linux (Node <20.13) it throws ERR_FEATURE_UNAVAILABLE_ON_PLATFORM.
// Fall back to polling on unsupported platforms.
let timer = null
let fsWatcher = null
let pollInterval = null
const onChange = () => {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
sync()
process.stderr.write(`[watch-resources] Synced at ${new Date().toLocaleTimeString()}\n`)
}, 300)
}
try {
fsWatcher = watch(src, { recursive: true }, onChange)
} catch {
// Fallback: poll every 2s (Linux without recursive watch support)
process.stderr.write(`[watch-resources] fs.watch recursive not supported, falling back to polling\n`)
pollInterval = setInterval(() => {
try { sync() } catch {}
}, 2000)
}
process.on('exit', () => {
if (timer) clearTimeout(timer)
if (fsWatcher) fsWatcher.close()
if (pollInterval) clearInterval(pollInterval)
})
process.stderr.write(`[watch-resources] Watching src/resources/ → dist/resources/\n`)