tweak logging

This commit is contained in:
Chris Wanstrath 2026-01-29 13:20:29 -08:00
parent abe7bc258e
commit 776b77c14f

View File

@ -55,7 +55,7 @@ export function stopApp(dir: string) {
const app = _apps.get(dir)
if (!app || app.state !== 'running') return
info(dir, 'Stopping...')
info(app, 'Stopping...')
app.state = 'stopping'
update()
app.proc?.kill()
@ -70,20 +70,16 @@ export function updateAppIcon(dir: string, icon: string) {
saveApp(dir, pkg)
}
const err = (app: string, ...msg: string[]) =>
console.error('🐾', `${app}:`, ...msg)
const getPort = () => NEXT_PORT++
const info = (app: string, ...msg: string[]) =>
console.log('🐾', `${app}:`, ...msg)
const info = (app: App, ...msg: string[]) => {
console.log('🐾', `[${app.name}]`, ...msg)
app.logs?.push({ time: Date.now(), text: msg.join(' ') })
}
const isApp = (dir: string): boolean =>
!loadApp(dir).error
const log = (app: string, ...msg: string[]) =>
console.log(`<${app}>`, ...msg)
const update = () => _listeners.forEach(cb => cb())
function allAppDirs() {
@ -120,19 +116,14 @@ function loadApp(dir: string): LoadResult {
if (json.scripts?.toes) {
return { pkg: json }
} else {
const error = 'Missing scripts.toes in package.json'
err(dir, error)
return { pkg: json, error }
return { pkg: json, error: 'Missing scripts.toes in package.json' }
}
} catch (e) {
const error = `Invalid JSON in package.json: ${e instanceof Error ? e.message : String(e)}`
err(dir, error)
return { pkg: {}, error }
}
} catch (e) {
const error = 'Missing package.json'
err(dir, error)
return { pkg: {}, error }
return { pkg: {}, error: 'Missing package.json' }
}
}
@ -152,11 +143,11 @@ async function runApp(dir: string, port: number) {
const cwd = join(APPS_DIR, dir)
const needsInstall = !existsSync(join(cwd, 'node_modules'))
if (needsInstall) info(dir, 'Installing dependencies...')
if (needsInstall) info(app, 'Installing dependencies...')
const install = Bun.spawn(['bun', 'install'], { cwd, stdout: 'pipe', stderr: 'pipe' })
await install.exited
info(dir, `Starting on port ${port}...`)
info(app, `Starting on port ${port}...`)
const proc = Bun.spawn(['bun', 'run', 'toes'], {
cwd,
@ -181,9 +172,8 @@ async function runApp(dir: string, port: number) {
const chunk = decoder.decode(value)
const lines = chunk.split('\n').map(l => l.trimEnd()).filter(Boolean)
for (const text of lines) {
log(dir, text)
const line: LogLine = { time: Date.now(), text }
app.logs = [...(app.logs ?? []).slice(-(MAX_LOGS - 1)), line]
info(app, text)
app.logs = (app.logs ?? []).slice(-MAX_LOGS)
}
if (lines.length) update()
}
@ -195,9 +185,9 @@ async function runApp(dir: string, port: number) {
// Handle process exit
proc.exited.then(code => {
if (code !== 0)
err(dir, `Exited with code ${code}`)
app.logs?.push({ time: Date.now(), text: `Exited with code ${code}` })
else
info(dir, 'Stopped')
app.logs?.push({ time: Date.now(), text: 'Stopped' })
// Reset to stopped state (or invalid if no longer valid)
app.state = isApp(dir) ? 'stopped' : 'invalid'