summaryrefslogtreecommitdiff
path: root/core/gjs/src/file.ts
blob: 90b33a15de4a044db2f906bc776b18cb7947b666 (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
import { Astal, Gio } from "./imports.js"

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)
    })!
}