/** * Loads a demo into the site and publishes it. * * npm run demo -- --template heritage|bold|community|clean [--force] the Acacia Hill church (development) * npm run demo -- --profile [--force] one of the eight demo churches (1.3.0), * keeping the church's profile and settings * npm run demo -- --list the profiles * * It replaces the site's content, so a site that has been published needs --force. On a server, * `crosscode demo website --profile …` runs it (backup first), for the Hub's demo sites. */ import 'dotenv/config'; import { PrismaPg } from '@prisma/adapter-pg'; import { agent, websiteTemplateSchema } from '@crosscode/contracts'; import { PrismaClient } from '../src/generated/prisma/client.js'; import { createCtx } from '../src/context.js'; import { loadDemo } from '../src/demo/demoChurch.js'; import { demoSeeds } from '../src/demo/seeds.js'; import { loadConfig } from '../src/lib/config.js'; const arg = (name: string) => { const i = process.argv.indexOf(`--${name}`); return i > 0 ? process.argv[i + 1] : undefined; }; const usage = 'Usage: npm run demo -- --template heritage|bold|community|clean | --profile [--force] | --list'; if (process.argv.includes('--list')) { for (const s of demoSeeds()) console.log(`${s.id.padEnd(18)} ${s.label}`); process.exit(0); } const profileArg = arg('profile'); const what = profileArg !== undefined ? agent.websiteDemoProfileSchema.safeParse(profileArg) : websiteTemplateSchema.safeParse(arg('template') ?? 'heritage'); if (!what.success) { console.error(profileArg !== undefined ? `Unknown profile "${profileArg}" (npm run demo -- --list).` : usage); process.exit(2); } const db = new PrismaClient({ adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL! }) }); const ctx = createCtx(loadConfig(), db); const published = await db.siteVersion.findFirst({ where: { NOT: { publishedBy: 'demo' } } }); if (published && !process.argv.includes('--force')) { console.error('This site has been published by its admins: loading the demo would replace their content. Add --force to do it anyway.'); await db.$disconnect(); process.exit(1); } const version = await loadDemo(ctx, what.data); console.log(`Demo ${what.data} loaded and published (version ${version.number}).`); await db.$disconnect();