Files
crosscode-platform/apps/website/backend/scripts/demo.ts
T
joshuaandClaude Opus 5.5 c8e270dbf1 website 1.8.0: demo scenarios (the scenario's people lead, Events' events through the feed)
- `npm run demo -- --profile <p> --scenario <p>`: each profile's `scenarioTeam` names four
  parts, roles and portraits; the names are the scenario's people from @crosscode/demo, the
  pastor first (home page and messages' speaker).
- With a linked Cross Events, a scenario load makes no events of its own, turns on Show events
  from and fetches Events' whole feed at once; an unreachable Events doesn't fail the load.
- A scenario on a site with no church profile saves the scenario's church, not Acacia Hill.
- crosscode script: `crosscode demo <church> website --scenario` passes it on and refuses a
  Website before 1.8.0; the Website installs and builds packages/demo.
- Docs: HANDOVER, APP-1.0-PLAN, VERSIONS, CHANGELOGs, What's New.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 15:23:12 +02:00

67 lines
3.7 KiB
TypeScript

/**
* 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 <profile> [--force] one of the eight demo churches (1.3.0),
* keeping the church's profile and settings
* npm run demo -- --profile <profile> --scenario <profile> [--force] 1.8.0: as part of that demo scenario
* (its people lead; with Cross Events
* linked, Events' events through the feed)
* 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 <church> 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 <profile> [--scenario <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);
}
// A demo scenario loads the profile of the same name (contracts agent.DEMO_SCENARIOS).
const scenarioArg = arg('scenario');
const scenario = scenarioArg !== undefined ? agent.demoScenarioSchema.safeParse(scenarioArg) : null;
if (scenario && !scenario.success) {
console.error(`Unknown scenario "${scenarioArg}" (npm run demo -- --list).`);
process.exit(2);
}
if (scenario && scenario.data !== profileArg) {
console.error(`The ${scenario.data} scenario loads the ${scenario.data} profile: --profile ${scenario.data} --scenario ${scenario.data}.`);
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, { scenario: scenario?.data });
console.log(`Demo ${what.data}${scenario ? ' (demo scenario)' : ''} loaded and published (version ${version.number}).`);
const sync = version.eventsSync;
if (sync?.ok) console.log(`Showing Cross Events' events: ${sync.imported} fetched.`);
else if (sync) console.warn(`Cross Events' events couldn't be fetched yet (${sync.error}); the next sync, within 15 minutes, brings them.`);
await db.$disconnect();