-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathutil.js
More file actions
235 lines (199 loc) · 5.55 KB
/
util.js
File metadata and controls
235 lines (199 loc) · 5.55 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Xdp from "gi://Xdp";
import GObject from "gi://GObject";
import { getLanguage } from "./common.js";
export const portal = new Xdp.Portal();
export const settings = new Gio.Settings({
schema_id: pkg.name,
path: "/re/sonny/Workbench/",
});
export const data_dir = Gio.File.new_for_path(
GLib.build_filenamev([GLib.get_user_data_dir(), pkg.name]),
);
export function ensureDir(file) {
try {
file.make_directory_with_parents(null);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw err;
}
}
}
let flatpak_info;
export function getFlatpakInfo() {
if (flatpak_info) return flatpak_info;
flatpak_info = new GLib.KeyFile();
try {
flatpak_info.load_from_file("/.flatpak-info", GLib.KeyFileFlags.NONE);
} catch (err) {
if (!err.matches(GLib.FileError, GLib.FileError.NOENT)) {
console.error(err);
}
return null;
}
return flatpak_info;
}
export { getLanguage };
export function listenProperty(object, property, fn, { initial = false } = {}) {
if (initial) {
fn(object[property]);
}
const signal = `notify::${property}`;
const handler_id = object.connect(signal, () => {
fn(object[property]);
});
return {
block() {
GObject.signal_handler_block(object, handler_id);
},
unblock() {
GObject.signal_handler_unblock(object, handler_id);
},
disconnect() {
return object.disconnect(handler_id);
},
};
}
export function decode(data) {
if (data instanceof GLib.Bytes) {
data = data.toArray();
}
return new TextDecoder().decode(data);
}
export function encode(data) {
return new TextEncoder().encode(
data.to_string?.() || data?.toString?.() || data,
);
}
// Take a function that return a promise and returns a function
// that will discard all calls during a pending execution
// it's like a job queue with a max size of 1 and no concurrency
export function unstack(fn, onError = console.error) {
let latest_promise;
let latest_arguments;
let pending = false;
return function unstack_wrapper(...args) {
latest_arguments = args;
if (pending) return;
if (!latest_promise) {
latest_promise = fn(...latest_arguments).catch(onError);
}
pending = true;
latest_promise.finally(() => {
pending = false;
latest_promise = fn(...latest_arguments).catch(onError);
});
};
}
export const demos_dir = Gio.File.new_for_path(
pkg.pkgdatadir,
).resolve_relative_path("demos");
export async function copyDirectory(source, destination) {
try {
destination.make_directory_with_parents(null);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw err;
}
}
const enumerator = await source.enumerate_children_async(
Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
Gio.FileQueryInfoFlags.NONE,
GLib.PRIORITY_DEFAULT,
null,
);
for await (const file_info of enumerator) {
const child = enumerator.get_child(file_info);
const child_dest = destination.get_child(child.get_basename());
if (file_info.get_file_type() === Gio.FileType.DIRECTORY) {
await copyDirectory(child, child_dest);
continue;
}
try {
await child.copy_async(
child_dest, // destination
Gio.FileCopyFlags.OVERWRITE, // flags
GLib.PRIORITY_DEFAULT, // priority
null, // cancellable
null, // progress_callback
);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw err;
}
}
}
}
export function getNowForFilename() {
return new GLib.DateTime().format("%Y-%m-%d %H-%M-%S");
}
// https://gitlab.gnome.org/GNOME/libadwaita/-/issues/746
export function makeDropdownFlat(dropdown) {
dropdown.get_first_child().add_css_class("flat");
}
export function buildRuntimePath(...args) {
return GLib.build_filenamev([
GLib.getenv("XDG_RUNTIME_DIR"),
GLib.getenv("FLATPAK_ID"),
...args,
]);
}
export function quitOnLastWindowClose(self) {
const { application } = self;
const application_windows = [...application.get_windows()];
const is_last_window_open = application_windows
.filter((w) => w !== self)
.every((w) => !w.get_mapped());
if (is_last_window_open) {
application.quit();
}
return false;
}
export function removeDirectory(file) {
// There is no method to recursively delete a folder so we trash instead
// https://github.com/flatpak/xdg-desktop-portal/issues/630 :/
// portal.trash_file(file.get_path(), null).catch(console.error);
try {
file.trash(null);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw err;
}
}
}
export async function copy(filename, source_dir, dest_dir, flags) {
const file = source_dir.get_child(filename);
const dest_file = dest_dir.get_child(file.get_basename());
try {
await file.copy_async(
dest_file, // destination
flags, // flags
GLib.PRIORITY_DEFAULT, // priority
null, // cancellable
null, // progress_callback
);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw err;
}
}
return dest_file;
}
const tmp_dirs = {};
export async function createTmpDir(filename) {
if (filename in tmp_dirs) {
return tmp_dirs[filename];
}
const tmp_dir = Gio.File.new_for_path(
GLib.build_filenamev([GLib.get_tmp_dir(), filename]),
);
try {
tmp_dir.make_directory(null);
tmp_dirs[filename] = tmp_dir;
} catch (err) {
console.error(err);
return null;
}
return tmp_dir;
}