-
Notifications
You must be signed in to change notification settings - Fork 39
/
utils.js
44 lines (36 loc) · 1.25 KB
/
utils.js
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 Gio from 'gi://Gio';
export class ScriptRunner {
constructor() {
this._cancellable = null;
}
async runScriptAsync(argv) {
return new Promise((resolve, reject) => {
this.cancel();
try {
const proc = new Gio.Subprocess({
argv,
flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
});
this._cancellable = new Gio.Cancellable();
proc.init(this._cancellable);
proc.communicate_utf8_async(null, null, (proc, res) => {
const [, stdout, stderr] = proc.communicate_utf8_finish(res);
if (proc.get_successful() && stdout) {
resolve(stdout);
} else {
reject(stderr);
}
});
this._cancellable.connect(() => proc.force_exit());
} catch (e) {
logError('ERROR: Python execution failed: ' + e);
}
});
}
cancel() {
if (this._cancellable instanceof Gio.Cancellable) {
this._cancellable.cancel();
this._cancellable = null;
}
}
}