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