Glimmer / app / native / Checks / make-catalog.mjs
| 1 | import { readFile, writeFile } from "node:fs/promises"; |
| 2 | import * as source from "../../src/data.ts"; |
| 3 | |
| 4 | const seed = await readFile(new URL("../../supabase/seed_lessons.sql", import.meta.url), "utf8"); |
| 5 | const fields = "'((?:[^']|'')*)'"; |
| 6 | const pattern = new RegExp( |
| 7 | `\\(${fields}, (\\d+), ${fields}, ${fields}, ${fields}, (\\d+), ${fields}, ${fields}, ${fields}, \\$pg\\$(.*?)\\$pg\\$::jsonb, \\d+\\)`, "gs", |
| 8 | ); |
| 9 | const unquote = (value) => value.replaceAll("''", "'"); |
| 10 | const lessons = [...seed.matchAll(pattern)].map(([, slug, number, arc, title, tagline, minutes, , hero, heroAlt, pages]) => ({ |
| 11 | slug, number: Number(number), arc: unquote(arc), title: unquote(title), tagline: unquote(tagline), |
| 12 | minutes: Number(minutes), hero, heroAlt: unquote(heroAlt), pages: JSON.parse(pages), |
| 13 | })); |
| 14 | if (lessons.length !== 90 || lessons.some((item, index) => item.number !== index + 1)) { |
| 15 | throw new Error("The original lesson catalog did not parse in full."); |
| 16 | } |
| 17 | // Keep native editorial changes reproducible without changing the original Expo curriculum. |
| 18 | const editorial = JSON.parse(await readFile(new URL("../LibraryContent/Editorial.json", import.meta.url), "utf8")); |
| 19 | for (const ids of [editorial.practiceTopics.map(topic => topic.id), editorial.practiceTopics.flatMap(topic => topic.exercises.map(practice => practice.id))]) { |
| 20 | if (ids.some(id => typeof id !== "string" || !/^[a-z][a-z0-9_-]*$/.test(id)) || new Set(ids).size !== ids.length) { |
| 21 | throw new Error("Coaching topics and practices each require unique, permanent identifiers."); |
| 22 | } |
| 23 | } |
| 24 | for (const [number, update] of Object.entries(editorial.takeaways)) { |
| 25 | const lesson = lessons.find(item => item.number === Number(number)); |
| 26 | const page = lesson?.pages.find(item => item.kind === "takeaway"); |
| 27 | if (!page || !update.body || !update.tryToday) throw new Error(`Invalid editorial takeaway for lesson ${number}`); |
| 28 | page.body = `${update.body}\n\n**Try today:** ${update.tryToday}`; |
| 29 | page.tryToday = update.tryToday; |
| 30 | } |
| 31 | // Deeper rewrites of existing lessons. IDs, numbers, page kinds and option counts stay |
| 32 | // fixed because journals store answers by page position. |
| 33 | for (const [number, edit] of Object.entries(editorial.lessonEdits ?? {})) { |
| 34 | const lesson = lessons.find(item => item.number === Number(number)); |
| 35 | if (!lesson) throw new Error(`Lesson edit for missing lesson ${number}`); |
| 36 | if (edit.pages) { |
| 37 | if (edit.pages.length !== lesson.pages.length) throw new Error(`Lesson ${number} must keep ${lesson.pages.length} pages`); |
| 38 | edit.pages.forEach((page, index) => { |
| 39 | const original = lesson.pages[index]; |
| 40 | if (page.kind !== original.kind || (original.options?.length ?? 0) !== (page.options?.length ?? 0)) { |
| 41 | throw new Error(`Lesson ${number} page ${index + 1} must keep its kind and option count`); |
| 42 | } |
| 43 | if (page.kind === "takeaway" && !page.tryToday) throw new Error(`Lesson ${number} takeaway needs tryToday`); |
| 44 | }); |
| 45 | } |
| 46 | Object.assign(lesson, Object.fromEntries(["arc", "title", "tagline", "minutes", "pages"].filter(key => edit[key] !== undefined).map(key => [key, edit[key]]))); |
| 47 | for (const page of lesson.pages) { |
| 48 | if (page.kind === "takeaway" && page.tryToday && !page.body.includes("**Try today:**")) page.body = `${page.body}\n\n**Try today:** ${page.tryToday}`; |
| 49 | } |
| 50 | } |
| 51 | // The Deep Path: lessons appended after the original 90. |
| 52 | for (const extra of editorial.extraLessons ?? []) { |
| 53 | if (extra.number !== lessons.length + 1 || lessons.some(item => item.slug === extra.slug)) throw new Error(`Extra lesson ${extra.slug} must be numbered ${lessons.length + 1} with a new slug`); |
| 54 | if (!extra.title || !extra.arc || !extra.tagline || !(extra.minutes > 0) || !Array.isArray(extra.pages) || extra.pages.at(-1)?.kind !== "takeaway") { |
| 55 | throw new Error(`Extra lesson ${extra.number} is incomplete`); |
| 56 | } |
| 57 | const pages = extra.pages.map(page => page.kind === "takeaway" && page.tryToday && !page.body.includes("**Try today:**") |
| 58 | ? { ...page, body: `${page.body}\n\n**Try today:** ${page.tryToday}` } : page); |
| 59 | lessons.push({ slug: extra.slug, number: extra.number, arc: extra.arc, title: extra.title, tagline: extra.tagline, |
| 60 | minutes: extra.minutes, hero: null, heroAlt: null, pages }); |
| 61 | } |
| 62 | const levels = new Set([1, 2, 3, 4, 5]); |
| 63 | for (const practice of editorial.practiceTopics.flatMap(topic => topic.exercises)) { |
| 64 | if (practice.level !== undefined && !levels.has(practice.level)) throw new Error(`Practice ${practice.id} has an invalid level`); |
| 65 | } |
| 66 | const challengeLevels = editorial.challengeLevels ?? {}; |
| 67 | const challenges = [...source.CHALLENGES.map(item => challengeLevels[item.id] ? { ...item, level: challengeLevels[item.id] } : item), |
| 68 | ...(editorial.challenges ?? [])]; |
| 69 | if (new Set(challenges.map(item => item.id)).size !== challenges.length |
| 70 | || challenges.some(item => item.level !== undefined && !levels.has(item.level)) |
| 71 | || challenges.some(item => !item.title || !item.sub || !(item.mins > 0) || !(item.coins > 0) || item.teach?.length < 1 || !item.q || item.opts?.length < 2 || !item.action || !item.done)) { |
| 72 | throw new Error("Challenges need unique IDs, a valid level and every page"); |
| 73 | } |
| 74 | // Apps added for the native app, placed after a named original source. |
| 75 | const sources = source.SOURCES.map(({ name }) => name); |
| 76 | const tags = Object.fromEntries(source.SOURCES.map(({ name }) => [name, source.contentTypesFor(name)])); |
| 77 | for (const added of editorial.sources ?? []) { |
| 78 | if (!added.name || !Array.isArray(added.tags) || sources.includes(added.name)) throw new Error(`Invalid editorial source ${added.name}`); |
| 79 | const position = sources.indexOf(added.after); |
| 80 | sources.splice(position < 0 ? sources.length : position + 1, 0, added.name); |
| 81 | tags[added.name] = added.tags; |
| 82 | } |
| 83 | const catalog = { |
| 84 | sources, |
| 85 | activities: [...source.ACT_POOL, ...(editorial.activities ?? [])], |
| 86 | people: source.PPL_POOL, |
| 87 | tags, |
| 88 | defaultTags: source.contentTypesFor(""), |
| 89 | initialTasks: source.INITIAL_TASKS, |
| 90 | library: source.TASK_LIBRARY, |
| 91 | preload: source.PRELOAD_SUGGESTIONS, |
| 92 | onboarding: source.ONB_STEPS.map(step => step.key === "primary_goal" ? { |
| 93 | ...step, mode: "multi", kicker: "Your goals", title: "What would you like help with?", sub: "Choose all that apply.", |
| 94 | } : step), |
| 95 | challenges, |
| 96 | energy: source.ENERGY_DEFS, |
| 97 | energyMessages: source.ENERGY_MSGS, |
| 98 | habits: source.HABIT_DATA, |
| 99 | cleaning: source.CLEAN_DATA, |
| 100 | seasonal: editorial.seasonal, |
| 101 | quotes: editorial.quotes, |
| 102 | coach: editorial.coach, |
| 103 | startingPractices: editorial.startingPractices, |
| 104 | practiceTopics: editorial.practiceTopics, |
| 105 | praise: source.PRAISE, |
| 106 | lessons, |
| 107 | }; |
| 108 | await writeFile(new URL("../LibraryContent/Catalog.json", import.meta.url), JSON.stringify(catalog, null, 2) + "\n"); |