summaryrefslogtreecommitdiff
path: root/lang/gjs/src/file.ts
blob: 4220d9d31867b144c65fbcfa683e09d2dbac786b (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
import Astal from "gi://AstalIO"
import Gio from "gi://Gio?version=2.0"

export { Gio }

export function readFile(path: string): string {
    return Astal.read_file(path) || ""
}

export function readFileAsync(path: string): Promise<string> {
    return new Promise((resolve, reject) => {
        Astal.read_file_async(path, (_, res) => {
            try {
                resolve(Astal.read_file_finish(res) || "")
            } catch (error) {
                reject(error)
            }
        })
    })
}

export function writeFile(path: string, content: string): void {
    Astal.write_file(path, content)
}

export function writeFileAsync(path: string, content: string): Promise<void> {
    return new Promise((resolve, reject) => {
        Astal.write_file_async(path, content, (_, res) => {
            try {
                resolve(Astal.write_file_finish(res))
            } catch (error) {
                reject(error)
            }
        })
    })
}

export function monitorFile(
    path: string,
    callback: (file: string, event: Gio.FileMonitorEvent) => void,
): Gio.FileMonitor {
    return Astal.monitor_file(path, (file: string, event: Gio.FileMonitorEvent) => {
        callback(file, event)
    })!
}