aboutsummaryrefslogtreecommitdiff
path: root/scripts/merge_packs.js
blob: 01bf4124c3d099aab88066698df6f29781250264 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env node
/*
  Merge all JSON packs from data/packs/*.json into texts.json at repo root.
  Each pack item: { category: string, content: string, attribution: string }
*/
const fs = require('fs');
const path = require('path');
const { glob } = require('glob');

const ROOT = path.resolve(__dirname, '..');
const PACKS_DIR = path.join(ROOT, 'data', 'packs');
const OUTPUT = path.join(ROOT, 'texts.json');

(async () => {
  try {
    const files = await glob('*.json', { cwd: PACKS_DIR, absolute: true });
    const items = [];
    const seen = new Set();

    for (const file of files) {
      const pack = JSON.parse(fs.readFileSync(file, 'utf8'));
      for (const item of pack) {
        if (!item || !item.content) continue;
        const key = `${item.category || ''}\u0000${item.content.trim().toLowerCase()}`;
        if (seen.has(key)) continue;
        seen.add(key);
        items.push({
          category: String(item.category || 'general'),
          content: String(item.content),
          attribution: String(item.attribution || path.relative(ROOT, file)),
        });
      }
    }

    // Shuffle
    for (let i = items.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [items[i], items[j]] = [items[j], items[i]];
    }

    fs.writeFileSync(OUTPUT, JSON.stringify(items, null, 2));
    console.log(`Merged ${items.length} items into ${OUTPUT}`);
  } catch (err) {
    console.error('merge_packs failed:', err);
    process.exit(1);
  }
})();