archivo del paquete→ .shtukbase/packages/blueprint-events/actions/sources.ts
actions/sources.ts
33 líneas, 1.8 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // On install: one page in /event-sources for every address in the start answer `events.sources`.
- // A page that is already there is left as it is. Run from the project root.
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- const root = process.cwd();
- const pkgs = join(root, 'base', 'packages.md');
- const m = existsSync(pkgs) ? /^---\n([\s\S]*?)\n---/.exec(readFileSync(pkgs, 'utf8')) : null;
- const facts: Record<string, unknown> = (m ? (Bun.YAML.parse(m[1]) as any)?.facts : null) ?? {};
- const raw = facts['events.sources'] ?? (facts as any).events?.sources;
- const urls = (Array.isArray(raw) ? raw : raw == null ? [] : String(raw).split(',')).map((u) => String(u).trim()).filter(Boolean);
- const dir = join(root, 'event-sources');
- mkdirSync(dir, { recursive: true });
- const q = (s: string) => `"${s.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
- let made = 0;
- for (const u of urls) {
- let url: URL;
- try {
- url = new URL(/^\w+:\/\//.test(u) ? u : `https://${u}`);
- } catch {
- console.log(` skipped "${u}": this is not an address`);
- continue;
- }
- const host = url.hostname.replace(/^www\./, '');
- const slug = `${host}${url.pathname}`.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60) || 'source';
- const file = join(dir, `${slug}.md`);
- if (existsSync(file)) continue;
- const title = url.pathname.length > 1 ? `${host}${url.pathname.replace(/\/+$/, '')}` : host;
- writeFileSync(file, `---\ntype: EventSource\ntitle: ${q(title)}\nurl: ${q(url.href)}\nkind: aggregator\nuse: active\n---\n\nA source from the start question. Fix the kind (\`kind\`): aggregator is a listing, venue is a venue, social is social networks.\n`);
- made++;
- }
- console.log(`blueprint-events: sources created — ${made}`);