Skip to content

jeffreybradley1963/pr-optional-output-in-response #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,50 @@ async function promise(cmd) {
handleError(error);
reject();
}
resolve();
const returnValue = { stdout, stderr };
resolve(returnValue);
});
});
}

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeffreybradley1963 I am not sure if it is going to return a new Promise with "new" keyword. Can you instead send the returnValue inside the resolve like

return new Promise((resolve, reject) => {
	resolve(returnValue);
});

}
}
}
Expand Down