-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
144 lines (126 loc) · 3.76 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
134
135
136
137
138
139
140
141
142
143
144
const ansi = require('ansi-escapes')
const chalk = require('chalk')
// eslint-disable-next-line no-multi-assign
module.exports = exports.default = function emailPrompt({
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',
'icloud.com',
'zeit.co',
'vercel.com',
'hey.com'
]),
forceLowerCase = true,
suggestionColor = 'gray',
autoCompleteChars = new Set([
'\t' /* tab */,
'\r' /* return */,
'\x1b[C' /* right arrow */,
' ' /* Spacebar */
]),
resolveChars = new Set(['\r']),
abortChars = new Set(['\x03']),
allowInvalidChars = false
} = {}) {
return new Promise((resolve, reject) => {
// Some environments (e.g., cygwin) don't provide a tty
if (!process.stdin.setRawMode) {
return reject(new Error('stdin lacks setRawMode support'))
}
const isRaw = process.stdin.isRaw
process.stdout.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 (s === '\x1b[D') {
if (val.length > Math.abs(caretOffset)) {
caretOffset--
}
} else if (s === '\x1b[C') {
if (caretOffset < 0) {
caretOffset++
}
} else if (s === '\x08' || s === '\x7f') {
// 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 (parts.length === 2 && parts[1].length > 0) {
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 === 0) {
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)
})
}