forked from vercel/email-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (116 loc) · 3.56 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const ansi = require('ansi-escapes');
const chalk = require('chalk');
module.exports = exports.default = function emailPropt({
start = '> Enter your email: ',
domains = new Set([
'aol.com',
'gmail.com',
'google.com',
'yahoo.com',
'ymail.com',
'hotmail.com',
'live.com',
'outlook.com',
'inbox.com',
'mail.com',
'gmx.com'
]),
forceLowerCase = true,
suggestionColor = 'gray',
autoCompleteChars = new Set([
'\t' /* tab */,
'\r' /* return */,
'\u001b[C' /* right arrow */,
' ' /* spacebar */
]),
resolveChars = new Set(['\r']),
abortChars = new Set(['\u0003']),
allowInvalidChars = false
} = {}) {
return new Promise((resolve, reject) => {
const isRaw = process.stdin.isRaw;
const isPaused = process.stdin.isPaused();
process.stdin.write(start);
process.stdin.setRawMode(true);
process.stdin.resume();
let val = '';
let suggestion = '';
let caretOffset = 0;
// to make `for..of` work with buble
const _domains = Array.from(domains);
const ondata = (v) => {
const s = v.toString();
// abort upon ctrl+C
if (abortChars.has(s)) {
restore();
return reject(new Error('User abort'));
}
let completion = '';
// if we have a suggestion *and*
// the user is at the end of the line *and*
// the user pressed one of the keys to trigger completion
if (suggestion !== '' && !caretOffset && autoCompleteChars.has(s)) {
val += suggestion;
suggestion = '';
} else {
if ('\u001b[D' === s) {
if (val.length > Math.abs(caretOffset)) {
caretOffset--;
}
} else if ('\u001b[C' === s) {
if (caretOffset < 0) {
caretOffset++;
}
} else if ('\x08' === s || '\x7f' === s) {
// delete key needs splicing according to caret position
val = val.substr(0, val.length + caretOffset - 1) +
val.substr(val.length + caretOffset);
} else {
if (resolveChars.has(s)) {
restore();
return resolve(val);
}
if (!allowInvalidChars) {
// disallow more than one @
if (/@/.test(val) && '@' === s) {
return;
}
if (/[^A-z0-9-+_.@]/.test(s)) {
return;
}
}
const add = forceLowerCase ? s.toLowerCase() : s;
val = val.substr(0, val.length + caretOffset) + add +
val.substr(val.length + caretOffset);
}
const parts = val.split('@');
if (2 === parts.length && parts[1].length) {
const [, _host] = parts;
const host = _host.toLowerCase();
for (const domain of _domains) {
if (host === domain) {
break;
}
if (host === domain.substr(0, host.length)) {
suggestion = domain.substr(host.length);
completion = chalk[suggestionColor](suggestion);
completion += ansi.cursorBackward(domain.length - host.length);
break;
}
}
}
if (!completion.length) suggestion = '';
}
process.stdout.write(ansi.eraseLines(1) + start + val + completion);
if (caretOffset) {
process.stdout.write(ansi.cursorBackward(Math.abs(caretOffset)));
}
};
const restore = () => {
process.stdin.setRawMode(isRaw);
process.stdin.pause();
process.stdin.removeListener('data', ondata);
};
process.stdin.on('data', ondata);
});
}