Skip to content

Commit

Permalink
feat: rewrite FancyReporter without ora
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Mar 31, 2018
1 parent 47f9bb0 commit 73c1ddc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 59 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Elegant Console Logger
- Fancy output with Fallback for CI environments
- A global mockable stdout/stderr wrapper
- Pluggable reporters
- [ORA](https://github.com/sindresorhus/ora) integration
- Consistance CLI experience
- Scoped Loggers

Expand Down
Binary file modified assets/screen1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ const consola = require('..')

consola.start('Starting build')

consola.warn('Something is going to happen soon')

setTimeout(() => {
consola.success('Built!')
consola.info('Some info')
consola.error(new Error('Foo'))
consola.success('Build succeed in 10 seconds')
consola.info('Some extra info is here')
consola.debug('Hum hum hum')
consola.error(new Error('consola is awesome'))
consola.fatal('This is the end!')
}, 1500)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"homepage": "https://github.com/nuxt/consola#readme",
"dependencies": {
"figures": "^2.0.0",
"ora": "^2.0.0",
"lodash": "^4.17.5",
"std-env": "^1.1.0"
},
"devDependencies": {
Expand Down
46 changes: 26 additions & 20 deletions src/reporters/fancy.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import chalk from 'chalk'
import ORA from 'ora'
import figures from 'figures'
import _ from 'lodash'

const NS_SEPERATOR = chalk.blue(figures(' › '))

const ORA_FN = {
start: 'start',
success: 'succeed',
error: 'fail',
warn: 'warn',
info: 'info'
const ICONS = {
start: figures('●'),
info: figures('ℹ'),
success: figures('✔'),
error: figures('✖'),
fatal: figures('✖'),
warn: figures('⚠'),
debug: figures('…'),
trace: figures('…'),
default: figures('❯'),
ready: figures('♥')
}

const pad = str => _.padEnd(str, 9)

export default class FancyReporter {
constructor (stream, options = {}) {
this.stream = stream || process.stderr
}

this.ora = new ORA(this.stream, Object.assign({
hideCursor: false
}, options.ora))
formatBadge (type, color = 'blue') {
return chalk['bg' + _.startCase(color)].black(` ${type.toUpperCase()} `) + ' '
}

formatTag (type, color = 'blue') {
return chalk.bgKeyword(color).black(` ${type.toUpperCase()} `)
const icon = ICONS[type] || ICONS.default
return chalk[color](pad(`${icon} ${type.toLowerCase()}`)) + ' '
}

clear () {
Expand All @@ -33,26 +41,24 @@ export default class FancyReporter {
let message = logObj.message

if (logObj.scope) {
message = logObj.scope.replace(/:/g, '>') + '>' + message
message =
(logObj.scope.replace(/:/g, '>') + '>').split('>').join(NS_SEPERATOR) +
message
}

message = message.replace(/>/g, NS_SEPERATOR)

if (logObj.clear) {
this.clear()
}

const oraFn = ORA_FN[logObj.type]

if (oraFn && logObj.badge !== true) {
this.ora[oraFn](message)
if (logObj.badge) {
this.stream.write('\n\n' + this.formatBadge(logObj.type, logObj.color) + message + '\n\n')
} else {
this.stream.write('\n\n' + this.formatTag(logObj.type, logObj.color) + ' ' + message + '\n\n')
this.stream.write(this.formatTag(logObj.type, logObj.color) + message + '\n')
}

if (logObj.additional) {
const lines = logObj.additional.split('\n').map(s => ' ' + s).join('\n')
this.stream.write(chalk.grey(lines) + '\n\n')
this.stream.write(chalk.grey(lines) + '\n')
}
}
}
35 changes: 1 addition & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,6 @@ cli-cursor@^2.0.0, cli-cursor@^2.1.0:
dependencies:
restore-cursor "^2.0.0"

cli-spinners@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-1.1.0.tgz#f1847b168844d917a671eb9d147e3df497c90d06"

cli-width@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
Expand Down Expand Up @@ -1420,12 +1416,6 @@ deep-is@~0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"

defaults@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/defaults/-/defaults-1.0.3.tgz#c656051e9817d9ff08ed881477f3fe4019f3ef7d"
dependencies:
clone "^1.0.2"

defined@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/defined/-/defined-1.0.0.tgz#c98d9bcef75674188e110969151199e39b1fa693"
Expand Down Expand Up @@ -2459,16 +2449,10 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
lodash@^4.14.0, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"

log-symbols@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a"
dependencies:
chalk "^2.0.1"

log-update@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/log-update/-/log-update-2.3.0.tgz#88328fd7d1ce7938b29283746f0b1bc126b24708"
Expand Down Expand Up @@ -2743,17 +2727,6 @@ optionator@^0.8.2:
type-check "~0.3.2"
wordwrap "~1.0.0"

ora@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ora/-/ora-2.0.0.tgz#8ec3a37fa7bffb54a3a0c188a1f6798e7e1827cd"
dependencies:
chalk "^2.3.1"
cli-cursor "^2.1.0"
cli-spinners "^1.1.0"
log-symbols "^2.2.0"
strip-ansi "^4.0.0"
wcwidth "^1.0.1"

os-homedir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"
Expand Down Expand Up @@ -4021,12 +3994,6 @@ vue-eslint-parser@^2.0.3:
esquery "^1.0.0"
lodash "^4.17.4"

wcwidth@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8"
dependencies:
defaults "^1.0.3"

whet.extend@~0.9.9:
version "0.9.9"
resolved "https://registry.yarnpkg.com/whet.extend/-/whet.extend-0.9.9.tgz#f877d5bf648c97e5aa542fadc16d6a259b9c11a1"
Expand Down

0 comments on commit 73c1ddc

Please sign in to comment.