Skip to content

Commit e52baa1

Browse files
authored
Merge pull request #12 from riskawarrior/feature/promises
Introduction of Promises
2 parents a5c0670 + 01338bf commit e52baa1

File tree

4 files changed

+112
-23
lines changed

4 files changed

+112
-23
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- "0.10"
43
- "0.12"
54
- "4"
65
- "5"
6+
- "6"

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ Simple command line prompting utility.
2222

2323
## API
2424

25-
### .prompt(message, [opts], fn)
25+
### .prompt(message, [opts], [fn])
2626

2727
Prompts for a value, printing the `message` and waiting for the input.
28-
When done, calls `fn` with `error` and `value`.
28+
When done, calls `fn` with `error` and `value`. Or returns with `Promise` if no `fn` is provided.
2929

3030
Default options:
3131
```js
@@ -47,6 +47,7 @@ Default options:
4747
```
4848

4949
The validators have two purposes:
50+
5051
```js
5152
function (value) {
5253
// Validation example, throwing an error when invalid
@@ -62,13 +63,24 @@ function (value) {
6263
Example usages
6364

6465
Ask for a name:
66+
6567
```js
6668
promptly.prompt('Name: ', function (err, value) {
6769
// err is always null in this case, because no validators are set
6870
console.log(value);
6971
});
7072
```
7173

74+
Using Promise:
75+
76+
```js
77+
promptly.prompt('Name: ')
78+
.then(function (value) {
79+
// no need for catch in this case, because no validators are set
80+
console.log(value);
81+
})
82+
```
83+
7284
Ask for a name with a constraint (non-empty value and length > 2):
7385

7486
```js
@@ -101,7 +113,7 @@ var validator = function (value) {
101113

102114
promptly.prompt('Name: ', { validator: validator, retry: false }, function (err, value) {
103115
if (err) {
104-
console.error('Invalid name:', e.message);
116+
console.error('Invalid name:', err.message);
105117
// Manually call retry
106118
// The passed error has a retry method to easily prompt again.
107119
return err.retry();

index.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,7 @@
33
var read = require('read');
44
var promptly = module.exports;
55

6-
promptly.prompt = function (message, opts, fn) {
7-
// Arguments parsing
8-
if (typeof opts === 'function') {
9-
fn = opts;
10-
opts = {};
11-
} else if (!opts) {
12-
opts = {};
13-
}
14-
15-
if (opts.trim === undefined) {
16-
opts.trim = true;
17-
}
18-
if (opts.retry === undefined) {
19-
opts.retry = true;
20-
}
21-
6+
function prompt(message, opts, fn) {
227
// Setup read's options
238
var readOpts = {
249
prompt: message,
@@ -79,6 +64,37 @@ promptly.prompt = function (message, opts, fn) {
7964
// Everything ok
8065
fn(null, data);
8166
});
67+
}
68+
69+
promptly.prompt = function (message, opts, fn) {
70+
// Arguments parsing
71+
if (typeof opts === 'function') {
72+
fn = opts;
73+
opts = {};
74+
} else if (!opts) {
75+
opts = {};
76+
}
77+
78+
if (opts.trim === undefined) {
79+
opts.trim = true;
80+
}
81+
if (opts.retry === undefined) {
82+
opts.retry = true;
83+
}
84+
85+
if (fn) {
86+
return prompt(message, opts, fn);
87+
}
88+
89+
return new Promise(function (resolve, reject) {
90+
prompt(message, opts, function (err, result) {
91+
if (err) {
92+
return reject(err);
93+
}
94+
95+
resolve(result);
96+
});
97+
});
8298
};
8399

84100
promptly.password = function (message, opts, fn) {
@@ -102,7 +118,7 @@ promptly.password = function (message, opts, fn) {
102118
}
103119

104120
// Use prompt()
105-
promptly.prompt(message, opts, fn);
121+
return promptly.prompt(message, opts, fn);
106122
};
107123

108124
promptly.confirm = function (message, opts, fn) {
@@ -143,7 +159,7 @@ promptly.confirm = function (message, opts, fn) {
143159
opts.validator.push(validator);
144160

145161
// Use choose() with true, false
146-
promptly.choose(message, [true, false], opts, fn);
162+
return promptly.choose(message, [true, false], opts, fn);
147163
};
148164

149165
promptly.choose = function (message, choices, opts, fn) {
@@ -176,5 +192,5 @@ promptly.choose = function (message, choices, opts, fn) {
176192
opts.validator.push(validator);
177193

178194
// Use prompt()
179-
promptly.prompt(message, opts, fn);
195+
return promptly.prompt(message, opts, fn);
180196
};

test/test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@ describe('prompt()', function () {
189189

190190
sendLine('yeaa');
191191
});
192+
193+
it('should prompt the user (using promise)', function (next) {
194+
promptly.prompt('something: ')
195+
.then(function (value) {
196+
expect(value).to.be('yeaa');
197+
expect(stdout).to.contain('something: ');
198+
next();
199+
})
200+
.catch(function () {
201+
expect().fail();
202+
next();
203+
});
204+
205+
sendLine('yeaa');
206+
});
192207
});
193208

194209
describe('choose()', function () {
@@ -239,6 +254,21 @@ describe('choose()', function () {
239254

240255
sendLine('1');
241256
});
257+
258+
it('should work using promise', function (next) {
259+
promptly.choose('apple or orange? ', ['apple', 'orange'])
260+
.then(function (value) {
261+
expect(value).to.be('orange');
262+
expect(stdout).to.contain('apple or orange? ');
263+
next();
264+
})
265+
.catch(function () {
266+
expect().fail();
267+
next();
268+
});
269+
270+
sendLine('orange');
271+
});
242272
});
243273

244274
describe('confirm()', function () {
@@ -289,6 +319,21 @@ describe('confirm()', function () {
289319

290320
sendLine('bleh');
291321
});
322+
323+
it('should work using promise', function (next) {
324+
promptly.confirm('yes or no? ')
325+
.then(function (value) {
326+
expect(stdout).to.contain('yes or no? ');
327+
expect(value).to.be(true);
328+
next();
329+
})
330+
.catch(function () {
331+
expect().fail();
332+
next();
333+
});
334+
335+
sendLine('y');
336+
});
292337
});
293338

294339
describe('password()', function () {
@@ -326,4 +371,20 @@ describe('password()', function () {
326371

327372
sendLine('');
328373
});
374+
375+
it('should prompt the user silently using promise', function (next) {
376+
promptly.password('something: ')
377+
.then(function (value) {
378+
expect(value).to.be('yeaa');
379+
expect(stdout).to.contain('something: ');
380+
expect(stdout).to.not.contain('yeaa');
381+
next();
382+
})
383+
.catch(function () {
384+
expect().fail();
385+
next();
386+
});
387+
388+
sendLine('yeaa');
389+
});
329390
});

0 commit comments

Comments
 (0)