summaryrefslogtreecommitdiffstats
path: root/assets/js/pkg-list.js
blob: 953c4ec0b23d5cc1b665c9f4c459ee0f6f841877 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
document.addEventListener('alpine:init', () => {
  Alpine.data('pkgList', (i18n) => ({
    state: 'loading', // 'loading' | 'loaded' | 'error'
    i18n: i18n,
    packages: [],
    filter: '',

    get filtered() {
      if (!this.filter) return this.packages;
      const q = this.filter.toLowerCase();
      return this.packages.filter(p => p.name.toLowerCase().includes(q));
    },

    async init() {
      try {
        const res = await fetch('https://packages.danix.xyz/PACKAGES.TXT');
        if (!res.ok) throw new Error('HTTP ' + res.status);
        const text = await res.text();
        this.packages = parsePkgTxt(text);
        this.state = 'loaded';
      } catch (e) {
        console.error('pkg-list fetch error:', e);
        this.state = 'error';
      }
    }
  }));
});

function parsePkgTxt(text) {
  const packages = [];
  const nameRe = /^PACKAGE NAME:\s+(.+\.txz)\s*$/m;
  const locRe  = /^PACKAGE LOCATION:\s+(.+)\s*$/m;

  // Split on blank lines between blocks
  const blocks = text.split(/\n{2,}/);

  for (const block of blocks) {
    const nameMatch = block.match(nameRe);
    const locMatch  = block.match(locRe);
    if (!nameMatch || !locMatch) continue;

    const filename = nameMatch[1].trim();
    const location = locMatch[1].trim(); // e.g. ./desktop/waybar

    // Parse name and version from filename: name-version-arch-build_tag.txz
    // Strategy: version segment starts with a digit after a hyphen
    const parts = filename.replace(/\.txz$/, '').split('-');
    let nameEnd = 1;
    for (let i = 1; i < parts.length; i++) {
      if (/^\d/.test(parts[i])) { nameEnd = i; break; }
    }
    const name    = parts.slice(0, nameEnd).join('-');
    const version = parts[nameEnd] || '';

    // Derive folder URL: ./desktop/waybar -> https://packages.danix.xyz/desktop/waybar/
    const folder = location.replace(/^\.\//, '');
    const url    = 'https://packages.danix.xyz/' + folder + '/';
    const label  = folder; // e.g. desktop/waybar

    packages.push({ name, version, url, label });
  }

  // Sort alphabetically by name
  packages.sort((a, b) => a.name.localeCompare(b.name));
  return packages;
}