summaryrefslogtreecommitdiff
path: root/src/services/updates.ts
blob: b58609bb86e477d6accf1b49c8e060e5d277c276 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { execAsync, GLib, GObject, property, readFileAsync, register, writeFileAsync } from "astal";
import { updates as config } from "config";

export interface Update {
    full: string;
    name: string;
    description: string;
    url: string;
    version: {
        old: string;
        new: string;
    };
}

export interface Repo {
    repo?: string[];
    updates: Update[];
    icon: string;
    name: string;
}

export interface Data {
    cached?: boolean;
    repos: Repo[];
    errors: string[];
    news: string;
}

@register({ GTypeName: "Updates" })
export default class Updates extends GObject.Object {
    static instance: Updates;
    static get_default() {
        if (!this.instance) this.instance = new Updates();

        return this.instance;
    }

    readonly #cachePath = `${CACHE}/updates.txt`;

    #timeout?: GLib.Source;
    #loading = false;
    #data: Data = { cached: true, repos: [], errors: [], news: "" };

    @property(Boolean)
    get loading() {
        return this.#loading;
    }

    @property(Object)
    get updateData() {
        return this.#data;
    }

    @property(Object)
    get list() {
        return this.#data.repos.map(r => r.updates).flat();
    }

    @property(Number)
    get numUpdates() {
        return this.#data.repos.reduce((acc, repo) => acc + repo.updates.length, 0);
    }

    @property(String)
    get news() {
        return this.#data.news;
    }

    async #updateFromCache() {
        this.#data = JSON.parse(await readFileAsync(this.#cachePath));
        this.notify("update-data");
        this.notify("list");
        this.notify("num-updates");
        this.notify("news");
    }

    async getRepo(repo: string) {
        return (await execAsync(`bash -c "comm -12 <(pacman -Qq | sort) <(pacman -Slq '${repo}' | sort)"`)).split("\n");
    }

    async constructUpdate(update: string) {
        const info = await execAsync(`pacman -Qi ${update.split(" ")[0]}`);
        return info.split("\n").reduce(
            (acc, line) => {
                let [key, value] = line.split(" : ");
                key = key.trim().toLowerCase();
                if (key === "name" || key === "description" || key === "url") acc[key] = value.trim();
                else if (key === "version") acc.version.old = value.trim();
                return acc;
            },
            { version: { new: update.split("->")[1].trim() } } as Update
        );
    }

    getRepoIcon(repo: string) {
        switch (repo) {
            case "core":
                return "hub";
            case "extra":
                return "add_circle";
            case "multilib":
                return "account_tree";
            default:
                return "deployed_code_update";
        }
    }

    getUpdates() {
        // Return if already getting updates
        if (this.#loading) return;

        this.#loading = true;
        this.notify("loading");

        // Get new updates
        Promise.allSettled([execAsync("checkupdates"), execAsync("yay -Qua"), execAsync("yay -Pw")])
            .then(async ([pacman, yay, news]) => {
                const data: Data = { repos: [], errors: [], news: news.status === "fulfilled" ? news.value : "" };

                // Pacman updates (checkupdates)
                if (pacman.status === "fulfilled") {
                    const repos: Repo[] = await Promise.all(
                        (await execAsync("pacman-conf -l")).split("\n").map(async r => ({
                            repo: await this.getRepo(r),
                            updates: [],
                            icon: this.getRepoIcon(r),
                            name: r[0].toUpperCase() + r.slice(1) + " repository",
                        }))
                    );

                    for (const update of pacman.value.split("\n")) {
                        const pkg = update.split(" ")[0];
                        for (const repo of repos)
                            if (repo.repo?.includes(pkg)) repo.updates.push(await this.constructUpdate(update));
                    }

                    for (const repo of repos) if (repo.updates.length > 0) data.repos.push(repo);
                }

                // AUR and devel updates (yay -Qua)
                if (yay.status === "fulfilled") {
                    const aur: Repo = { updates: [], icon: "deployed_code_account", name: "AUR" };

                    for (const update of yay.value.split("\n")) {
                        if (/^\s*->/.test(update)) data.errors.push(update); // Error
                        else aur.updates.push(await this.constructUpdate(update));
                    }

                    if (aur.updates.length > 0) data.repos.push(aur);
                }

                if (data.errors.length > 0 && data.repos.length === 0) {
                    this.#updateFromCache().catch(console.error);
                } else {
                    // Sort updates by name
                    for (const repo of data.repos) repo.updates.sort((a, b) => a.name.localeCompare(b.name));

                    // Cache and set
                    writeFileAsync(this.#cachePath, JSON.stringify({ cached: true, ...data })).catch(console.error);
                    this.#data = data;
                    this.notify("update-data");
                    this.notify("list");
                    this.notify("num-updates");
                    this.notify("news");
                }

                this.#loading = false;
                this.notify("loading");

                this.#timeout?.destroy();
                this.#timeout = setTimeout(() => this.getUpdates(), config.interval.get());
            })
            .catch(console.error);
    }

    constructor() {
        super();

        // Initial update from cache, if fail then write valid data to cache so future reads don't fail
        this.#updateFromCache().catch(() =>
            writeFileAsync(this.#cachePath, JSON.stringify(this.#data)).catch(console.error)
        );
        this.getUpdates();

        config.interval.subscribe(i => {
            this.#timeout?.destroy();
            this.#timeout = setTimeout(() => this.getUpdates(), i);
        });
    }
}