summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/customreboot@nova1545/utils.js
blob: 1c7514af3befcca9b32642361117a09e103a6d05 (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
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
/* utils.js
 *
 * Copyright (C) 2020
 *      Daniel Shchur (DocQuantum) <shchurgood@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
*/

import Gio from "gi://Gio";


var DEBUG = false;

/**
 * @param {String[]} argv 
 * @param {String} input 
 * @param {Gio.Cancellable} cancellable
 * @returns {Promise} Function execution
 * => @returns {[int, String, String]} [StatusCode, STDOUT, STDERR]
 * 
 * Executes a command asynchronously.
 */
export async function ExecCommand(argv, input = null, cancellable = null) {
    let flags = Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE;

    if (input !== null)
        flags |= Gio.SubprocessFlags.STDIN_PIPE;

    let proc = new Gio.Subprocess({
        argv: argv,
        flags: flags
    });
    proc.init(cancellable);
    return new Promise((resolve,reject) => {
        proc.communicate_utf8_async(input, cancellable, (proc, res) => {
            try {
                resolve([(function() {
                    if(!proc.get_if_exited())
                        throw new Error("Subprocess failed to exit in time!");
                    return proc.get_exit_status()
                })()].concat(proc.communicate_utf8_finish(res).slice(1)));
            } catch (e) {
                reject(e);
            }
        });
    });
}

/**
 * @param {bool} value
 * 
 * Set's whether to debug or to not.
 */
export function SetDebug(value){
    DEBUG = value;
}

/**
 * @param {string} msg 
 * 
 * Logs general messages if debug is set to true.
 */
export function Log(msg) {
    if(DEBUG)
        console.log(`CustomReboot NOTE: ${msg}`);
}

/**
 * @param {string} msg 
 * 
 * Logs warning messages if debug is set to true.
 */
export function LogWarning(msg) {
    if(DEBUG)
        console.warn(`CustomReboot WARN: ${msg}`);
}