(agentDir: string)
| 31 | * to read/parse, and records missing the required cron fields. |
| 32 | */ |
| 33 | export async function listCronTasks(agentDir: string): Promise<CronTask[]> { |
| 34 | const dir = cronDirOf(agentDir); |
| 35 | let entries: import('node:fs').Dirent[]; |
| 36 | try { |
| 37 | entries = await readdir(dir, { withFileTypes: true }); |
| 38 | } catch { |
| 39 | return []; |
| 40 | } |
| 41 | const out: CronTask[] = []; |
| 42 | for (const entry of entries) { |
| 43 | if (!entry.isFile() || !entry.name.endsWith('.json')) continue; |
| 44 | const id = entry.name.slice(0, -'.json'.length); |
| 45 | if (!VALID_CRON_ID.test(id)) continue; |
| 46 | let parsed: unknown; |
| 47 | try { |
| 48 | parsed = JSON.parse(await readFile(join(dir, entry.name), 'utf8')); |
| 49 | } catch { |
| 50 | continue; |
| 51 | } |
| 52 | if (isCronTask(parsed)) out.push(parsed); |
| 53 | } |
| 54 | out.sort((a, b) => a.createdAt - b.createdAt); |
| 55 | return out; |
| 56 | } |
| 57 | |
| 58 | function isCronTask(value: unknown): value is CronTask { |
| 59 | if (typeof value !== 'object' || value === null) return false; |
no test coverage detected