-
Notifications
You must be signed in to change notification settings - Fork 9
/
view.js
104 lines (96 loc) · 2.45 KB
/
view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Promise = require('bluebird');
const util = require('util');
const inquirer = require('inquirer');
const prompt = require('prompt');
function inquireForInput(message, complete) {
inquirer.prompt(
[
{
type: 'input',
name: 'input',
message
}
],
function (response) {
confirmInquire(response.input, function (err, confirmed) {
if (err) return inquireForInput(message, complete);
complete(null, confirmed.input);
});
}
);
}
module.exports.inquireForInput = inquireForInput;
function confirmInquire(input, complete) {
inquirer.prompt(
[
{
type: 'confirm',
name: 'confirmed',
message: `Your input was '${input}': All good?`,
default: true
}
],
function (response) {
if (!response.confirmed) {
return complete({ response: 'unconfirmed', input });
}
return complete(null, { input });
}
);
}
function promptForInput(message, validator, invalidMessage) {
return prmpt(message, validator, invalidMessage)
.then(confirm)
.catch(UserCancelledError, function () {
return promptForInput(message, validator, invalidMessage);
});
}
module.exports.promptForInput = promptForInput;
function prmpt(message, validator, invalidMessage) {
prompt.message = '';
prompt.delimiter = '';
prompt.start();
const schema = {
properties: {
input: {
description: message,
pattern: validator,
message: invalidMessage,
required: true
}
}
};
return new Promise(function (resolve, reject) {
prompt.get(schema, function (err, result) {
if (err) return reject(err);
resolve(result.input);
});
});
}
function confirm(input) {
const schema = {
properties: {
confirm: {
description: util.format('Your input was %s: All good? (Y/n)', input),
pattern: /Y|n/,
message: 'Enter capital Y for yes, lowercase n for no',
required: true
}
}
};
return new Promise(function (resolve, reject) {
prompt.get(schema, function (err, result) {
if (err) return reject(err);
if (result.confirm === 'Y') {
resolve(input);
} else {
reject(new UserCancelledError());
}
});
});
}
function UserCancelledError(message) {
this.name = 'UserCancelledError';
this.message = message || 'The user cancelled the input operation';
}
UserCancelledError.prototype = new Error();