@xrystal/core 3.53.2 → 3.54.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/x/bin/commands/watch.mjs +91 -0
- package/x/bin/helpers/cli-help.mjs +1 -0
- package/x/bin/helpers/workspace-heal.mjs +6 -0
- package/x/bin/helpers/xrystal-watch-daemon.mjs +174 -0
- package/x/bin/helpers/xrystal-watch-spawn.mjs +92 -0
- package/x/bin/main-cli.js +2 -0
- package/x/bin/scripts/xrystal-watch-daemon.mjs +5 -0
package/package.json
CHANGED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
|
|
3
|
+
import { printCommandFooter, printCommandHeader } from '../helpers/banner.mjs'
|
|
4
|
+
import { CLI_PRIMARY_NAME } from '../helpers/cli-identity.mjs'
|
|
5
|
+
import { readCliPackageJson } from '../helpers/project-root.mjs'
|
|
6
|
+
import { collectWatchTargets } from '../helpers/xrystal-watch-daemon.mjs'
|
|
7
|
+
import { readRegistry, resolveXrystalHome } from '../helpers/xrystal-registry.mjs'
|
|
8
|
+
import {
|
|
9
|
+
ensureWatchDaemon,
|
|
10
|
+
startWatchDaemon,
|
|
11
|
+
stopWatchDaemon,
|
|
12
|
+
watchDaemonStatus,
|
|
13
|
+
watchLogPath,
|
|
14
|
+
} from '../helpers/xrystal-watch-spawn.mjs'
|
|
15
|
+
|
|
16
|
+
export function registerWatchCommand(cli, { cliName = CLI_PRIMARY_NAME } = {}) {
|
|
17
|
+
const watch = cli
|
|
18
|
+
.command('watch')
|
|
19
|
+
.description('~/.xrystal background tmp watcher — auto self-heal for registered workspaces')
|
|
20
|
+
|
|
21
|
+
watch
|
|
22
|
+
.command('start', { isDefault: true })
|
|
23
|
+
.description('Start background daemon (also auto-starts on workspace sync/project)')
|
|
24
|
+
.option('--foreground', 'Run in foreground (debug)')
|
|
25
|
+
.action((options) => {
|
|
26
|
+
const version = readCliPackageJson().version
|
|
27
|
+
printCommandHeader({ cliName, command: 'watch start', version })
|
|
28
|
+
|
|
29
|
+
const result = startWatchDaemon({ detached: !options.foreground, quiet: false })
|
|
30
|
+
if (result.started) {
|
|
31
|
+
console.log(chalk.green(` daemon started (pid ${result.pid})`))
|
|
32
|
+
console.log(chalk.gray(` log: ${watchLogPath()}`))
|
|
33
|
+
} else if (result.running) {
|
|
34
|
+
console.log(chalk.gray(` already running (pid ${result.pid})`))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
printCommandFooter({ version })
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
watch
|
|
41
|
+
.command('stop')
|
|
42
|
+
.description('Stop background daemon')
|
|
43
|
+
.action(() => {
|
|
44
|
+
const version = readCliPackageJson().version
|
|
45
|
+
printCommandHeader({ cliName, command: 'watch stop', version })
|
|
46
|
+
|
|
47
|
+
const result = stopWatchDaemon()
|
|
48
|
+
if (result.stopped) {
|
|
49
|
+
console.log(chalk.green(` stopped pid ${result.pid}`))
|
|
50
|
+
} else {
|
|
51
|
+
console.log(chalk.gray(' not running'))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
printCommandFooter({ version })
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
watch
|
|
58
|
+
.command('status')
|
|
59
|
+
.description('Show daemon state and watched tmp paths')
|
|
60
|
+
.action(() => {
|
|
61
|
+
const version = readCliPackageJson().version
|
|
62
|
+
printCommandHeader({ cliName, command: 'watch status', version })
|
|
63
|
+
|
|
64
|
+
const status = watchDaemonStatus()
|
|
65
|
+
console.log(chalk.gray(` home: ${resolveXrystalHome()}`))
|
|
66
|
+
console.log(status.running
|
|
67
|
+
? chalk.green(` running pid ${status.pid}`)
|
|
68
|
+
: chalk.yellow(' not running'))
|
|
69
|
+
|
|
70
|
+
const targets = collectWatchTargets(readRegistry())
|
|
71
|
+
console.log(chalk.gray(` watching ${targets.length} tmp file(s)`))
|
|
72
|
+
for (const target of targets) {
|
|
73
|
+
console.log(` • [${target.kind}] ${target.file}`)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (status.running) {
|
|
77
|
+
console.log(chalk.gray(`\n log: ${watchLogPath()}`))
|
|
78
|
+
} else {
|
|
79
|
+
console.log(chalk.dim('\n tip: auto-starts on xry sync / xry project inside a workspace'))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
printCommandFooter({ version })
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
watch
|
|
86
|
+
.command('ensure')
|
|
87
|
+
.description('Start daemon only if not running (internal / CI)')
|
|
88
|
+
.action(() => {
|
|
89
|
+
ensureWatchDaemon({ quiet: false })
|
|
90
|
+
})
|
|
91
|
+
}
|
|
@@ -13,6 +13,7 @@ const QUICK_COMMANDS = [
|
|
|
13
13
|
{ cmd: 'tmp', desc: 'Ensure tmp.yml for current scope (inner x/ or outer ./)' },
|
|
14
14
|
{ cmd: 'create <name> --workspace', desc: 'Outer tmp + api/general + web/mobile slots' },
|
|
15
15
|
{ cmd: 'workspaces', desc: 'Global registry ~/.xrystal (not repo tmp.yml)' },
|
|
16
|
+
{ cmd: 'watch status', desc: 'Background tmp watcher for registered workspaces (~/.xrystal)' },
|
|
16
17
|
{ cmd: 'dev / build / doctor', desc: 'Develop & diagnostics' },
|
|
17
18
|
]
|
|
18
19
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
} from './workspace-resolve.mjs'
|
|
5
5
|
import { healRegistryAtPath } from './xrystal-registry.mjs'
|
|
6
6
|
import { ensureOuterWorkspaceTmp } from './workspace-tmp.mjs'
|
|
7
|
+
import { ensureWatchDaemon } from './xrystal-watch-spawn.mjs'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Workspace self-heal: slot dirs, outer tmp options, ~/.xrystal registry.
|
|
@@ -13,6 +14,7 @@ export function runWorkspaceSelfHeal(workspaceRoot, {
|
|
|
13
14
|
ensureTmp = true,
|
|
14
15
|
healRegistry = true,
|
|
15
16
|
forceRegistry = false,
|
|
17
|
+
ensureDaemon = true,
|
|
16
18
|
} = {}) {
|
|
17
19
|
if (ensureTmp) {
|
|
18
20
|
ensureOuterWorkspaceTmp(workspaceRoot)
|
|
@@ -26,5 +28,9 @@ export function runWorkspaceSelfHeal(workspaceRoot, {
|
|
|
26
28
|
registryChanged = healRegistryAtPath(workspaceRoot, { force: forceRegistry }).changed
|
|
27
29
|
}
|
|
28
30
|
|
|
31
|
+
if (ensureDaemon && healRegistry) {
|
|
32
|
+
ensureWatchDaemon({ quiet: true })
|
|
33
|
+
}
|
|
34
|
+
|
|
29
35
|
return { slotDirsCreated, tmpOptionsPatched, registryChanged }
|
|
30
36
|
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import fs, { unwatchFile, watchFile } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { packageDefaults } from './package-defaults.mjs'
|
|
5
|
+
import { findEntries, readWorkspaceCoreTmp } from './workspace-resolve.mjs'
|
|
6
|
+
import { readRegistry, registryFilePath, resolveXrystalHome } from './xrystal-registry.mjs'
|
|
7
|
+
import { runWorkspaceSelfHeal } from './workspace-heal.mjs'
|
|
8
|
+
import { watchPidPath } from './xrystal-watch-spawn.mjs'
|
|
9
|
+
|
|
10
|
+
const DEBOUNCE_MS = 200
|
|
11
|
+
const REGISTRY_RESCAN_MS = 30_000
|
|
12
|
+
|
|
13
|
+
function defaultWatchIntervalMs() {
|
|
14
|
+
return packageDefaults.tmpWatchDefaults?.intervalMs ?? 500
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function resolveWorkspaceWatchIntervalMs(workspaceRoot) {
|
|
18
|
+
try {
|
|
19
|
+
return readWorkspaceCoreTmp(workspaceRoot).fileWatchIntervalMs ?? defaultWatchIntervalMs()
|
|
20
|
+
} catch {
|
|
21
|
+
return defaultWatchIntervalMs()
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Collect outer + inner tmp.yml paths for all registered workspaces. */
|
|
26
|
+
export function collectWatchTargets(registry = readRegistry()) {
|
|
27
|
+
const targets = []
|
|
28
|
+
const seen = new Set()
|
|
29
|
+
|
|
30
|
+
for (const ws of registry.workspaces ?? []) {
|
|
31
|
+
if (!ws?.path || !fs.existsSync(ws.path)) continue
|
|
32
|
+
|
|
33
|
+
const outer = path.join(ws.path, 'tmp.yml')
|
|
34
|
+
if (fs.existsSync(outer)) {
|
|
35
|
+
const key = path.resolve(outer)
|
|
36
|
+
if (!seen.has(key)) {
|
|
37
|
+
seen.add(key)
|
|
38
|
+
targets.push({ file: key, workspaceRoot: ws.path, kind: 'outer' })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
for (const entry of findEntries(ws.path)) {
|
|
44
|
+
if (!entry.tmp || !fs.existsSync(entry.tmp)) continue
|
|
45
|
+
const key = path.resolve(entry.tmp)
|
|
46
|
+
if (seen.has(key)) continue
|
|
47
|
+
seen.add(key)
|
|
48
|
+
targets.push({ file: key, workspaceRoot: ws.path, kind: 'inner' })
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
// workspace may be mid-edit — skip until next rescan
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return targets
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function appendWatchLog(line) {
|
|
59
|
+
try {
|
|
60
|
+
fs.appendFileSync(path.join(resolveXrystalHome(), 'watch.log'), `${new Date().toISOString()} ${line}\n`)
|
|
61
|
+
} catch {
|
|
62
|
+
// ignore log failures
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function runWatchDaemonMain() {
|
|
67
|
+
const pidFile = watchPidPath()
|
|
68
|
+
fs.mkdirSync(resolveXrystalHome(), { recursive: true })
|
|
69
|
+
fs.writeFileSync(pidFile, `${process.pid}\n`)
|
|
70
|
+
|
|
71
|
+
const active = new Map()
|
|
72
|
+
const debounceTimers = new Map()
|
|
73
|
+
let registryWatcher = null
|
|
74
|
+
let rescanTimer = null
|
|
75
|
+
|
|
76
|
+
const scheduleHeal = (workspaceRoot, reason) => {
|
|
77
|
+
const key = path.resolve(workspaceRoot)
|
|
78
|
+
if (debounceTimers.has(key)) {
|
|
79
|
+
clearTimeout(debounceTimers.get(key))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
debounceTimers.set(key, setTimeout(() => {
|
|
83
|
+
debounceTimers.delete(key)
|
|
84
|
+
try {
|
|
85
|
+
const result = runWorkspaceSelfHeal(workspaceRoot, {
|
|
86
|
+
ensureTmp: false,
|
|
87
|
+
healRegistry: true,
|
|
88
|
+
forceRegistry: true,
|
|
89
|
+
})
|
|
90
|
+
appendWatchLog(
|
|
91
|
+
`heal ${path.basename(workspaceRoot)} reason=${reason}`
|
|
92
|
+
+ ` dirs=${result.slotDirsCreated.join(',') || '-'}`
|
|
93
|
+
+ ` tmp=${result.tmpOptionsPatched ? 'patched' : 'ok'}`
|
|
94
|
+
+ ` registry=${result.registryChanged ? 'updated' : 'ok'}`,
|
|
95
|
+
)
|
|
96
|
+
} catch (error) {
|
|
97
|
+
appendWatchLog(`heal error ${workspaceRoot}: ${error.message}`)
|
|
98
|
+
}
|
|
99
|
+
rebuildWatchers()
|
|
100
|
+
}, DEBOUNCE_MS))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const rebuildWatchers = () => {
|
|
104
|
+
const registry = readRegistry()
|
|
105
|
+
const targets = collectWatchTargets(registry)
|
|
106
|
+
const nextPaths = new Set(targets.map((t) => t.file))
|
|
107
|
+
|
|
108
|
+
for (const [file] of active) {
|
|
109
|
+
if (!nextPaths.has(file)) {
|
|
110
|
+
unwatchFile(file)
|
|
111
|
+
active.delete(file)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (const target of targets) {
|
|
116
|
+
const interval = resolveWorkspaceWatchIntervalMs(target.workspaceRoot)
|
|
117
|
+
const existing = active.get(target.file)
|
|
118
|
+
if (existing && existing.interval === interval) continue
|
|
119
|
+
|
|
120
|
+
if (existing) {
|
|
121
|
+
unwatchFile(target.file)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
watchFile(target.file, { interval }, (curr, prev) => {
|
|
125
|
+
if (curr.mtimeMs !== prev.mtimeMs) {
|
|
126
|
+
scheduleHeal(target.workspaceRoot, target.kind)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
active.set(target.file, { interval, workspaceRoot: target.workspaceRoot, kind: target.kind })
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const registryPath = registryFilePath()
|
|
133
|
+
if (registryWatcher) {
|
|
134
|
+
unwatchFile(registryWatcher.path)
|
|
135
|
+
registryWatcher = null
|
|
136
|
+
}
|
|
137
|
+
if (fs.existsSync(registryPath)) {
|
|
138
|
+
watchFile(registryPath, { interval: defaultWatchIntervalMs() }, (curr, prev) => {
|
|
139
|
+
if (curr.mtimeMs !== prev.mtimeMs) rebuildWatchers()
|
|
140
|
+
})
|
|
141
|
+
registryWatcher = { path: registryPath }
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const shutdown = (signal) => {
|
|
146
|
+
appendWatchLog(`shutdown ${signal}`)
|
|
147
|
+
if (rescanTimer) clearInterval(rescanTimer)
|
|
148
|
+
for (const [file] of active) unwatchFile(file)
|
|
149
|
+
active.clear()
|
|
150
|
+
if (registryWatcher) unwatchFile(registryWatcher.path)
|
|
151
|
+
for (const timer of debounceTimers.values()) clearTimeout(timer)
|
|
152
|
+
debounceTimers.clear()
|
|
153
|
+
try { fs.unlinkSync(pidFile) } catch { /* ignore */ }
|
|
154
|
+
process.exit(0)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
process.on('SIGTERM', () => shutdown('SIGTERM'))
|
|
158
|
+
process.on('SIGINT', () => shutdown('SIGINT'))
|
|
159
|
+
|
|
160
|
+
appendWatchLog(`started pid=${process.pid}`)
|
|
161
|
+
rebuildWatchers()
|
|
162
|
+
|
|
163
|
+
// Safety rescan — picks up new inner tmp without file event on registry
|
|
164
|
+
rescanTimer = setInterval(() => {
|
|
165
|
+
rebuildWatchers()
|
|
166
|
+
}, REGISTRY_RESCAN_MS)
|
|
167
|
+
|
|
168
|
+
// Initial heal pass for all registered workspaces
|
|
169
|
+
for (const ws of readRegistry().workspaces ?? []) {
|
|
170
|
+
if (ws?.path && fs.existsSync(ws.path)) {
|
|
171
|
+
scheduleHeal(ws.path, 'boot')
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
|
+
|
|
6
|
+
import { resolveXrystalHome } from './xrystal-registry.mjs'
|
|
7
|
+
|
|
8
|
+
const HELPERS_DIR = path.dirname(fileURLToPath(import.meta.url))
|
|
9
|
+
|
|
10
|
+
export function watchDaemonScriptPath() {
|
|
11
|
+
return path.join(HELPERS_DIR, '../scripts/xrystal-watch-daemon.mjs')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function watchPidPath() {
|
|
15
|
+
return path.join(resolveXrystalHome(), 'watch.pid')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function watchLogPath() {
|
|
19
|
+
return path.join(resolveXrystalHome(), 'watch.log')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function watchDaemonStatus() {
|
|
23
|
+
const pidFile = watchPidPath()
|
|
24
|
+
if (!fs.existsSync(pidFile)) {
|
|
25
|
+
return { running: false, pid: null }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const pid = Number(fs.readFileSync(pidFile, 'utf8').trim())
|
|
29
|
+
if (!Number.isFinite(pid) || pid <= 0) {
|
|
30
|
+
try { fs.unlinkSync(pidFile) } catch { /* ignore */ }
|
|
31
|
+
return { running: false, pid: null, stale: true }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
process.kill(pid, 0)
|
|
36
|
+
return { running: true, pid }
|
|
37
|
+
} catch {
|
|
38
|
+
try { fs.unlinkSync(pidFile) } catch { /* ignore */ }
|
|
39
|
+
return { running: false, pid: null, stale: true }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function startWatchDaemon({ detached = true, quiet = true } = {}) {
|
|
44
|
+
const status = watchDaemonStatus()
|
|
45
|
+
if (status.running) {
|
|
46
|
+
return { ...status, started: false }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const script = watchDaemonScriptPath()
|
|
50
|
+
if (!fs.existsSync(script)) {
|
|
51
|
+
throw new Error(`watch daemon script missing: ${script}`)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const logFd = fs.openSync(watchLogPath(), 'a')
|
|
55
|
+
const child = spawn(process.execPath, [script], {
|
|
56
|
+
detached,
|
|
57
|
+
stdio: detached ? ['ignore', logFd, logFd] : 'inherit',
|
|
58
|
+
env: { ...process.env, XRY_WATCH_DAEMON: '1' },
|
|
59
|
+
})
|
|
60
|
+
fs.closeSync(logFd)
|
|
61
|
+
|
|
62
|
+
if (detached) {
|
|
63
|
+
child.unref()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return { running: true, pid: child.pid, started: true, quiet }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function stopWatchDaemon() {
|
|
70
|
+
const status = watchDaemonStatus()
|
|
71
|
+
if (!status.running || !status.pid) {
|
|
72
|
+
return { stopped: false, ...status }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
process.kill(status.pid, 'SIGTERM')
|
|
77
|
+
} catch {
|
|
78
|
+
try { fs.unlinkSync(watchPidPath()) } catch { /* ignore */ }
|
|
79
|
+
return { stopped: false, running: false, pid: null }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return { stopped: true, pid: status.pid }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Start background ~/.xrystal tmp watcher when not already running. */
|
|
86
|
+
export function ensureWatchDaemon({ quiet = true } = {}) {
|
|
87
|
+
try {
|
|
88
|
+
return startWatchDaemon({ detached: true, quiet })
|
|
89
|
+
} catch {
|
|
90
|
+
return { running: false, started: false, quiet }
|
|
91
|
+
}
|
|
92
|
+
}
|
package/x/bin/main-cli.js
CHANGED
|
@@ -13,6 +13,7 @@ import { registerProjectsCommand, registerWorkspacesCommand } from './commands/p
|
|
|
13
13
|
import { registerTagCommand, registerVersionCommand } from './commands/release.mjs'
|
|
14
14
|
import { registerTelepresenceCommand } from './commands/telepresence.mjs'
|
|
15
15
|
import { registerHooksCommand } from './commands/hooks.mjs'
|
|
16
|
+
import { registerWatchCommand } from './commands/watch.mjs'
|
|
16
17
|
import { printBanner, shouldShowBanner } from './helpers/banner.mjs'
|
|
17
18
|
import { formatRootHelp, formatCreateCommandHelp, isCreateHelpRequest, isRootHelpRequest, printQuickStartGuide } from './helpers/cli-help.mjs'
|
|
18
19
|
import {
|
|
@@ -79,6 +80,7 @@ registerProjectInfoCommand(cli, cliContext)
|
|
|
79
80
|
registerSyncCommand(cli, cliContext)
|
|
80
81
|
registerWorkspacesCommand(cli, cliContext)
|
|
81
82
|
registerProjectsCommand(cli, cliContext)
|
|
83
|
+
registerWatchCommand(cli, cliContext)
|
|
82
84
|
registerHooksCommand(cli, cliContext)
|
|
83
85
|
registerTelepresenceCommand(cli, cliContext)
|
|
84
86
|
registerProjectCommands(cli, cliContext)
|