Comparing version 2.0.0 to 2.1.0
54
index.js
@@ -6,18 +6,3 @@ 'use strict'; | ||
promptly.prompt = function (message, opts, fn) { | ||
// Arguments parsing | ||
if (typeof opts === 'function') { | ||
fn = opts; | ||
opts = {}; | ||
} else if (!opts) { | ||
opts = {}; | ||
} | ||
if (opts.trim === undefined) { | ||
opts.trim = true; | ||
} | ||
if (opts.retry === undefined) { | ||
opts.retry = true; | ||
} | ||
function prompt(message, opts, fn) { | ||
// Setup read's options | ||
@@ -83,2 +68,33 @@ var readOpts = { | ||
}); | ||
} | ||
promptly.prompt = function (message, opts, fn) { | ||
// Arguments parsing | ||
if (typeof opts === 'function') { | ||
fn = opts; | ||
opts = {}; | ||
} else if (!opts) { | ||
opts = {}; | ||
} | ||
if (opts.trim === undefined) { | ||
opts.trim = true; | ||
} | ||
if (opts.retry === undefined) { | ||
opts.retry = true; | ||
} | ||
if (fn) { | ||
return prompt(message, opts, fn); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
prompt(message, opts, function (err, result) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(result); | ||
}); | ||
}); | ||
}; | ||
@@ -107,3 +123,3 @@ | ||
// Use prompt() | ||
promptly.prompt(message, opts, fn); | ||
return promptly.prompt(message, opts, fn); | ||
}; | ||
@@ -149,3 +165,3 @@ | ||
// Use choose() with true, false | ||
promptly.choose(message, [true, false], opts, fn); | ||
return promptly.choose(message, [true, false], opts, fn); | ||
}; | ||
@@ -183,3 +199,3 @@ | ||
// Use prompt() | ||
promptly.prompt(message, opts, fn); | ||
return promptly.prompt(message, opts, fn); | ||
}; |
{ | ||
"name": "promptly", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Simple command line prompting utility", | ||
@@ -10,5 +10,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"async": "^1.5.1", | ||
"async": "^2.0.0", | ||
"expect.js": "^0.3.1", | ||
"mocha": "^2.1.0" | ||
"mocha": "^3.0.2" | ||
}, | ||
@@ -15,0 +15,0 @@ "scripts": { |
@@ -25,6 +25,6 @@ # promptly | ||
### .prompt(message, [opts], fn) | ||
### .prompt(message, [opts], [fn]) | ||
Prompts for a value, printing the `message` and waiting for the input. | ||
When done, calls `fn` with `error` and `value`. | ||
When done, calls `fn` with `error` and `value` or returns a `Promise` if no `fn` is provided. | ||
@@ -51,2 +51,3 @@ Default options: | ||
The validators have two purposes: | ||
```js | ||
@@ -67,2 +68,3 @@ function (value) { | ||
Ask for a name: | ||
```js | ||
@@ -75,2 +77,12 @@ promptly.prompt('Name: ', function (err, value) { | ||
Using Promise: | ||
```js | ||
promptly.prompt('Name: ') | ||
.then(function (value) { | ||
// no need for catch in this case, because no validators are set | ||
console.log(value); | ||
}); | ||
``` | ||
Ask for a name with a constraint (non-empty value and length > 2): | ||
@@ -108,3 +120,3 @@ | ||
if (err) { | ||
console.error('Invalid name:', e.message); | ||
console.error('Invalid name:', err.message); | ||
// Manually call retry | ||
@@ -111,0 +123,0 @@ // The passed error has a retry method to easily prompt again. |
@@ -192,2 +192,17 @@ 'use strict'; | ||
}); | ||
it('should prompt the user (using promise)', function (next) { | ||
promptly.prompt('something: ') | ||
.then(function (value) { | ||
expect(value).to.be('yeaa'); | ||
expect(stdout).to.contain('something: '); | ||
next(); | ||
}) | ||
.catch(function () { | ||
expect().fail(); | ||
next(); | ||
}); | ||
sendLine('yeaa'); | ||
}); | ||
}); | ||
@@ -243,2 +258,17 @@ | ||
}); | ||
it('should work using promise', function (next) { | ||
promptly.choose('apple or orange? ', ['apple', 'orange']) | ||
.then(function (value) { | ||
expect(value).to.be('orange'); | ||
expect(stdout).to.contain('apple or orange? '); | ||
next(); | ||
}) | ||
.catch(function () { | ||
expect().fail(); | ||
next(); | ||
}); | ||
sendLine('orange'); | ||
}); | ||
}); | ||
@@ -294,2 +324,17 @@ | ||
}); | ||
it('should work using promise', function (next) { | ||
promptly.confirm('yes or no? ') | ||
.then(function (value) { | ||
expect(stdout).to.contain('yes or no? '); | ||
expect(value).to.be(true); | ||
next(); | ||
}) | ||
.catch(function () { | ||
expect().fail(); | ||
next(); | ||
}); | ||
sendLine('y'); | ||
}); | ||
}); | ||
@@ -332,2 +377,18 @@ | ||
}); | ||
it('should prompt the user silently using promise', function (next) { | ||
promptly.password('something: ') | ||
.then(function (value) { | ||
expect(value).to.be('yeaa'); | ||
expect(stdout).to.contain('something: '); | ||
expect(stdout).to.not.contain('yeaa'); | ||
next(); | ||
}) | ||
.catch(function () { | ||
expect().fail(); | ||
next(); | ||
}); | ||
sendLine('yeaa'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
24507
484
177
9