Skip to content

Commit

Permalink
refactor(index): concat invalid args provided into error object
Browse files Browse the repository at this point in the history
  • Loading branch information
Frazer Smith committed Nov 10, 2020
1 parent a0624e9 commit dd1084e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const platform = os.platform();
* @param {object} acceptedOptions - Object containing options that a binary accepts.
* @returns {Promise<string|Error>} Promise of stdout string on resolve, or Error object on rejection.
*/
function parseOptions(options, acceptedOptions) {
function parseOptions(acceptedOptions, options) {
return new Promise((resolve, reject) => {
const args = [];
const invalidArgs = [];
Object.keys(options).forEach((key) => {
if (Object.prototype.hasOwnProperty.call(acceptedOptions, key)) {
// eslint-disable-next-line valid-typeof
Expand All @@ -26,19 +27,21 @@ function parseOptions(options, acceptedOptions) {
args.push(options[key]);
}
} else {
reject(
new Error(
`Invalid value type provided for option '${key}', expected ${
acceptedOptions[key].type
} but recieved ${typeof options[key]}`
)
invalidArgs.push(
`Invalid value type provided for option '${key}', expected ${
acceptedOptions[key].type
} but recieved ${typeof options[key]}`
);
}
} else {
reject(new Error(`Invalid option provided '${key}'`));
invalidArgs.push(`Invalid option provided '${key}'`);
}
});
resolve(args);
if (invalidArgs.length === 0) {
resolve(args);
} else {
reject(new Error(invalidArgs.join('; ')));
}
});
}

Expand Down Expand Up @@ -115,7 +118,7 @@ class UnRTF {
}

try {
const args = await parseOptions(options, acceptedOptions);
const args = await parseOptions(acceptedOptions, options);
args.push(file);

const { stdout } = await execFileAsync(
Expand Down

0 comments on commit dd1084e

Please sign in to comment.