From 478dcc4f0f1411b04aecffd2e046af138bbdf00c Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 23 May 2023 01:39:34 -0400 Subject: [PATCH] Add Zotero.Utilities.Internal.subprocess(command, args) To run a short-lived command and return stdout The Subprocess module can also start long-running process and communicate with them, but we'll implement something different for that if we need it. --- .../zotero/xpcom/utilities_internal.js | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index db027962fb..5873fd6627 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -26,6 +26,12 @@ ***** END LICENSE BLOCK ***** */ +var { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm"); + +XPCOMUtils.defineLazyModuleGetters(globalThis, { + Subprocess: "resource://gre/modules/Subprocess.jsm", +}); + /** * @class Utility functions not made available to translators */ @@ -618,7 +624,31 @@ Zotero.Utilities.Internal = { return deferred.promise; }), - + + /** + * Run a short-lived process and return its stdout + * + * @param {String} command - The command to run; if no slashes, will search PATH for the command + * @param {String[]} args - An array of arguments to pass to the command + * @return {String} + */ + subprocess: async function (command, args) { + command = command.includes('/') ? command : await Subprocess.pathSearch(command); + + Zotero.debug("Running " + command + " " + args.map(arg => "'" + arg + "'").join(" ")); + + let proc = await Subprocess.call({ + command, + arguments: args, + }); + let result = ""; + let str; + while ((str = await proc.stdout.readString())) { + result += str; + } + return result; + }, + /** * Get string data from the clipboard * @param {String[]} mimeType MIME type of data to get