Skip to content

Commit

Permalink
Merge pull request #12 from riskawarrior/feature/promises
Browse files Browse the repository at this point in the history
Introduction of Promises
  • Loading branch information
satazor authored Aug 14, 2016
2 parents a5c0670 + 01338bf commit e52baa1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "6"
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Simple command line prompting utility.

## API

### .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 with `Promise` if no `fn` is provided.

Default options:
```js
Expand All @@ -47,6 +47,7 @@ Default options:
```

The validators have two purposes:

```js
function (value) {
// Validation example, throwing an error when invalid
Expand All @@ -62,13 +63,24 @@ function (value) {
Example usages

Ask for a name:

```js
promptly.prompt('Name: ', function (err, value) {
// err is always null in this case, because no validators are set
console.log(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):

```js
Expand Down Expand Up @@ -101,7 +113,7 @@ var validator = function (value) {

promptly.prompt('Name: ', { validator: validator, retry: false }, function (err, value) {
if (err) {
console.error('Invalid name:', e.message);
console.error('Invalid name:', err.message);
// Manually call retry
// The passed error has a retry method to easily prompt again.
return err.retry();
Expand Down
54 changes: 35 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@
var read = require('read');
var promptly = module.exports;

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
var readOpts = {
prompt: message,
Expand Down Expand Up @@ -79,6 +64,37 @@ promptly.prompt = function (message, opts, fn) {
// Everything ok
fn(null, data);
});
}

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);
});
});
};

promptly.password = function (message, opts, fn) {
Expand All @@ -102,7 +118,7 @@ promptly.password = function (message, opts, fn) {
}

// Use prompt()
promptly.prompt(message, opts, fn);
return promptly.prompt(message, opts, fn);
};

promptly.confirm = function (message, opts, fn) {
Expand Down Expand Up @@ -143,7 +159,7 @@ promptly.confirm = function (message, opts, fn) {
opts.validator.push(validator);

// Use choose() with true, false
promptly.choose(message, [true, false], opts, fn);
return promptly.choose(message, [true, false], opts, fn);
};

promptly.choose = function (message, choices, opts, fn) {
Expand Down Expand Up @@ -176,5 +192,5 @@ promptly.choose = function (message, choices, opts, fn) {
opts.validator.push(validator);

// Use prompt()
promptly.prompt(message, opts, fn);
return promptly.prompt(message, opts, fn);
};
61 changes: 61 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ describe('prompt()', function () {

sendLine('yeaa');
});

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');
});
});

describe('choose()', function () {
Expand Down Expand Up @@ -239,6 +254,21 @@ describe('choose()', function () {

sendLine('1');
});

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');
});
});

describe('confirm()', function () {
Expand Down Expand Up @@ -289,6 +319,21 @@ describe('confirm()', function () {

sendLine('bleh');
});

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');
});
});

describe('password()', function () {
Expand Down Expand Up @@ -326,4 +371,20 @@ describe('password()', function () {

sendLine('');
});

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');
});
});

0 comments on commit e52baa1

Please sign in to comment.