add simple spawn function to return promise

This commit is contained in:
joshuaboud 2021-10-04 13:16:08 -03:00
parent ae06067e97
commit f97883887c
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
1 changed files with 22 additions and 0 deletions

View File

@ -107,3 +107,25 @@ export function check_if_exists(path) {
proc.fail((e, data) => {resolve(true)});
});
}
/**
* Spawn process and resolve/reject with output
*
* @param {string[]} argv argv of proc
* @param {string | null} input optional input to stdin of proc
* @returns {Promise<string>}
*/
export function simple_spawn(argv, input = null) {
return new Promise((resolve, reject) => {
var proc = cockpit.spawn(argv, {superuser: "try"});
proc.done(data => resolve(data));
proc.fail((e, data) => {
if (data)
reject(data);
else
reject("Execution error: " + e);
});
if (input != null)
proc.input(input);
});
}