Browse Source

Perfected code structure for .sh

master
Leo Lamprecht 7 years ago
parent
commit
f6685729ae
  1. 8
      src/providers/sh/commands/alias.js
  2. 10
      src/providers/sh/commands/billing.js
  3. 16
      src/providers/sh/commands/billing/add.js
  4. 4
      src/providers/sh/commands/certs.js
  5. 4
      src/providers/sh/commands/dns.js
  6. 6
      src/providers/sh/commands/domains.js
  7. 20
      src/providers/sh/commands/domains/buy.js
  8. 2
      src/providers/sh/commands/list.js
  9. 2
      src/providers/sh/commands/logout.js
  10. 4
      src/providers/sh/commands/logs.js
  11. 4
      src/providers/sh/commands/remove.js
  12. 6
      src/providers/sh/commands/scale.js
  13. 4
      src/providers/sh/commands/secrets.js
  14. 6
      src/providers/sh/commands/teams.js
  15. 26
      src/providers/sh/commands/teams/add.js
  16. 26
      src/providers/sh/commands/teams/invite.js
  17. 10
      src/providers/sh/commands/teams/list.js
  18. 14
      src/providers/sh/commands/teams/switch.js
  19. 12
      src/providers/sh/commands/upgrade.js
  20. 2
      src/providers/sh/commands/whoami.js
  21. 22
      src/providers/sh/util/alias.js
  22. 0
      src/providers/sh/util/billing/card-brands.json
  23. 0
      src/providers/sh/util/billing/country-list.json
  24. 0
      src/providers/sh/util/billing/geocode.js
  25. 2
      src/providers/sh/util/domains.js
  26. 2
      src/providers/sh/util/domains/treat-buy-error.js
  27. 79
      src/providers/sh/util/etc/input/list.js
  28. 20
      src/providers/sh/util/etc/input/patch-inquirer.js
  29. 58
      src/providers/sh/util/etc/input/prompt-bool.js
  30. 262
      src/providers/sh/util/etc/input/text.js
  31. 3
      src/providers/sh/util/etc/output/chars.js
  32. 6
      src/providers/sh/util/etc/output/cmd.js
  33. 3
      src/providers/sh/util/etc/output/erase-lines.js
  34. 9
      src/providers/sh/util/etc/output/error.js
  35. 6
      src/providers/sh/util/etc/output/info.js
  36. 1
      src/providers/sh/util/etc/output/logo.js
  37. 6
      src/providers/sh/util/etc/output/note.js
  38. 7
      src/providers/sh/util/etc/output/param.js
  39. 6
      src/providers/sh/util/etc/output/success.js
  40. 15
      src/providers/sh/util/etc/output/wait.js
  41. 0
      src/util/check-path.js
  42. 0
      src/util/exit.js
  43. 0
      src/util/fatal-error.js
  44. 0
      src/util/input/prompt-options.js
  45. 0
      src/util/input/regexes.js
  46. 1
      src/util/output/code.js
  47. 0
      src/util/output/right-pad.js
  48. 0
      src/util/output/stamp.js
  49. 0
      src/util/output/table.js
  50. 0
      src/util/output/uid.js
  51. 0
      src/util/to-human-path.js
  52. 0
      src/util/url.js

8
src/providers/sh/commands/alias.js

@ -16,10 +16,10 @@ const NowDomains = require('../util/domains')
const { handleError, error } = require('../util/error')
const toHost = require('../util/to-host')
const { reAlias } = require('../util/re-alias')
const exit = require('../util/etc/exit')
const info = require('../util/etc/output/info')
const logo = require('../util/etc/output/logo')
const promptBool = require('../util/etc/input/prompt-bool')
const exit = require('../../../util/exit')
const info = require('../../../util/output/info')
const logo = require('../../../util/output/logo')
const promptBool = require('../../../util/input/prompt-bool')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

10
src/providers/sh/commands/billing.js

@ -9,11 +9,11 @@ const ms = require('ms')
const { handleError, error } = require('../util/error')
const NowCreditCards = require('../util/credit-cards')
const indent = require('../util/indent')
const listInput = require('../util/etc/input/list')
const success = require('../util/etc/output/success')
const promptBool = require('../util/etc/input/prompt-bool')
const info = require('../util/etc/output/info')
const logo = require('../util/etc/output/logo')
const listInput = require('../../../util/input/list')
const success = require('../../../util/output/success')
const promptBool = require('../../../util/input/prompt-bool')
const info = require('../../../util/output/info')
const logo = require('../../../util/output/logo')
const addBilling = require('./billing/add')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

16
src/providers/sh/commands/billing/add.js

@ -6,14 +6,14 @@ const chalk = require('chalk')
const ccValidator = require('credit-card')
// Utilities
const textInput = require('../../util/etc/input/text')
const countries = require('../../util/etc/billing/country-list')
const cardBrands = require('../../util/etc/billing/card-brands')
const geocode = require('../../util/etc/billing/geocode')
const success = require('../../util/etc/output/success')
const wait = require('../../util/etc/output/wait')
const { tick } = require('../../util/etc/output/chars')
const rightPad = require('../../util/etc/output/right-pad')
const textInput = require('../../../../util/input/text')
const countries = require('../../util/billing/country-list')
const cardBrands = require('../../util/billing/card-brands')
const geocode = require('../../util/billing/geocode')
const success = require('../../../../util/output/success')
const wait = require('../../../../util/output/wait')
const { tick } = require('../../../../util/output/chars')
const rightPad = require('../../../../util/output/right-pad')
function expDateMiddleware(data) {
return data

4
src/providers/sh/commands/certs.js

@ -16,8 +16,8 @@ const supportsColor = require('supports-color')
// Utilities
const { handleError, error } = require('../util/error')
const NowCerts = require('../util/certs')
const exit = require('../util/etc/exit')
const logo = require('../util/etc/output/logo')
const exit = require('../../../util/exit')
const logo = require('../../../util/output/logo')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

4
src/providers/sh/commands/dns.js

@ -11,8 +11,8 @@ const DomainRecords = require('../util/domain-records')
const indent = require('../util/indent')
const strlen = require('../util/strlen')
const { handleError, error } = require('../util/error')
const exit = require('../util/etc/exit')
const logo = require('../util/etc/output/logo')
const exit = require('../../../util/exit')
const logo = require('../../../util/output/logo')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

6
src/providers/sh/commands/domains.js

@ -9,9 +9,9 @@ const table = require('text-table')
// Utilities
const NowDomains = require('../util/domains')
const exit = require('../util/etc/exit')
const logo = require('../util/etc/output/logo')
const promptBool = require('../util/etc/input/prompt-bool')
const exit = require('../../../util/exit')
const logo = require('../../../util/output/logo')
const promptBool = require('../../../util/input/prompt-bool')
const strlen = require('../util/strlen')
const toHost = require('../util/to-host')
const { handleError, error } = require('../util/error')

20
src/providers/sh/commands/domains/buy.js

@ -2,16 +2,16 @@
const { italic, bold } = require('chalk')
// Utilities
const error = require('../../util/etc/output/error')
const wait = require('../../util/etc/output/wait')
const cmd = require('../../util/etc/output/cmd')
const param = require('../../util/etc/output/param')
const info = require('../../util/etc/output/info')
const success = require('../../util/etc/output/success')
const stamp = require('../../util/etc/output/stamp')
const promptBool = require('../../util/etc/input/prompt-bool')
const eraseLines = require('../../util/etc/output/erase-lines')
const treatBuyError = require('../../util/etc/domains/treat-buy-error')
const error = require('../../../../util/output/error')
const wait = require('../../../../util/output/wait')
const cmd = require('../../../../util/output/cmd')
const param = require('../../../../util/output/param')
const info = require('../../../../util/output/info')
const success = require('../../../../util/output/success')
const stamp = require('../../../../util/output/stamp')
const promptBool = require('../../../../util/input/prompt-bool')
const eraseLines = require('../../../../util/output/erase-lines')
const treatBuyError = require('../../util/domains/treat-buy-error')
const NowCreditCards = require('../../util/credit-cards')
const addBilling = require('../billing/add')

2
src/providers/sh/commands/list.js

@ -11,7 +11,7 @@ const supportsColor = require('supports-color')
// Utilities
const Now = require('../util')
const { handleError, error } = require('../util/error')
const logo = require('../util/etc/output/logo')
const logo = require('../../../util/output/logo')
const sort = require('../util/sort-deployments')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

2
src/providers/sh/commands/logout.js

@ -7,7 +7,7 @@ const fetch = require('node-fetch')
const ora = require('ora')
// Utilities
const logo = require('../util/etc/output/logo')
const logo = require('../../../util/output/logo')
const { handleError } = require('../util/error')
const {
readConfigFile,

4
src/providers/sh/commands/logs.js

@ -12,9 +12,9 @@ const io = require('socket.io-client')
// Utilities
const Now = require('../util')
const { handleError, error } = require('../util/error')
const logo = require('../util/etc/output/logo')
const logo = require('../../../util/output/logo')
const { compare, deserialize } = require('../util/logs')
const { maybeURL, normalizeURL, parseInstanceURL } = require('../util/etc/url')
const { maybeURL, normalizeURL, parseInstanceURL } = require('../../../util/url')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

4
src/providers/sh/commands/remove.js

@ -9,8 +9,8 @@ const table = require('text-table')
// Utilities
const Now = require('../util')
const { handleError, error } = require('../util/error')
const logo = require('../util/etc/output/logo')
const { normalizeURL } = require('../util/etc/url')
const logo = require('../../../util/output/logo')
const { normalizeURL } = require('../../../util/url')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

6
src/providers/sh/commands/scale.js

@ -12,11 +12,11 @@ const supportsColor = require('supports-color')
// Utilities
const { handleError, error } = require('../util/error')
const NowScale = require('../util/scale')
const exit = require('../util/etc/exit')
const logo = require('../util/etc/output/logo')
const exit = require('../../../util/exit')
const logo = require('../../../util/output/logo')
const info = require('../util/scale-info')
const sort = require('../util/sort-deployments')
const success = require('../util/etc/output/success')
const success = require('../../../util/output/success')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

4
src/providers/sh/commands/secrets.js

@ -10,8 +10,8 @@ const ms = require('ms')
const strlen = require('../util/strlen')
const { handleError, error } = require('../util/error')
const NowSecrets = require('../util/secrets')
const exit = require('../util/etc/exit')
const logo = require('../util/etc/output/logo')
const exit = require('../../../util/exit')
const logo = require('../../../util/output/logo')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

6
src/providers/sh/commands/teams.js

@ -5,10 +5,10 @@ const chalk = require('chalk')
const minimist = require('minimist')
// Utilities
const error = require('../util/etc/output/error')
const error = require('../../../util/output/error')
const NowTeams = require('../util/teams')
const logo = require('../util/etc/output/logo')
const exit = require('../util/etc/exit')
const logo = require('../../../util/output/logo')
const exit = require('../../../util/exit')
const { handleError } = require('../util/error')
const list = require('./teams/list')
const add = require('./teams/add')

26
src/providers/sh/commands/teams/add.js

@ -2,19 +2,19 @@
const chalk = require('chalk')
// Utilities
const stamp = require('../../util/etc/output/stamp')
const info = require('../../util/etc/output/info')
const error = require('../../util/etc/output/error')
const wait = require('../../util/etc/output/wait')
const rightPad = require('../../util/etc/output/right-pad')
const eraseLines = require('../../util/etc/output/erase-lines')
const { tick } = require('../../util/etc/output/chars')
const success = require('../../util/etc/output/success')
const cmd = require('../../util/etc/output/cmd')
const note = require('../../util/etc/output/note')
const uid = require('../../util/etc/output/uid')
const textInput = require('../../util/etc/input/text')
const exit = require('../../util/etc/exit')
const stamp = require('../../../../util/output/stamp')
const info = require('../../../../util/output/info')
const error = require('../../../../util/output/error')
const wait = require('../../../../util/output/wait')
const rightPad = require('../../../../util/output/right-pad')
const eraseLines = require('../../../../util/output/erase-lines')
const { tick } = require('../../../../util/output/chars')
const success = require('../../../../util/output/success')
const cmd = require('../../../../util/output/cmd')
const note = require('../../../../util/output/note')
const uid = require('../../../../util/output/uid')
const textInput = require('../../../../util/input/text')
const exit = require('../../../../util/exit')
const cfg = require('../../util/cfg')
const invite = require('./invite')

26
src/providers/sh/commands/teams/invite.js

@ -2,20 +2,20 @@
const chalk = require('chalk')
// Utilities
const regexes = require('../../util/etc/input/regexes')
const wait = require('../../util/etc/output/wait')
const regexes = require('../../../../util/input/regexes')
const wait = require('../../../../util/output/wait')
const cfg = require('../../util/cfg')
const fatalError = require('../../util/etc/fatal-error')
const cmd = require('../../util/etc/output/cmd')
const info = require('../../util/etc/output/info')
const stamp = require('../../util/etc/output/stamp')
const param = require('../../util/etc/output/param')
const { tick } = require('../../util/etc/output/chars')
const rightPad = require('../../util/etc/output/right-pad')
const textInput = require('../../util/etc/input/text')
const eraseLines = require('../../util/etc/output/erase-lines')
const success = require('../../util/etc/output/success')
const error = require('../../util/etc/output/error')
const fatalError = require('../../../../util/fatal-error')
const cmd = require('../../../../util/output/cmd')
const info = require('../../../../util/output/info')
const stamp = require('../../../../util/output/stamp')
const param = require('../../../../util/output/param')
const { tick } = require('../../../../util/output/chars')
const rightPad = require('../../../../util/output/right-pad')
const textInput = require('../../../../util/input/text')
const eraseLines = require('../../../../util/output/erase-lines')
const success = require('../../../../util/output/success')
const error = require('../../../../util/output/error')
function validateEmail(data) {
return regexes.email.test(data.trim()) || data.length === 0

10
src/providers/sh/commands/teams/list.js

@ -2,12 +2,12 @@
const chalk = require('chalk')
// Utilities
const wait = require('../../util/etc/output/wait')
const wait = require('../../../../util/output/wait')
const cfg = require('../../util/cfg')
const info = require('../../util/etc/output/info')
const error = require('../../util/etc/output/error')
const { tick: tickChar } = require('../../util/etc/output/chars')
const table = require('../../util/etc/output/table')
const info = require('../../../../util/output/info')
const error = require('../../../../util/output/error')
const { tick: tickChar } = require('../../../../util/output/chars')
const table = require('../../../../util/output/table')
module.exports = async function({ teams, token }) {
const stopSpinner = wait('Fetching teams')

14
src/providers/sh/commands/teams/switch.js

@ -2,14 +2,14 @@
const chalk = require('chalk')
// Utilities
const wait = require('../../util/etc/output/wait')
const listInput = require('../../util/etc/input/list')
const wait = require('../../../../util/output/wait')
const listInput = require('../../../../util/input/list')
const cfg = require('../../util/cfg')
const exit = require('../../util/etc/exit')
const success = require('../../util/etc/output/success')
const info = require('../../util/etc/output/info')
const error = require('../../util/etc/output/error')
const param = require('../../util/etc/output/param')
const exit = require('../../../../util/exit')
const success = require('../../../../util/output/success')
const info = require('../../../../util/output/info')
const error = require('../../../../util/output/error')
const param = require('../../../../util/output/param')
async function updateCurrentTeam({ cfg, newTeam } = {}) {
delete newTeam.created

12
src/providers/sh/commands/upgrade.js

@ -8,12 +8,12 @@ const ms = require('ms')
// Utilities
const NowPlans = require('../util/plans')
const indent = require('../util/indent')
const listInput = require('../util/etc/input/list')
const code = require('../util/etc/output/code')
const error = require('../util/etc/output/error')
const success = require('../util/etc/output/success')
const cmd = require('../util/etc/output/cmd')
const logo = require('../util/etc/output/logo')
const listInput = require('../../../util/input/list')
const code = require('../../../util/output/code')
const error = require('../../../util/output/error')
const success = require('../../../util/output/success')
const cmd = require('../../../util/output/cmd')
const logo = require('../../../util/output/logo')
const { handleError } = require('../util/error')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

2
src/providers/sh/commands/whoami.js

@ -5,7 +5,7 @@ const minimist = require('minimist')
const chalk = require('chalk')
// Utilities
const logo = require('../util/etc/output/logo')
const logo = require('../../../util/output/logo')
const { handleError } = require('../util/error')
const getWelcome = require('../../../get-welcome')
const providers = require('../../')

22
src/providers/sh/util/alias.js

@ -7,22 +7,22 @@ const chalk = require('chalk')
const { write: copy } = require('clipboardy')
// Ours
const promptBool = require('./etc/input/prompt-bool')
const info = require('./etc/output/info')
const param = require('./etc/output/param')
const wait = require('./etc/output/wait')
const success = require('./etc/output/success')
const uid = require('./etc/output/uid')
const eraseLines = require('./etc/output/erase-lines')
const stamp = require('./etc/output/stamp')
const error = require('./etc/output/error')
const treatBuyError = require('./etc/domains/treat-buy-error')
const promptBool = require('../../../util/input/prompt-bool')
const info = require('../../../util/output/info')
const param = require('../../../util/output/param')
const wait = require('../../../util/output/wait')
const success = require('../../../util/output/success')
const uid = require('../../../util/output/uid')
const eraseLines = require('../../../util/output/erase-lines')
const stamp = require('../../../util/output/stamp')
const error = require('../../../util/output/error')
const treatBuyError = require('../util/domains/treat-buy-error')
const scaleInfo = require('./scale-info')
const { DOMAIN_VERIFICATION_ERROR } = require('./errors')
const isZeitWorld = require('./is-zeit-world')
const resolve4 = require('./dns')
const toHost = require('./to-host')
const exit = require('./etc/exit')
const exit = require('../../../util/exit')
const Now = require('./')
const argv = minimist(process.argv.slice(2), {

0
src/providers/sh/util/etc/billing/card-brands.json → src/providers/sh/util/billing/card-brands.json

0
src/providers/sh/util/etc/billing/country-list.json → src/providers/sh/util/billing/country-list.json

0
src/providers/sh/util/etc/billing/geocode.js → src/providers/sh/util/billing/geocode.js

2
src/providers/sh/util/domains.js

@ -8,7 +8,7 @@ const chalk = require('chalk')
const Now = require('.')
const isZeitWorld = require('./is-zeit-world')
const { DNS_VERIFICATION_ERROR } = require('./errors')
const cmd = require('./etc/output/param')
const cmd = require('../../../util/output/param')
const domainRegex = /^((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}$/

2
src/providers/sh/util/etc/domains/treat-buy-error.js → src/providers/sh/util/domains/treat-buy-error.js

@ -1,4 +1,4 @@
const error = require('../output/error')
const error = require('../../../../util/output/error')
module.exports = function(err) {
switch (err.code) {

79
src/providers/sh/util/etc/input/list.js

@ -1,79 +0,0 @@
const inquirer = require('inquirer')
const stripAnsi = require('strip-ansi')
// eslint-disable-next-line import/no-unassigned-import
require('./patch-inquirer')
function getLength(string) {
let biggestLength = 0
string.split('\n').map(str => {
str = stripAnsi(str)
if (str.length > biggestLength) {
biggestLength = str.length
}
return undefined
})
return biggestLength
}
module.exports = async function({
message = 'the question',
// eslint-disable-line no-unused-vars
choices = [
{
name: 'something\ndescription\ndetails\netc',
value: 'something unique',
short: 'generally the first line of `name`'
}
],
pageSize = 15, // Show 15 lines without scrolling (~4 credit cards)
separator = true, // Puts a blank separator between each choice
abort = 'end' // Wether the `abort` option will be at the `start` or the `end`
}) {
let biggestLength = 0
choices = choices.map(choice => {
if (choice.name) {
const length = getLength(choice.name)
if (length > biggestLength) {
biggestLength = length
}
return choice
}
throw new Error('Invalid choice')
})
if (separator === true) {
choices = choices.reduce(
(prev, curr) => prev.concat(new inquirer.Separator(' '), curr),
[]
)
}
const abortSeparator = new inquirer.Separator('─'.repeat(biggestLength))
const _abort = {
name: 'Abort',
value: undefined
}
if (abort === 'start') {
const blankSep = choices.shift()
choices.unshift(abortSeparator)
choices.unshift(_abort)
choices.unshift(blankSep)
} else {
choices.push(abortSeparator)
choices.push(_abort)
}
const nonce = Date.now()
const answer = await inquirer.prompt({
name: nonce,
type: 'list',
message,
choices,
pageSize
})
return answer[nonce]
}

20
src/providers/sh/util/etc/input/patch-inquirer.js

@ -1,20 +0,0 @@
const inquirer = require('inquirer')
const chalk = require('chalk')
// Here we patch inquirer to use a `>` instead of the ugly green `?`
/* eslint-disable no-multiple-empty-lines, no-var, no-undef, no-eq-null, eqeqeq, semi */
const getQuestion = function() {
var message = chalk.bold('> ' + this.opt.message) + ' '
// Append the default if available, and if question isn't answered
if (this.opt.default != null && this.status !== 'answered') {
message += chalk.dim('(' + this.opt.default + ') ')
}
return message
}
/* eslint-enable */
inquirer.prompt.prompts.input.prototype.getQuestion = getQuestion
inquirer.prompt.prompts.list.prototype.getQuestion = getQuestion

58
src/providers/sh/util/etc/input/prompt-bool.js

@ -1,58 +0,0 @@
const chalk = require('chalk')
module.exports = (
label,
{
defaultValue = false,
abortSequences = new Set(['\u0003']),
resolveChars = new Set(['\r']),
yesChar = 'y',
noChar = 'n',
stdin = process.stdin,
stdout = process.stdout,
trailing = ''
} = {}
) => {
return new Promise(resolve => {
const isRaw = stdin.isRaw
stdin.setRawMode(true)
stdin.resume()
function restore() {
stdout.write(trailing)
stdin.setRawMode(isRaw)
stdin.pause()
stdin.removeListener('data', onData)
}
function onData(buffer) {
const data = buffer.toString()
if (data[0].toLowerCase() === yesChar) {
restore()
resolve(true)
} else if (data[0].toLowerCase() === noChar) {
restore()
resolve(false)
} else if (abortSequences.has(data)) {
restore()
resolve(false)
} else if (resolveChars.has(data[0])) {
restore()
resolve(defaultValue)
} else {
// ignore extraneous input
}
}
const defaultText =
defaultValue === null
? `[${yesChar}|${noChar}]`
: defaultValue
? `[${chalk.bold(yesChar.toUpperCase())}|${noChar}]`
: `[${yesChar}|${chalk.bold(noChar.toUpperCase())}]`
stdout.write(`${chalk.gray('>')} ${label} ${chalk.gray(defaultText)} `)
stdin.on('data', onData)
})
}

262
src/providers/sh/util/etc/input/text.js

@ -1,262 +0,0 @@
// Packages
const ansiEscapes = require('ansi-escapes')
const ansiRegex = require('ansi-regex')
const chalk = require('chalk')
const stripAnsi = require('strip-ansi')
// Utilities
const eraseLines = require('../output/erase-lines')
const ESCAPES = {
LEFT: '\u001B[D',
RIGHT: '\u001B[C',
CTRL_C: '\u0003',
BACKSPACE: '\u0008',
CTRL_H: '\u007F',
CARRIAGE: '\r'
}
const formatCC = data => {
return data.replace(/\s/g, '').replace(/(.{4})/g, '$1 ').trim()
}
module.exports = function(
{
label = '',
initialValue = '',
// If false, the `- label` will be printed as `✖ label` in red
// Until the first keypress
valid = true,
// 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
// Tab
// Right arrow
autoCompleteChars = new Set(['\t', '\x1b[C']),
// If true, converts everything the user types to to lowercase
forceLowerCase = false
} = {}
) {
return new Promise((resolve, reject) => {
const isRaw = process.stdin.isRaw
let value
let caretOffset = 0
let regex
let suggestion = ''
if (valid) {
stdout.write(label)
} else {
const _label = label.replace('-', '✖')
stdout.write(chalk.red(_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) {
let data = buffer.toString()
value = stripAnsi(value)
if (abortSequences.has(data)) {
restore()
return reject(new Error('USER_ABORT'))
}
if (forceLowerCase) {
data = data.toLowerCase()
}
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 = formatCC(value)
value = value.replace(regex, chalk.gray('$1'))
} else if (mask === 'expDate') {
value = value.replace(regex, chalk.gray('$1'))
}
const l = chalk.red(label.replace('-', '✖'))
eraseLines(1)
stdout.write(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) {
let formattedData = data
if (mask === 'cc' || mask === 'ccv') {
formattedData = formatCC(data)
}
if (value[value.length + caretOffset + 1] === ' ') {
tmp =
value.substr(0, value.length + caretOffset) +
formattedData +
value.substr(value.length + caretOffset + formattedData.length)
caretOffset += formattedData.length + 1
if (value[value.length + caretOffset] === '/') {
caretOffset += formattedData.length + 1
}
} else {
tmp =
value.substr(0, value.length + caretOffset) +
formattedData +
value.substr(value.length + caretOffset + formattedData.length)
caretOffset += formattedData.length
}
} 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 = formatCC(value)
value = value.replace(regex, chalk.gray('$1'))
} else if (mask === 'expDate') {
value = value.replace(regex, chalk.gray('$1'))
}
eraseLines(1)
stdout.write(label + value + suggestion)
if (caretOffset) {
process.stdout.write(ansiEscapes.cursorBackward(Math.abs(caretOffset)))
}
}
stdin.on('data', onData)
})
}

3
src/providers/sh/util/etc/output/chars.js

@ -1,3 +0,0 @@
module.exports = {
tick: '✓'
}

6
src/providers/sh/util/etc/output/cmd.js

@ -1,6 +0,0 @@
const chalk = require('chalk')
// The equivalent of <code>, for embedding a cmd
// eg: Please run ${cmd(woot)}
module.exports = cmd => `${chalk.gray('`')}${chalk.cyan(cmd)}${chalk.gray('`')}`

3
src/providers/sh/util/etc/output/erase-lines.js

@ -1,3 +0,0 @@
const ansiEscapes = require('ansi-escapes')
module.exports = n => process.stdout.write(ansiEscapes.eraseLines(n))

9
src/providers/sh/util/etc/output/error.js

@ -1,9 +0,0 @@
const chalk = require('chalk')
// Prints an error message
module.exports = msg => {
if (msg.message) {
msg = msg.message
}
console.error(`${chalk.red('> Error!')} ${msg}`)
}

6
src/providers/sh/util/etc/output/info.js

@ -1,6 +0,0 @@
const chalk = require('chalk')
// Prints an informational message
module.exports = msg => {
console.log(`${chalk.gray('>')} ${msg}`)
}

1
src/providers/sh/util/etc/output/logo.js

@ -1 +0,0 @@
module.exports = process.platform === 'win32' ? 'Δ' : '𝚫'

6
src/providers/sh/util/etc/output/note.js

@ -1,6 +0,0 @@
const chalk = require('chalk')
// Prints a note
module.exports = msg => {
console.log(`${chalk.yellow('> NOTE:')} ${msg}`)
}

7
src/providers/sh/util/etc/output/param.js

@ -1,7 +0,0 @@
const chalk = require('chalk')
// Returns a user param in a nice formatting
// e.g.: google.com -> "google.com" (in bold)
module.exports = param =>
chalk.bold(`${chalk.gray('"')}${chalk.bold(param)}${chalk.gray('"')}`)

6
src/providers/sh/util/etc/output/success.js

@ -1,6 +0,0 @@
const chalk = require('chalk')
// Prints a success message
module.exports = msg => {
console.log(`${chalk.cyan('> Success!')} ${msg}`)
}

15
src/providers/sh/util/etc/output/wait.js

@ -1,15 +0,0 @@
const ora = require('ora')
const chalk = require('chalk')
const { eraseLine } = require('ansi-escapes')
// Prints a spinner followed by the given text
module.exports = msg => {
const spinner = ora(chalk.gray(msg))
spinner.color = 'gray'
spinner.start()
return () => {
spinner.stop()
process.stdout.write(eraseLine)
}
}

0
src/providers/sh/util/etc/check-path.js → src/util/check-path.js

0
src/providers/sh/util/etc/exit.js → src/util/exit.js

0
src/providers/sh/util/etc/fatal-error.js → src/util/fatal-error.js

0
src/providers/sh/util/etc/input/prompt-options.js → src/util/input/prompt-options.js

0
src/providers/sh/util/etc/input/regexes.js → src/util/input/regexes.js

1
src/providers/sh/util/etc/output/code.js → src/util/output/code.js

@ -1,3 +1,4 @@
// Packages
const chalk = require('chalk')
// The equivalent of <code>, for embedding anything

0
src/providers/sh/util/etc/output/right-pad.js → src/util/output/right-pad.js

0
src/providers/sh/util/etc/output/stamp.js → src/util/output/stamp.js

0
src/providers/sh/util/etc/output/table.js → src/util/output/table.js

0
src/providers/sh/util/etc/output/uid.js → src/util/output/uid.js

0
src/providers/sh/util/etc/to-human-path.js → src/util/to-human-path.js

0
src/providers/sh/util/etc/url.js → src/util/url.js

Loading…
Cancel
Save