From 7c04aceef9c655b6fd906c1a2955ce593dad16d0 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Mon, 9 Feb 2026 21:17:02 -0800 Subject: [PATCH] [cron] setup app env properly when running tasks --- apps/cron/20260201-000000/lib/executor.ts | 3 ++- src/server/apps.ts | 31 ++--------------------- src/tools/env.ts | 30 ++++++++++++++++++++++ src/tools/index.ts | 1 + 4 files changed, 35 insertions(+), 30 deletions(-) create mode 100644 src/tools/env.ts diff --git a/apps/cron/20260201-000000/lib/executor.ts b/apps/cron/20260201-000000/lib/executor.ts index ab86598..fc18c35 100644 --- a/apps/cron/20260201-000000/lib/executor.ts +++ b/apps/cron/20260201-000000/lib/executor.ts @@ -1,4 +1,5 @@ import { join } from 'path' +import { loadAppEnv } from '@because/toes/tools' import type { CronJob } from './schedules' import { getNextRun } from './scheduler' @@ -32,7 +33,7 @@ export async function executeJob(job: CronJob, onUpdate: () => void): Promise { - const envDir = join(TOES_DIR, 'env') - const env: Record = {} - - const parseEnvFile = (path: string) => { - if (!existsSync(path)) return - const content = readFileSync(path, 'utf-8') - for (const line of content.split('\n')) { - const trimmed = line.trim() - if (!trimmed || trimmed.startsWith('#')) continue - const eqIndex = trimmed.indexOf('=') - if (eqIndex === -1) continue - const key = trimmed.slice(0, eqIndex).trim() - let value = trimmed.slice(eqIndex + 1).trim() - // Remove surrounding quotes - if ((value.startsWith('"') && value.endsWith('"')) || - (value.startsWith("'") && value.endsWith("'"))) { - value = value.slice(1, -1) - } - if (key) env[key] = value - } - } - - parseEnvFile(join(envDir, '_global.env')) - parseEnvFile(join(envDir, `${appName}.env`)) - - return env -} function maybeResetBackoff(app: App) { if (app.started && Date.now() - app.started >= STABLE_RUN_TIME) { @@ -599,7 +572,7 @@ async function runApp(dir: string, port: number) { info(app, `Starting on port ${port}...`) // Load env vars from TOES_DIR/env/ - const appEnv = loadAppEnv(dir) + const appEnv = loadAppEnv(dir, TOES_DIR) const proc = Bun.spawn(['bun', 'run', 'toes'], { cwd, diff --git a/src/tools/env.ts b/src/tools/env.ts new file mode 100644 index 0000000..e5a57f3 --- /dev/null +++ b/src/tools/env.ts @@ -0,0 +1,30 @@ +import { existsSync, readFileSync } from 'fs' +import { join } from 'path' + +export function loadAppEnv(appName: string, toesDir?: string): Record { + const envDir = join(toesDir || process.env.TOES_DIR!, 'env') + const env: Record = {} + + const parseEnvFile = (path: string) => { + if (!existsSync(path)) return + const content = readFileSync(path, 'utf-8') + for (const line of content.split('\n')) { + const trimmed = line.trim() + if (!trimmed || trimmed.startsWith('#')) continue + const eqIndex = trimmed.indexOf('=') + if (eqIndex === -1) continue + const key = trimmed.slice(0, eqIndex).trim() + let value = trimmed.slice(eqIndex + 1).trim() + if ((value.startsWith('"') && value.endsWith('"')) || + (value.startsWith("'") && value.endsWith("'"))) { + value = value.slice(1, -1) + } + if (key) env[key] = value + } + } + + parseEnvFile(join(envDir, '_global.env')) + parseEnvFile(join(envDir, `${appName}.env`)) + + return env +} diff --git a/src/tools/index.ts b/src/tools/index.ts index 562c952..3057967 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -1,2 +1,3 @@ export { theme } from '../client/themes' +export { loadAppEnv } from './env' export { baseStyles, ToolScript } from './scripts.tsx'