You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.7 KiB

Add `now billing` and `now upgrade` (#309) * Add the skeleton of `now cc` * Add the `ls` command * Add `inquirer` dependency * Add the `set-default` command * Fix typo * Show the real number of cards when selecting the default one * Add the `ls` command * Fix: Do not throw if there's no cards in the account * Add `blessed` dependency * Add the first sketch of `now cc add` * Add instructions * Add labels * Save every element in the `elements` array instead of variables * Tweaks * Fix: update the element attribute if it's not a special case * Add the `name` input; Add moving between inputs; Make the state more reliable * Auto "detect" if the input is losing focus * Remove useless stuff * Add the ability to move between the fields with tab/shift+tab * Add CCV field * Make the cycling between the fields "infinite" * Add expiration date field and allow only numbers in the CCV field * The form shouldn't have a fixed height * Add the address box and label * Add the address fields * Remove blessed stuff * Add preliminary input field * output utils * add prompt for booleans * fix @matheuss linting problems * remove example * lint * error and info helpers * helper for embedded commands * Remove useless stuff * Add `trailing` option * Add `resolveChars` option * Add `validate` option * Add `strip-ansi` dependency * Add `credit-card` dependency * Add credit card masking * Add support for expiration date mask * Make things simpler * Add auto completion support * Always show the `card_` id prefix * Add `@google/maps` dependency * Always print the initial value if it's available * Add `stripe` dependency * Add `add()` method * Add billing related utils * Add `now cc add` * Rename `cc` to `billing` * Fix: log only one blank line * Refactor * Add list input component * This shouldn't be here * Add `code` output util * Add `now upgrade | downgrade` * add build step * make it more future-proof * more reliable build * remove lock for now * Hide the CCV * Print the new line before `Saving card` * Use the new `success` component * Add confirmation steps for `cc rm` and `cc set-default` * Temporarily monket patch Inquirer * Build before testing * Run the tests using the built files * Fix the `prepublish` script and run the `build` one before packaging * Improve `now help`
8 years ago
const ansiEscapes = require('ansi-escapes')
const ansiRegex = require('ansi-regex')
const chalk = require('chalk')
const stripAnsi = require('strip-ansi')
const ESCAPES = {
LEFT: '\x1b[D',
RIGHT: '\x1b[C',
CTRL_C: '\x03',
BACKSPACE: '\x08',
CTRL_H: '\x7f',
CARRIAGE: '\r'
}
module.exports = function ({
label = '',
initialValue = '',
// can be:
// - `false`, which does nothing
// - `cc`, for credit cards
// - `date`, for dates in the mm / yyyy format
mask = false,
placeholder = '',
abortSequences = new Set(['\x03']),
eraseSequences = new Set([ESCAPES.BACKSPACE, ESCAPES.CTRL_H]),
resolveChars = new Set([ESCAPES.CARRIAGE]),
stdin = process.stdin,
stdout = process.stdout,
// char to print before resolving/rejecting the promise
// if `false`, nothing will be printed
trailing = ansiEscapes.eraseLines(1),
// gets called on each keypress;
// `data` contains the current keypress;
// `futureValue` contains the current value + the
// keypress in the correct place
validateKeypress = (data, futureValue) => true, // eslint-disable-line no-unused-vars
// get's called before the promise is resolved
// returning `false` here will prevent the user from submiting the value
validateValue = data => true, // eslint-disable-line no-unused-vars
// receives the value of the input and should return a string
// or false if no autocomplion is available
autoComplete = value => false, // eslint-disable-line no-unused-vars
autoCompleteChars = new Set([
'\t', // tab
'\x1b[C' // right arrow
])
} = {}) {
return new Promise((resolve, reject) => {
const isRaw = process.stdin.isRaw
let value
let caretOffset = 0
let regex
let suggestion = ''
stdout.write(label)
value = initialValue
stdout.write(initialValue)
if (mask) {
if (!value) {
value = chalk.gray(placeholder)
caretOffset = 0 - stripAnsi(value).length
stdout.write(value)
stdout.write(ansiEscapes.cursorBackward(Math.abs(caretOffset)))
}
regex = placeholder.split('').reduce((prev, curr) => {
if (curr !== ' ' && !prev.includes(curr)) {
if (curr === '/') {
prev.push(' / ')
} else {
prev.push(curr)
}
}
return prev
}, []).join('|')
regex = new RegExp(`(${regex})`, 'g')
}
stdin.setRawMode(true)
stdin.resume()
function restore() {
stdin.setRawMode(isRaw)
stdin.pause()
stdin.removeListener('data', onData)
if (trailing) {
stdout.write(trailing)
}
}
async function onData(buffer) {
const data = buffer.toString()
value = stripAnsi(value)
if (abortSequences.has(data)) {
restore()
return reject(new Error('USER_ABORT'))
}
if (suggestion !== '' && !caretOffset && autoCompleteChars.has(data)) {
value += stripAnsi(suggestion)
suggestion = ''
} else if (data === ESCAPES.LEFT) {
if (value.length > Math.abs(caretOffset)) {
caretOffset--
}
} else if (data === ESCAPES.RIGHT) {
if (caretOffset < 0) {
caretOffset++
}
} else if (eraseSequences.has(data)) {
let char
if ((mask) && value.length > Math.abs(caretOffset)) {
if (value[value.length + caretOffset - 1] === ' ') {
if (value[value.length + caretOffset - 2] === '/') {
caretOffset -= 1
}
char = placeholder[value.length + caretOffset]
value = value.substr(0, value.length + caretOffset - 2) + char +
value.substr(value.length + caretOffset - 1)
caretOffset--
} else {
char = placeholder[value.length + caretOffset - 1]
value = value.substr(0, value.length + caretOffset - 1) + char +
value.substr(value.length + caretOffset)
}
caretOffset--
} else {
value = value.substr(0, value.length + caretOffset - 1) +
value.substr(value.length + caretOffset)
}
suggestion = ''
} else if (resolveChars.has(data)) {
if (validateValue(value)) {
restore()
resolve(value)
} else {
if (mask === 'cc' || mask === 'ccv') {
value = value.replace(/\s/g, '').replace(/(.{4})/g, '$1 ').trim()
value = value.replace(regex, chalk.gray('$1'))
} else if (mask === 'expDate') {
value = value.replace(regex, chalk.gray('$1'))
}
const l = chalk.red(label.replace('-', '✖'))
stdout.write(ansiEscapes.eraseLines(1) + l + value + ansiEscapes.beep)
if (caretOffset) {
process.stdout.write(ansiEscapes.cursorBackward(Math.abs(caretOffset)))
}
}
return
} else if (!ansiRegex().test(data)) {
let tmp = value.substr(0, value.length + caretOffset) + data +
value.substr(value.length + caretOffset)
if (mask) {
if (/\d/.test(data) && caretOffset !== 0) {
if (value[value.length + caretOffset + 1] === ' ') {
tmp = value.substr(0, value.length + caretOffset) + data +
value.substr(value.length + caretOffset + 1)
caretOffset += 2
if (value[value.length + caretOffset] === '/') {
caretOffset += 2
}
} else {
tmp = value.substr(0, value.length + caretOffset) + data +
value.substr(value.length + caretOffset + 1)
caretOffset++
}
} else if (/\s/.test(data) && caretOffset < 0) {
caretOffset++
tmp = value
} else {
return stdout.write(ansiEscapes.beep)
}
value = tmp
} else if (validateKeypress(data, value)) {
value = tmp
if (caretOffset === 0) {
const completion = await autoComplete(value)
if (completion) {
suggestion = chalk.gray(completion)
suggestion += ansiEscapes.cursorBackward(completion.length)
} else {
suggestion = ''
}
}
} else {
return stdout.write(ansiEscapes.beep)
}
}
if (mask === 'cc' || mask === 'ccv') {
value = value.replace(/\s/g, '').replace(/(.{4})/g, '$1 ').trim()
value = value.replace(regex, chalk.gray('$1'))
} else if (mask === 'expDate') {
value = value.replace(regex, chalk.gray('$1'))
}
stdout.write(ansiEscapes.eraseLines(1) + label + value + suggestion)
if (caretOffset) {
process.stdout.write(ansiEscapes.cursorBackward(Math.abs(caretOffset)))
}
}
stdin.on('data', onData)
})
}