yargs-promise
Advanced tools
Comparing version 1.0.0 to 1.1.0
37
index.js
@@ -12,16 +12,8 @@ class YargsPromise { | ||
const yargs = this.yargs; | ||
let returnArgs; | ||
return new Promise((resolve, reject) => { | ||
let context = Object.assign({}, this.ctx); | ||
// the following context methods use `data` | ||
// to distinguish themselves as resolve/reject | ||
// calls inside of command handlers | ||
context.resolve = function(data, argv) { | ||
return resolve({ data, argv }); | ||
}; | ||
let context = Object.assign({}, this.ctx, {resolve, reject}); | ||
context.reject = function(data, argv) { | ||
return reject({ data, argv }); | ||
}; | ||
yargs.parse(msg, context, function(error, argv, output) { | ||
returnArgs = argv; | ||
// the reject/resolve calls below are from | ||
@@ -31,8 +23,25 @@ // internal yarg behavior. | ||
// reject built in validation error | ||
return reject({argv, error}); | ||
return argv.reject(error); | ||
} | ||
// resolve built in output | ||
return resolve({argv, output}); | ||
return argv.resolve(output); | ||
}); | ||
}); | ||
}) | ||
.then(p => Promise.resolve(p)) // resolve possible promise | ||
.then(data => { | ||
if (data && data.argv) { | ||
return data; | ||
} | ||
return {data, argv: returnArgs}; | ||
}) | ||
.catch((p) => // resolve possible promise | ||
Promise.resolve(p).then(error => { | ||
if (error && error.argv) { | ||
return Promise.reject(error); | ||
} | ||
return Promise.reject({error, argv: returnArgs}); | ||
}) | ||
); | ||
} | ||
@@ -39,0 +48,0 @@ } |
@@ -10,3 +10,3 @@ const yargs = require('yargs'); | ||
customContextMethod = jest.fn(); | ||
parser = new YargsPromise(yargs, { customContextMethod, foo: 'bar' }); | ||
parser = new YargsPromise(yargs, {customContextMethod, foo: 'bar'}); | ||
}); | ||
@@ -18,10 +18,36 @@ | ||
it('resolves built in output from commands without handler like help', (done) => { | ||
parser.command('hello <name>', 'hello world parser') | ||
.help(); | ||
it('resolves built in data from commands without handler like help', done => { | ||
parser.command('hello <name>', 'hello world parser').help(); | ||
return parser.parse('hello --help') | ||
.then(({argv, output}) => { | ||
return parser | ||
.parse('hello --help') | ||
.then(({argv, data}) => { | ||
expect(argv['_']).toEqual(['hello']); | ||
expect(output).toContain('Show help'); | ||
expect(data).toContain('Show help'); | ||
done(); | ||
}) | ||
.catch(err => { | ||
done.fail(err); | ||
}); | ||
}); | ||
it('can resolve returned promises from command handler argv.resolve', done => { | ||
parser.command( | ||
'hello <name>', | ||
'hello world parser', | ||
() => {}, | ||
argv => { | ||
argv.resolve( | ||
new Promise((resolve, reject) => { | ||
resolve('response data'); | ||
}) | ||
); | ||
} | ||
); | ||
return parser | ||
.parse('hello world') | ||
.then(({argv, data}) => { | ||
expect(argv['_']).toEqual(['hello']); | ||
expect(data).toContain('response data'); | ||
return done(); | ||
@@ -32,7 +58,32 @@ }) | ||
it('resolves parsed argv from commands without handlers', (done) => { | ||
it('can reject returned promises from command handler argv.reject', done => { | ||
parser.command( | ||
'hello <name>', | ||
'hello world parser', | ||
() => {}, | ||
argv => { | ||
argv.reject( | ||
new Promise((resolve, reject) => { | ||
resolve('response data'); | ||
}) | ||
); | ||
} | ||
); | ||
return parser | ||
.parse('hello world') | ||
.then(() => done.fail('Should not be reached')) | ||
.catch(data => { | ||
expect(data.argv['_']).toEqual(['hello']); | ||
expect(data.error).toEqual('response data'); | ||
return done(); | ||
}); | ||
}); | ||
it('resolves parsed argv from commands without handlers', done => { | ||
parser.command('hello <name>'); | ||
return parser.parse('hello world') | ||
.then(({argv, output}) => { | ||
return parser | ||
.parse('hello world') | ||
.then(({argv, data}) => { | ||
expect(argv['_']).toEqual(['hello']); | ||
@@ -45,10 +96,13 @@ expect(argv.name).toEqual('world'); | ||
it('rejects errors from built in validation errors', (done) => { | ||
it('rejects errors from built in validation errors', done => { | ||
parser.command('hello <name>'); | ||
return parser.parse('hello') | ||
return parser | ||
.parse('hello') | ||
.then(() => done.fail('Should not be reached')) | ||
.catch(({ error, argv }) => { | ||
.catch(({error, argv}) => { | ||
expect(argv['_']).toEqual(['hello']); | ||
expect(error.message).toEqual('Not enough non-option arguments: got 0, need at least 1'); | ||
expect(error.message).toEqual( | ||
'Not enough non-option arguments: got 0, need at least 1' | ||
); | ||
return done(); | ||
@@ -59,8 +113,14 @@ }) | ||
it('resolves custom data via context.reject in command handler', (done) => { | ||
parser.command('hello <name>', 'hello world command', () => {}, (argv) => { | ||
return argv.resolve('custom handler', argv); | ||
}); | ||
it('resolves custom data via context.reject in command handler', done => { | ||
parser.command( | ||
'hello <name>', | ||
'hello world command', | ||
() => {}, | ||
argv => { | ||
return argv.resolve('custom handler', argv); | ||
} | ||
); | ||
return parser.parse('hello world') | ||
return parser | ||
.parse('hello world') | ||
.then(({data, argv}) => { | ||
@@ -75,27 +135,39 @@ expect(data).toEqual('custom handler'); | ||
it('rejects custom data via context.reject in command handler', (done) => { | ||
parser.command('hello <name>', 'hello world command', () => {}, (argv) => { | ||
if (argv._[1]) { | ||
return argv.reject("Custom too many arguments", argv); | ||
it('rejects custom data via context.reject in command handler', done => { | ||
parser.command( | ||
'hello <name>', | ||
'hello world command', | ||
() => {}, | ||
argv => { | ||
if (argv._[1]) { | ||
return argv.reject('Custom too many arguments'); | ||
} | ||
return argv.resolve('hello', argv); | ||
} | ||
return argv.resolve('hello', argv); | ||
}); | ||
); | ||
return parser.parse('hello world 123') | ||
return parser | ||
.parse('hello world 123') | ||
.then(() => done.fail('Should not be reached')) | ||
.catch(({ data, argv }) => { | ||
.catch(({error, argv}) => { | ||
expect(argv['_'][1]).toEqual(123); | ||
expect(data).toEqual('Custom too many arguments'); | ||
expect(error).toEqual('Custom too many arguments'); | ||
return done(); | ||
}) | ||
.catch(error => done.fail(error)); | ||
.catch(err => done.fail(err)); | ||
}); | ||
it('supports merging custom context', (done) => { | ||
parser.command('hello <name>', 'hello world command', () => {}, (argv) => { | ||
argv.customContextMethod('yargs!!!'); | ||
return argv.resolve('custom handler', argv); | ||
}); | ||
it('supports merging custom context', done => { | ||
parser.command( | ||
'hello <name>', | ||
'hello world command', | ||
() => {}, | ||
argv => { | ||
argv.customContextMethod('yargs!!!'); | ||
return argv.resolve('custom handler', argv); | ||
} | ||
); | ||
return parser.parse('hello world') | ||
return parser | ||
.parse('hello world') | ||
.then(({data, argv}) => { | ||
@@ -102,0 +174,0 @@ expect(data).toEqual('custom handler'); |
{ | ||
"name": "yargs-promise", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "description": "Use the headless yargs parser with promises", |
@@ -25,3 +25,3 @@ # yargs-promise | ||
``` | ||
```js | ||
const yargs = require('yargs'); | ||
@@ -36,11 +36,11 @@ const YargsPromise = require('yargs-promise'); | ||
.command('hello <name>', 'hello world parser' , ()=>{}, (argv) => { | ||
// resolve stuff | ||
argv.resolve(yourData, argv); // pass back argv if you need it | ||
// resolve a promise or other value | ||
argv.resolve(doSomething); | ||
// reject stuff | ||
argv.reject(yourErrorData, argv); // pass back argv if you need it | ||
argv.reject(yourErrorData); | ||
// or do nothing and reject/resolve will be handled internally | ||
// however { data } will not be present in resolved or rejected responses | ||
console.log('testing argv', argv); | ||
console.log('testing argv'); | ||
}) | ||
@@ -51,27 +51,7 @@ .help(); | ||
parser.parse('hello world') | ||
.then(({argv, output, data}) => { | ||
// `output` exists if there was console output from yargs and if this was | ||
// resolved in internal parser callback | ||
// `data` exists if the promise was resolved in command handler | ||
// `argv` exists if the promise was resolved in internal parser callback | ||
// otherwise it will need to be passed as the 2nd argument to | ||
// context.resolve(data, argv) | ||
.then(({data, argv}) => { | ||
// data is what your code resolved or what an internal command resolved | ||
}) | ||
.catch((error, argv, data) => { | ||
// `error` exists if there was an internal error from yargs | ||
// `argv` exists if the promise was rejected in internal parser callback | ||
// otherwise it will need to be passed as the 2nd argument to | ||
// context.reject(data, argv) | ||
if (error) { | ||
// built in error validation | ||
} | ||
if (data) { | ||
// rejected from command handler | ||
} | ||
// argv contains parsed input | ||
.catch(({error, argv}) => { | ||
// `error` is what your code rejected or an internal error from yargs | ||
}); | ||
@@ -83,3 +63,3 @@ | ||
``` | ||
```js | ||
const yargs = require('yargs'); | ||
@@ -106,2 +86,4 @@ const YargsPromise = require('yargs-promise'); | ||
Need access to yargs object? Work with the direct `yargs` object prior to passing it into the yargs-promise constructor. For convenience, it is also available at `parser.yargs`. | ||
### How it works | ||
@@ -108,0 +90,0 @@ |
78053
204
102