Permalink
Browse files

Improve render layer

  • Loading branch information...
Kikobeats committed Feb 2, 2018
1 parent 605053b commit 44715758df592f325f34c250db1415ebd233ab99
@@ -1,20 +1,25 @@
'use strict'
const chalk = require('chalk')
const theme = require('../view/theme')
const { theme } = require('../view/helpers')
const { description } = require('../../package.json')
const green = chalk.hex(theme.green)
module.exports = chalk.gray(`${chalk.gray(description)}
Usage
$ ${green('urlint')} url [<flags>]
Options
-w, --whitelist Add an url to ignore
-c, --concurrence Adjust the number of concurrent petitions (defaults to 30)
-v, --version Output the version number
$ ${green('urlint')} <url> [<flags>]
Flags
-c, --concurrence Adjust the number of concurrent petitions (defaults to 30)
-f, --followRedirect Redirect responses should be followed (defaults to true)
-h, --help Show the help information
-q, --quiet Show only the resume (defaults to false)
-r, --retries Number of request retries when network errors happens (defaults to 2)
-t, --timeout Milliseconds to wait before consider a timeout response
-v, --version Output the version number
-w, --whitelist Add one or multiple url pattern to ignore
Examples
@@ -24,5 +29,4 @@ module.exports = chalk.gray(`${chalk.gray(description)}
– Exclude a particular URL
$ ${green(
'urlint'
)} https://kikobeats.com --whitelist https://www.linkedin.com/in/kikobeats'
`)
)} https://kikobeats.com --whitelist https://www.linkedin.com/in/kikobeats'`)
@@ -2,8 +2,9 @@
'use strict'
const normalizeUrl = require('normalize-url')
const { first, size, isEmpty } = require('lodash')
const { first, isEmpty } = require('lodash')
const urlint = require('urlint')
const isCI = require('is-ci')
const pkg = require('../../package.json')
const getUrls = require('./get-urls')
@@ -24,6 +25,31 @@ const cli = require('meow')(require('./help'), {
alias: 'c',
type: 'number',
default: 30
},
quiet: {
alias: 'q',
type: 'boolean',
default: isCI
},
followRedirect: {
alias: 'f',
type: 'boolean',
default: true
},
logspeed: {
alias: 's',
type: 'number',
default: 100
},
timeout: {
alias: 't',
type: 'number',
default: false
},
retries: {
alias: 'r',
type: 'number',
default: 2
}
}
})
@@ -41,12 +67,6 @@ const opts = Object.assign({}, cli.flags, {
;(async () => {
const urls = await getUrls(url, opts)
const emitter = await urlint(urls, opts)
console.log()
view({ emitter, ...opts })
emitter.on('end', data => {
const exitCode = size(data) > 1 ? 1 : 0
setTimeout(process.exit, 300, exitCode)
})
})()

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -1,8 +1,46 @@
'use strict'
const { STATUS_COLORS, STATUS_COLORS_FALLBACK } = require('./constants')
const chalk = require('chalk')
const { mapValues } = require('lodash')
const getStatusColor = status =>
STATUS_COLORS[status.toString().charAt(0)] || STATUS_COLORS_FALLBACK
const THEME = {
red: '#ff5c57',
green: '#5af78e',
yellow: '#f3f99d',
blue: '#57c7ff',
magenta: '#ff6ac1',
cyan: '#9aedfe'
}
module.exports = { getStatusColor }
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
module.exports = {
colorizeStatus,
colorizeLine,
theme: THEME
}
@@ -1,9 +1,52 @@
'use strict'
const path = require('path')
const { size, isNil } = require('lodash')
const neatLog = require('neat-log')
const componentPath = path.join(__dirname, './component.js')
const render = require('./render')
const mount = require('./mount')
const setState = (state, { statusCode, data }) => {
const status = state.count[statusCode]
const linkStatus = state.links[statusCode]
const linkItem = [data.statusCode, data.url]
module.exports = props => mount(componentPath, props)
const count = { [statusCode]: isNil(status) ? 1 : status + 1 }
const links = {
[statusCode]: isNil(linkStatus) ? [linkItem] : linkStatus.concat([linkItem])
}
return { count, links }
}
module.exports = ({ emitter, quiet, ...opts }) => {
const neat = neatLog(render, opts)
neat.use((state, bus) => {
state.quiet = quiet
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()
const exitCode = size(data) > 1 ? 1 : 0
process.exit(exitCode)
})
})
}

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,46 @@
'use strict'
const { map, reduce } = require('lodash')
const os = require('os')
const spinner = require('ora')({
text: '',
color: 'blue'
})
const { colorizeStatus, colorizeLine } = require('./helpers')
const resumeCount = (count, statusCode) => {
return colorizeStatus(statusCode, `${statusCode} ${count}`)
}
const count = state => {
const { quiet, count, fetchingUrl = '' } = state
if (quiet) return ''
const resume = map(count, resumeCount).join(os.EOL)
const footer = fetchingUrl
? `${os.EOL}${os.EOL}${spinner.frame()}${colorizeLine(fetchingUrl)}`
: ''
return `${resume}${footer}`
}
const links = ({ count, links }) => {
const total = reduce(count, (acc, count) => acc + count, 0)
const resume = map(count, (count, statusCode) => {
const header = resumeCount(count, statusCode)
const rows = map(
links[statusCode],
([statusCode, url]) => `${colorizeStatus(statusCode)} ${url}`
).join(os.EOL)
return header + os.EOL + rows + os.EOL
}).join(os.EOL)
return colorizeLine(`${resume}${os.EOL}Total ${total}`)
}
module.exports = state => (state.end === false ? count(state) : links(state))
module.exports.count = count
module.exports.links = links

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 4471575

Please sign in to comment.