1 | 'use strict';
|
2 |
|
3 | var extend = require('extend-shallow');
|
4 |
|
5 | module.exports = function(app, options, cb) {
|
6 | var opts = extend({}, options);
|
7 | var inquirer = require('inquirer');
|
8 | inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
|
9 |
|
10 | var question = {
|
11 | name: 'tasks',
|
12 | type: 'autocomplete',
|
13 | message: opts.message,
|
14 | source: listTasks(opts)
|
15 | };
|
16 |
|
17 | return inquirer.prompt([question])
|
18 | .then(function(answers) {
|
19 | return answers && answers.tasks;
|
20 | });
|
21 | };
|
22 |
|
23 | function listTasks(options) {
|
24 | return function(answers, str) {
|
25 | return new Promise(function(resolve) {
|
26 | resolve(options.choices.filter(filter(str, options)));
|
27 | });
|
28 | };
|
29 | }
|
30 |
|
31 | function filter(str, options) {
|
32 | return function(choice, choices) {
|
33 | if (typeof options.filter === 'function') {
|
34 | return options.filter(str, choice, choices);
|
35 | }
|
36 | return new RegExp(str, 'i').test(choice.name[0]);
|
37 | };
|
38 | }
|