summaryrefslogtreecommitdiff
path: root/gjs/src
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-06-20 15:16:59 +0200
committerAylur <[email protected]>2024-06-20 15:16:59 +0200
commit4e32f62dcd50c545670ef67f9b0f65ed87821426 (patch)
treec4160e311b91cd7bf5ae515db8315f322e99922a /gjs/src
parent976638c016eb21ade63779c9dade6110983dc75b (diff)
add file utils
Diffstat (limited to 'gjs/src')
-rw-r--r--gjs/src/file.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/gjs/src/file.ts b/gjs/src/file.ts
new file mode 100644
index 0000000..0825e31
--- /dev/null
+++ b/gjs/src/file.ts
@@ -0,0 +1,40 @@
+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, callback)
+}