Comparing version 0.0.1 to 0.1.0
33
index.js
@@ -18,2 +18,5 @@ 'use strict'; | ||
} | ||
if (opts.retry === undefined) { | ||
opts.retry = true; | ||
} | ||
@@ -23,4 +26,4 @@ // Setup read's options | ||
prompt: message, | ||
stdin: opts.input || process.stdin, | ||
stdout: opts.output || process.stdout, | ||
input: opts.input || process.stdin, | ||
output: opts.output || process.stdout, | ||
silent: opts.silent | ||
@@ -30,3 +33,3 @@ }; | ||
// Use readline question | ||
read(readOpts, function (err, response) { | ||
read(readOpts, function (err, data) { | ||
// Ignore the error attribute | ||
@@ -40,10 +43,10 @@ // It is set on SIGINT or if timeout reached (we are not using timeout) | ||
if (opts.trim) { | ||
response = response.trim(); | ||
data = data.trim(); | ||
} | ||
// Mandatory? | ||
if (opts['default'] === undefined && !response) { | ||
if (opts['default'] == null && !data) { | ||
return promptly.prompt(message, opts, fn); | ||
} else { | ||
response = response || opts['default']; | ||
data = data || opts['default']; | ||
} | ||
@@ -62,6 +65,10 @@ | ||
try { | ||
response = opts.validator[x](response); | ||
data = opts.validator[x](data); | ||
} catch (e) { | ||
// Retry automatically if the retry option is enabled | ||
if (opts.retry) { | ||
if (e.message) { | ||
readOpts.output.write(e.message + '\n'); | ||
} | ||
return promptly.prompt(message, opts, fn); | ||
@@ -78,3 +85,3 @@ } | ||
// Everything ok | ||
fn(null, response); | ||
fn(null, data); | ||
}); | ||
@@ -99,2 +106,5 @@ }; | ||
} | ||
if (opts['default'] === undefined) { | ||
opts['default'] = ''; | ||
} | ||
@@ -138,3 +148,3 @@ // Use prompt() | ||
return value; | ||
throw new Error(); | ||
}; | ||
@@ -161,7 +171,2 @@ opts.validator.push(validator); | ||
// Set the default options | ||
if (opts.retry === undefined) { | ||
opts.retry = true; | ||
} | ||
// Push the choice validator | ||
@@ -168,0 +173,0 @@ var validator = function (value) { |
{ | ||
"name": "promptly", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Simple command line prompting utility", | ||
"main": "index.js", | ||
"dependencies": { | ||
"read": "~1.0.4" | ||
}, | ||
"devDependencies": { | ||
@@ -30,6 +33,3 @@ "mocha": "~1.8.1", | ||
"author": "IndigoUnited <hello@indigounited.com> (http://indigounited.com)", | ||
"license": "MIT", | ||
"dependencies": { | ||
"read": "~1.0.4" | ||
} | ||
"license": "MIT" | ||
} |
# promptly # | ||
--- | ||
[![Build Status](https://secure.travis-ci.org/IndigoUnited/node-promptly.png)](http://travis-ci.org/IndigoUnited/node-promptly.png) | ||
Simple command line prompting utility. | ||
@@ -13,5 +15,6 @@ | ||
In all the commands, the options argument is not mandatory. | ||
Note that the `options` argument is optional for all the commands. | ||
### .prompt(message, opts, fn) ### | ||
@@ -25,4 +28,4 @@ | ||
{ | ||
// The default value to assume. If not supplied, the input is mandatory | ||
'default': 'default value', | ||
// The default value. If not supplied, the input is mandatory | ||
'default': null, | ||
// Automatically trim the input | ||
@@ -33,3 +36,3 @@ 'trim': true, | ||
// Automatically retry if a validator fails | ||
'retry': false, | ||
'retry': true, | ||
// Do not print what the user types | ||
@@ -67,2 +70,3 @@ 'silent': false, | ||
Ask for a name with a constraint (non-empty value and length > 2): | ||
```js | ||
@@ -78,14 +82,10 @@ var validator = function (value) { | ||
promptly.prompt('Name: ', { validator: validator }, function (err, value) { | ||
if (err) { | ||
console.error('Invalid name'); | ||
// Manually call retry | ||
// The passed error have a retry method to easily prompt again. | ||
err.retry(); | ||
} | ||
console.log('Name is: ', value); | ||
// Since retry is true by default, err is always null | ||
// because promptly will be prompting for a name until it validates | ||
// Between each prompt, the error message from the validator will be printed | ||
console.log('Name is:', value); | ||
}); | ||
``` | ||
Same as above but retry automatically: | ||
Same as above but do not retry automatically: | ||
@@ -101,9 +101,14 @@ ```js | ||
promptly.prompt('Name: ', { validator: validator , retry: true}, function (err, value) { | ||
// err is always null because promptly will be prompting for a name until it validates | ||
console.log('Name is: ', value); | ||
promptly.prompt('Name: ', { validator: validator, retry: false }, function (err, value) { | ||
if (err) { | ||
console.error('Invalid name:', e.message); | ||
// Manually call retry | ||
// The passed error has a retry method to easily prompt again. | ||
return err.retry(); | ||
} | ||
console.log('Name is:', value); | ||
}); | ||
``` | ||
### .confirm(message, opts, fn) ### | ||
@@ -114,6 +119,5 @@ | ||
The available options are the same, except that `retry` defaults to `true`. | ||
Truthy values are: `y`, `yes` and `1`. | ||
Falsy values are `n`, `no`, and `0`. | ||
Comparison is made in case insensitive way. | ||
Truthy values are: `y`, `yes` and `1`. | ||
Falsy values are `n`, `no`, and `0`. | ||
Comparison is made in a case insensitive way. | ||
@@ -124,3 +128,3 @@ Example usage: | ||
promptly.confirm('Are you sure? ', function (err, value) { | ||
console.log('Answer: ', value); | ||
console.log('Answer:', value); | ||
}); | ||
@@ -133,6 +137,4 @@ ``` | ||
Ask the user to choose between multiple `choices` (array of choices). | ||
Calls `fn` with `error` and `value` (true or false). | ||
Calls `fn` with `error` and `value`. | ||
The available options are the same, except that `retry` defaults to `true`. | ||
Example usage: | ||
@@ -142,3 +144,3 @@ | ||
promptly.choose('Do you want an apple or an orange? ', ['apple', 'orange'], function (err, value) { | ||
console.log('Answer: ', value); | ||
console.log('Answer:', value); | ||
}); | ||
@@ -151,5 +153,5 @@ ``` | ||
Prompts for a password, printing the `message` and waiting for the input. | ||
When available, calls `fn` with `error` and `value`. | ||
When available, calls `fn` with `error` and `value`. | ||
The available options are the same, except that `trim` and `silent` defaults to `false`. | ||
The available options are the same, except that `trim` and `silent` default to `false` and `default` is an empty string (to allow empty passwords). | ||
@@ -160,3 +162,3 @@ Example usage: | ||
promptly.password('Type a password: ', function (err, value) { | ||
console.log('Password is', value); | ||
console.log('Password is:', value); | ||
}); | ||
@@ -163,0 +165,0 @@ ``` |
@@ -80,3 +80,3 @@ 'use strict'; | ||
promptly.prompt('something: ', { validator: validator }, function (err, value) { | ||
promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) { | ||
expect(err).to.be(null); | ||
@@ -91,10 +91,10 @@ expect(value).to.be('yeaa'); | ||
it('should give error if the validator fails', function (next) { | ||
it('should assume values from the validator', function (next) { | ||
stdout = ''; | ||
var validator = function () { throw new Error('bla'); }; | ||
var validator = function () { return 'bla'; }; | ||
promptly.prompt('something: ', { validator: validator }, function (err) { | ||
expect(err).to.be.an(Error); | ||
expect(err.message).to.be('bla'); | ||
promptly.prompt('something: ', { validator: validator }, function (err, value) { | ||
expect(err).to.be(null); | ||
expect(value).to.be('bla'); | ||
expect(stdout).to.contain('something: '); | ||
@@ -107,14 +107,37 @@ next(); | ||
it('should assume values from the validator', function (next) { | ||
it('should automatically retry if a validator fails by default', function (next) { | ||
stdout = ''; | ||
var validator = function () { return 'bla'; }; | ||
var validator = function (value) { | ||
if (value !== 'yeaa') { | ||
throw new Error('bla'); | ||
} | ||
promptly.prompt('something: ', { validator: validator }, function (err, value) { | ||
expect(err).to.be(null); | ||
expect(value).to.be('bla'); | ||
return value; | ||
}; | ||
promptly.prompt('something: ', { validator: validator, retry: true }, function (err, value) { | ||
expect(stdout).to.contain('something: '); | ||
expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something')); | ||
expect(stdout).to.contain('bla'); | ||
expect(value).to.equal('yeaa'); | ||
next(); | ||
}); | ||
process.stdin.emit('data', 'wtf\n'); | ||
process.stdin.emit('data', 'yeaa\n'); | ||
}); | ||
it('should give error if the validator fails and retry is false', function (next) { | ||
stdout = ''; | ||
var validator = function () { throw new Error('bla'); }; | ||
promptly.prompt('something: ', { validator: validator, retry: false }, function (err) { | ||
expect(err).to.be.an(Error); | ||
expect(err.message).to.be('bla'); | ||
expect(stdout).to.contain('something: '); | ||
next(); | ||
}); | ||
process.stdin.emit('data', ' yeaa \n'); | ||
@@ -135,3 +158,3 @@ }); | ||
promptly.prompt('something: ', { validator: validator }, function (err, value) { | ||
promptly.prompt('something: ', { validator: validator, retry: false }, function (err, value) { | ||
times++; | ||
@@ -154,23 +177,2 @@ | ||
it('should automatically retry if a validator fails and retry is enabled', function (next) { | ||
stdout = ''; | ||
var validator = function (value) { | ||
if (value !== 'yeaa') { | ||
throw new Error('bla'); | ||
} | ||
return value; | ||
}; | ||
promptly.prompt('something: ', { validator: validator, retry: true }, function (err, value) { | ||
expect(stdout).to.contain('something: '); | ||
expect(stdout.indexOf('something')).to.not.be(stdout.lastIndexOf('something')); | ||
expect(value).to.equal('yeaa'); | ||
next(); | ||
}); | ||
process.stdin.emit('data', 'wtf\n'); | ||
process.stdin.emit('data', 'yeaa\n'); | ||
}); | ||
}); | ||
@@ -187,2 +189,3 @@ | ||
expect(stdout.indexOf('apple or orange')).to.not.be(stdout.lastIndexOf('apple or orange')); | ||
expect(stdout).to.contain('Invalid choice'); | ||
next(); | ||
@@ -269,3 +272,3 @@ }); | ||
expect(err).to.be.an(Error); | ||
expect(err.message).to.contain('choice'); | ||
expect(err.message).to.not.contain('Invalid choice'); | ||
expect(stdout).to.contain('yes or no: '); | ||
@@ -307,2 +310,15 @@ next(); | ||
}); | ||
it('show allow empty passwords by default', function (next) { | ||
stdout = ''; | ||
promptly.password('something: ', function (err, value) { | ||
expect(value).to.be(''); | ||
expect(stdout).to.contain('something: '); | ||
next(); | ||
}); | ||
process.stdin.emit('data', '\n'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21247
8
390
159