change cron discovery to use regex

This commit is contained in:
Chris Wanstrath 2026-02-09 16:23:32 -08:00
parent a1aa37297f
commit 4d3083764a

View File

@ -1,10 +1,12 @@
import { readdir } from 'fs/promises' import { readdir, readFile } from 'fs/promises'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { join } from 'path' import { join } from 'path'
import { isValidSchedule, toCronExpr, type CronJob, type Schedule } from './schedules' import { isValidSchedule, toCronExpr, type CronJob, type Schedule } from './schedules'
const APPS_DIR = process.env.APPS_DIR! const APPS_DIR = process.env.APPS_DIR!
const SCHEDULE_RE = /export\s+const\s+schedule\s*=\s*['"]([^'"]+)['"]/
export async function getApps(): Promise<string[]> { export async function getApps(): Promise<string[]> {
const entries = await readdir(APPS_DIR, { withFileTypes: true }) const entries = await readdir(APPS_DIR, { withFileTypes: true })
const apps: string[] = [] const apps: string[] = []
@ -38,8 +40,15 @@ export async function discoverCronJobs(): Promise<CronJob[]> {
const name = file.replace(/\.ts$/, '') const name = file.replace(/\.ts$/, '')
try { try {
const mod = await import(filePath) const source = await readFile(filePath, 'utf-8')
const schedule = mod.schedule as Schedule const match = source.match(SCHEDULE_RE)
if (!match) {
console.error(`No schedule export found in ${filePath}`)
continue
}
const schedule = match[1] as Schedule
if (!isValidSchedule(schedule)) { if (!isValidSchedule(schedule)) {
console.error(`Invalid schedule in ${filePath}: ${schedule}`) console.error(`Invalid schedule in ${filePath}: ${schedule}`)