From db5410d028a878756dacea1c369bf03ee7d57e24 Mon Sep 17 00:00:00 2001 From: Jeffrey Bradley Date: Tue, 11 Jul 2023 13:21:20 -0400 Subject: [PATCH] add options to retrieve output --- index.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 76e2a9f..ba0e030 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,8 @@ async function promise(cmd) { handleError(error); reject(); } - resolve(); + const returnValue = { stdout, stderr }; + resolve(returnValue); }); }); } @@ -30,28 +31,41 @@ async function promise(cmd) { module.exports = async (opt = {}) => { const defaultOptions = { path: ``, - cmd: `` + cmd: ``, + returnStdout: false, + returnStderr: false, }; const options = { ...defaultOptions, ...opt }; - const { path, cmd } = options; + const { path, cmd, returnStderr, returnStdout } = options; + + let returnValue = { + stdout: [], + stderr: [], + }; // changes directory if a path exists path !== `` ? process.chdir(path) : null; // checks if there is one command or array of commands if (typeof cmd !== 'object') { - return promise(cmd); + const output = await promise(cmd); + returnValue.stderr = returnStderr ? output.stderr : []; + returnValue.stdout = returnStdout ? output.stdout : []; + return Promise.resolve(returnValue); } else { for (let i = 0; i < cmd.length; i++) { - await promise(cmd[i]); - + const output = await promise(cmd[i]); + if (returnStderr) { + returnValue.stderr.push(output.stderr); + } + if (returnStdout) { + returnValue.stdout.push(output.stdout); + } let j = i + 1; if (j === cmd.length) { - return new Promise((resolve, reject) => { - resolve(); - }); + return Promise.resolve(returnValue); } } }