Skip to content

Commit

Permalink
Check if command is 'oas' before throwing error (#62)
Browse files Browse the repository at this point in the history
* Check if command is 'oas' before throwing error

* In-progress test

* Prettier

* Fix `rdme oas` by removing `promisify(cp.spawn)`

According to this: https://stackoverflow.com/a/46321385 `cp.spawn` isn't really
suitable for promisifying due to it being an event listener. This causes
the promise to never resolve, which is what causes the test to timeout.

Refactored to use new Promise() and listen for the `close` event

* Prettier
  • Loading branch information
kanadgupta authored and erunion committed Aug 27, 2019
1 parent 7c74f66 commit 78f346b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
5 changes: 4 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ module.exports = processArgv => {
//
// Instead of failing out to the user with an undecipherable "Unknown value: ..." error, let's
// try to parse their request again but a tad less eager.
if (e.name !== 'UNKNOWN_VALUE' || (e.name === 'UNKNOWN_VALUE' && !argv.version)) {
if (
(e.name !== 'UNKNOWN_VALUE' || (e.name === 'UNKNOWN_VALUE' && !argv.version)) &&
argv.command !== 'oas'
) {
throw e;
}

Expand Down
21 changes: 15 additions & 6 deletions cmds/oas.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const cp = require('child_process');
const { spawn } = require('child_process');
const path = require('path');
const { promisify } = require('util');

const spawn = promisify(cp.spawn);

exports.command = 'oas';
exports.usage = 'oas';
Expand All @@ -13,7 +10,19 @@ exports.position = 1;
exports.args = [];

exports.run = function() {
return spawn(path.join(__dirname, '..', 'node_modules', '.bin', 'oas'), process.argv.slice(3), {
stdio: 'inherit',
const cp = spawn(
path.join(__dirname, '..', 'node_modules', '.bin', 'oas'),
process.argv.slice(3),
{
stdio: 'inherit',
},
);

return new Promise((resolve, reject) => {
cp.on('close', code => {
if (code && code > 0) return reject();

return resolve();
});
});
};
6 changes: 6 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,10 @@ describe('cli', () => {
assert.equal(err.message, 'No project version provided. Please use `--version`.');
});
});

it('should not error with oas arguments passed in', () => {
return assert.doesNotReject(() => {
return cli(['oas', 'init']);
});
});
});

0 comments on commit 78f346b

Please sign in to comment.