From 0738194780ce52b9047c430b9498487417b2cd07 Mon Sep 17 00:00:00 2001 From: Aylur Date: Sun, 1 Sep 2024 02:06:50 +0200 Subject: move libastal to /core starting point of the monorepo --- core/gjs/src/process.ts | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 core/gjs/src/process.ts (limited to 'core/gjs/src/process.ts') diff --git a/core/gjs/src/process.ts b/core/gjs/src/process.ts new file mode 100644 index 0000000..c5329e2 --- /dev/null +++ b/core/gjs/src/process.ts @@ -0,0 +1,69 @@ +import { Astal } from "./imports.js" + +type Args = { + cmd: string | string[] + out?: (stdout: string) => Out + err?: (stderr: string) => Err +} + +function args(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 = printerr, +) { + 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 +} + +/** @throws {GLib.Error} Throws stderr */ +export function exec(cmd: string | string[]) { + return Array.isArray(cmd) + ? Astal.Process.execv(cmd) + : Astal.Process.exec(cmd) +} + +export function execAsync(cmd: string | string[]): Promise { + return new Promise((resolve, reject) => { + if (Array.isArray(cmd)) { + Astal.Process.exec_asyncv(cmd, (_, res) => { + try { + resolve(Astal.Process.exec_asyncv_finish(res)) + } + catch (error) { + reject(error) + } + }) + } + else { + Astal.Process.exec_async(cmd, (_, res) => { + try { + resolve(Astal.Process.exec_finish(res)) + } + catch (error) { + reject(error) + } + }) + } + }) +} -- cgit v1.2.3