ShtukbaseStandard
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.

  1. // On install: one page in /event-sources for every address in the start answer `events.sources`.
  2. // A page that is already there is left as it is. Run from the project root.
  3. import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. const root = process.cwd();
  6. const pkgs = join(root, 'base', 'packages.md');
  7. const m = existsSync(pkgs) ? /^---\n([\s\S]*?)\n---/.exec(readFileSync(pkgs, 'utf8')) : null;
  8. const facts: Record<string, unknown> = (m ? (Bun.YAML.parse(m[1]) as any)?.facts : null) ?? {};
  9. const raw = facts['events.sources'] ?? (facts as any).events?.sources;
  10. const urls = (Array.isArray(raw) ? raw : raw == null ? [] : String(raw).split(',')).map((u) => String(u).trim()).filter(Boolean);
  11. const dir = join(root, 'event-sources');
  12. mkdirSync(dir, { recursive: true });
  13. const q = (s: string) => `"${s.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
  14. let made = 0;
  15. for (const u of urls) {
  16. let url: URL;
  17. try {
  18. url = new URL(/^\w+:\/\//.test(u) ? u : `https://${u}`);
  19. } catch {
  20. console.log(` skipped "${u}": this is not an address`);
  21. continue;
  22. }
  23. const host = url.hostname.replace(/^www\./, '');
  24. const slug = `${host}${url.pathname}`.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60) || 'source';
  25. const file = join(dir, `${slug}.md`);
  26. if (existsSync(file)) continue;
  27. const title = url.pathname.length > 1 ? `${host}${url.pathname.replace(/\/+$/, '')}` : host;
  28. 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`);
  29. made++;
  30. }
  31. console.log(`blueprint-events: sources created — ${made}`);