summaryrefslogtreecommitdiff
path: root/gjs/src/process.ts
blob: f7ff7b0b39326a465247130fb25c7ccb112461ca (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
import { Astal } from "./imports.js"

type Args<Out = void, Err = void> = {
    cmd: string | string[]
    out?: (stdout: string) => Out
    err?: (stderr: string) => Err
}

function args<O, E>(argsOrCmd: Args | string | string[], onOut: O, onErr: E) {
    const params = Array.isArray(argsOrCmd) || typeof argsOrCmd === "string"
    return {
        cmd: params ? argsOrCmd : argsOrCmd.cmd,
        err: params ? onErr : argsOrCmd.err || onErr,
        out: params ? onOut : argsOrCmd.out || onOut,
    }
}

export function subprocess(args: Args): Astal.Process
export function subprocess(
    cmd: string | string[],
    onOut?: (stdout: string) => void,
    onErr?: (stderr: string) => void,
): Astal.Process
export function subprocess(
    argsOrCmd: Args | string | string[],
    onOut: (stdout: string) => void = print,
    onErr: (stderr: string) => void = console.log,
) {
    const { cmd, err, out } = args(argsOrCmd, onOut, onErr)
    const proc = Array.isArray(cmd)
        ? Astal.Process.subprocessv(cmd)
        : Astal.Process.subprocess(cmd)

    proc.connect("stdout", (_, stdout: string) => out(stdout))
    proc.connect("stderr", (_, stderr: string) => err(stderr))
    return proc
}

export function exec<Out = string, Err = string>(
    args: Args<Out, Err>
): Out | Err
export function exec<Out = string, Err = string>(
    cmd: string | string[],
    onOut?: (stdout: string) => Out,
    onErr?: (stderr: string) => Err,
): Out | Err
export function exec<Out = string, Err = string>(
    argsOrCmd: Args<Out, Err> | string | string[],
    onOut: (stdout: string) => Out = out => out as Out,
    onErr: (stderr: string) => Err = out => out as Err,
): Out | Err {
    const { cmd, err, out } = args(argsOrCmd, onOut, onErr)
    return Array.isArray(cmd)
        ? out(Astal.Process.execv(cmd)!) as Out
        : err(Astal.Process.exec(cmd)!) as Err
}

export function execAsync(cmd: string | string[]): Promise<string> {
    const proc = Array.isArray(cmd)
        ? Astal.Process.exec_asyncv(cmd)
        : Astal.Process.exec_async(cmd)
    return new Promise((resolve, reject) => {
        proc.connect("stdout", (_, out: string) => resolve(out))
        proc.connect("stderr", (_, err: string) => reject(err))
    })
}