Permalink
Browse files

Improve feedback

  • Loading branch information...
Kikobeats committed Apr 30, 2018
1 parent dc1d8af commit 2033bee19e9b89387364dcb9be69ab5913f9df9b
Showing with 97 additions and 111 deletions.
  1. +2 −6 bin/cli/help.js
  2. +2 −2 bin/cli/index.js
  3. +46 −0 bin/view/colorize.js
  4. +2 −35 bin/view/helpers.js
  5. +23 −35 bin/view/index.js
  6. +19 −21 bin/view/render.js
  7. +0 −10 bin/view/theme.js
  8. +3 −2 package.json
@@ -1,13 +1,9 @@
'use strict'
const chalk = require('chalk')
const theme = require('../view/theme')
const { green, gray } = require('../view/colorize')
const { description } = require('../../package.json')
const green = chalk.hex(theme.green)
module.exports = chalk.gray(`${chalk.gray(description)}
module.exports = gray(`${gray(description)}
Usage
$ ${green('urlint')} <url> [<flags>]
@@ -2,8 +2,8 @@
'use strict'
const { size, concat, first, isEmpty } = require('lodash')
const normalizeUrl = require('normalize-url')
const { concat, first, isEmpty } = require('lodash')
const urlint = require('urlint')
const isCI = require('is-ci')
const got = require('got')
@@ -78,5 +78,5 @@ if (isEmpty(cli.input)) {
const emitter = await urlint(urls, opts)
console.log()
view({ emitter, ...opts })
view({ total: size(urls), emitter, ...opts })
})()
@@ -0,0 +1,46 @@
'use strict'
const { mapValues } = require('lodash')
const chalk = require('chalk')
const THEME = {
red: '#ff5c57',
green: '#5af78e',
yellow: '#f3f99d',
blue: '#57c7ff',
magenta: '#ff6ac1',
cyan: '#9aedfe'
}
const STATUS_CODE_COLOR = {
'2': 'green',
'3': 'blue',
'4': 'yellow',
'5': 'red',
'9': 'red'
}
const statusCodeColors = mapValues(THEME, hex => chalk.hex(hex))
const STATUS_CODE_COLOR_FALLBACK = 'magenta'
const getStatusCodeId = statusCode => statusCode.toString().charAt(0)
const getStatusCodeColor = statusCode => {
const id = getStatusCodeId(statusCode)
return STATUS_CODE_COLOR[id] || STATUS_CODE_COLOR_FALLBACK
}
const byStatusCode = (statusCode, str = statusCode) => {
const color = getStatusCodeColor(statusCode)
const statusCodeColor = statusCodeColors[color]
return statusCodeColor(str)
}
module.exports = {
...statusCodeColors,
gray: chalk.gray,
byStatusCode,
theme: THEME
}
@@ -1,36 +1,6 @@
'use strict'
const { isEqual, includes, keys, size, mapValues, isNil } = require('lodash')
const chalk = require('chalk')
const theme = require('./theme')
const STATUS_CODE_COLOR = {
'2': 'green',
'3': 'blue',
'4': 'yellow',
'5': 'red',
'9': 'red'
}
const statusCodeColors = mapValues(theme, hex => chalk.hex(hex))
const STATUS_CODE_COLOR_FALLBACK = 'magenta'
const getStatusCodeId = statusCode => statusCode.toString().charAt(0)
const getStatusCodeColor = statusCode => {
const id = getStatusCodeId(statusCode)
return STATUS_CODE_COLOR[id] || STATUS_CODE_COLOR_FALLBACK
}
const colorizeStatus = (statusCode, str = statusCode) => {
const color = getStatusCodeColor(statusCode)
const statusCodeColor = statusCodeColors[color]
return statusCodeColor(str)
}
const colorizeLine = chalk.gray
const { isEqual, includes, keys, size, isNil } = require('lodash')
const setState = (state, { statusCode, data }) => {
const status = state.count[statusCode]
@@ -49,13 +19,10 @@ const processExit = data => {
const statusCodes = keys(data)
const isOnly2xx =
includes(statusCodes, '2xx') && isEqual(size(statusCodes), 1)
const exitCode = isOnly2xx ? 0 : 1
process.exit(exitCode)
process.exit(isOnly2xx ? 0 : 1)
}
module.exports = {
colorizeStatus,
colorizeLine,
setState,
processExit
}
@@ -5,49 +5,37 @@ const neatLog = require('neat-log')
const { processExit, setState } = require('./helpers')
const render = require('./render')
module.exports = ({ emitter, quiet, ...opts }) => {
if (quiet) {
const state = { count: {}, links: {} }
module.exports = ({ total, emitter, quiet, logspeed, ...opts }) => {
const state = {
total,
current: 0,
count: {},
links: {},
end: false,
fetchingUrl: '',
startTimestamp: Date.now()
}
const neat = neatLog(render, { ...opts, logspeed, state })
neat.use((state, bus) => {
emitter.on('status', ({ statusCode, data }) => {
const newState = setState(state, { statusCode, data })
state.count = { ...state.count, ...newState.count }
state.links = { ...state.links, ...newState.links }
})
emitter.on('fetching', ({ url }) => {
state.fetchingUrl = url
++state.current
})
emitter.on('end', data => {
const output = render.links(state)
console.log(output)
state.end = true
neat.render()
processExit(data)
})
} else {
const neat = neatLog(render, opts)
neat.use((state, bus) => {
state.count = {}
state.links = {}
state.end = false
setInterval(() => {
bus.emit('render')
}, 100)
emitter.on('status', ({ statusCode, data }) => {
const newState = setState(state, { statusCode, data })
state.count = { ...state.count, ...newState.count }
state.links = { ...state.links, ...newState.links }
})
emitter.on('fetching', ({ url }) => {
state.fetchingUrl = url
})
emitter.on('end', data => {
state.end = true
bus.clear()
neat.render()
processExit(data)
})
})
}
setInterval(() => bus.emit('render'), logspeed)
})
}
@@ -1,17 +1,14 @@
'use strict'
const { toNumber, first, chain, map, reduce } = require('lodash')
const os = require('os')
const prettyMs = require('pretty-ms')
const { EOL } = require('os')
const spinner = require('ora')({
text: '',
color: 'blue'
})
const { colorizeStatus, colorizeLine } = require('./helpers')
const spinner = require('ora')({ text: '', color: 'gray' })
const { green, blue, gray, byStatusCode } = require('./colorize')
const resumeCount = (count, statusCode) =>
colorizeStatus(statusCode, `${statusCode} ${count}`)
byStatusCode(statusCode, `${statusCode} ${count}`)
const sortByStatusCode = data =>
chain(data)
@@ -24,28 +21,29 @@ const sortByStatusCode = data =>
.value()
const renderCount = state => {
const { count, fetchingUrl = '' } = state
const resume = map(count, resumeCount).join(os.EOL)
const footer = fetchingUrl
? `${os.EOL}${os.EOL}${spinner.frame()}${colorizeLine(fetchingUrl)}`
: ''
return `${resume}${footer}`
const { count, fetchingUrl, startTimestamp, current, total } = state
const countByStatusCode = map(count, resumeCount).join(EOL) || green(0)
const spinnerFrame = spinner.frame()
const timestamp = blue(prettyMs(Date.now() - startTimestamp))
const url = gray(fetchingUrl)
const progress = gray(`${current}/${total}`)
return `${countByStatusCode}${EOL}${EOL}${timestamp} ${spinnerFrame}${progress} ${url}`
}
const renderResume = ({ count, links }) => {
const info = map(sortByStatusCode(count), (count, statusCode) => {
const statusHeader = resumeCount(count, statusCode)
const countByStatusCode = resumeCount(count, statusCode)
const rows = map(
links[statusCode],
([statusCode, url]) => `${colorizeStatus(statusCode)} ${url}`
).join(os.EOL)
return statusHeader + os.EOL + rows + os.EOL
}).join(os.EOL)
([statusCode, url]) => `${byStatusCode(statusCode)} ${url}`
).join(EOL)
return countByStatusCode + EOL + rows + EOL
}).join(EOL)
const total = reduce(count, (acc, count) => acc + count, 0)
return colorizeLine(`${info}${os.EOL}Total ${total}`)
return gray(`${info}${EOL}Total ${total}`)
}
module.exports = state =>

This file was deleted.

Oops, something went wrong.
@@ -39,9 +39,10 @@
"is-ci": "~1.1.0",
"lodash": "~4.17.10",
"meow": "~5.0.0",
"neat-log": "~2.1.1",
"neat-log": "~2.2.0",
"normalize-url": "~2.0.1",
"ora": "~2.0.0",
"ora": "~2.1.0",
"pretty-ms": "~3.1.0",
"update-notifier": "~2.5.0",
"urlint": "~1.0.7",
"xml-urls": "~1.0.1"

0 comments on commit 2033bee

Please sign in to comment.