Completed
Push — master ( 55b531...a54cab )
by Thomas
01:26 queued 01:08
created

output.js ➔ processBacklog   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 9.4285
1
'use strict'
2
3
const ora = require('ora')
4
const chalk = require('chalk')
5
const moment = require('moment')
6
const format = require('util').format
7
const eraseLine = require('ansi-escapes').eraseLine
8
const slice = Array.prototype.slice
9
10
var theme = {
11
  success: chalk.green,
12
  warn: chalk.yellow,
13
  yellow: chalk.yellow,
14
  err: chalk.red,
15
  errEmphasis: chalk.red.underline,
16
  info: chalk.underline.white,
17
  offset: chalk.italic,
18
  notice: chalk.underline
19
}
20
21
var themeChars = {
22
  notice: '',
23
  log: '•',
24
  err: '• ',
25
  warn: 'Warning: ',
26
  errEmphasis: '',
27
  success: '',
28
  offset: '',
29
  info: '',
30
  yellow: ''
31
}
32
33
var activeSpinner = null
34
var backlog = []
35
36
function processBacklog () {
37
  for (var x in backlog) {
38
    var action = backlog[x]
39
    exports[action[0]].apply(action[1])
40
  }
41
  backlog = []
42
}
43
44
function log () {
45
  var args = slice.call(arguments)
46
  var level = args.shift()
47
48
  args[0] = args[0] ? themeChars[level] + args[0] : ''
49
  args[0] = args[0] ? theme[level](args[0]) : ''
50
  console.log.apply(null, args)
51
}
52
53
function write () {
54
  var out = process.stdout
55
  var args = slice.call(arguments)
56
  var level = args.shift()
57
58
  out.write(theme[level](format.apply(null, [themeChars[level]].concat(args))))
59
}
60
61
exports.log = function () {
62
  if (activeSpinner) {
63
    backlog.push(['log', this.arguments])
64
    return
65
  }
66
67
  console.log.apply(null, [themeChars.log].concat(slice.call(arguments)))
68
}
69
70
exports.write = function (str) {
71
  process.stdout.write(str)
72
}
73
74
exports.newline = function () {
75
  process.stdout.write('\n')
76
}
77
78
exports.wait = function (msg) {
79
  var spinner = ora({
80
    text: chalk.gray(msg),
81
    color: 'gray'
82
  })
83
  spinner.start()
84
85
  return function (result) {
86
    if (result === undefined) {
87
      spinner.stop()
88
      process.stdout.write(eraseLine)
89
    } else if (result === true) {
90
      spinner.succeed()
91
    } else {
92
      spinner.fail(result)
93
    }
94
95
    activeSpinner = null
96
    processBacklog()
97
  }
98
}
99
100
exports.logComponent = function (component, color, message) {
101
  message = message.split('\n').map(function (string) {
0 ignored issues
show
Unused Code introduced by
The assignment to variable message seems to be never used. Consider removing it.
Loading history...
102
    if (string.length) {
103
      exports.logComponentLine(component, color, string)
104
    }
105
  })
106
}
107
108
exports.logComponentLine = function (component, color, message) {
109
  var str = chalk.bold(chalk[color](component))
110
  str += ' ' + message
111
  str += ' ' + chalk.dim(moment().format('hh:mm:ss'))
112
113
  return this.log(str)
114
}
115
116
exports.notice = exports.info = exports.err = exports.warn = function () {
117
  // will be overwritten later, only here for syntax completion
118
}
119
120
Object.keys(theme).forEach(function (level) {
121
  exports[level] = function () {
122
    log.apply(null, [level].concat(slice.call(arguments)))
123
  }
124
125
  var c = level[0].toUpperCase()
126
  exports['write' + c + level.slice(1)] = function () {
127
    write.apply(null, [level].concat(slice.call(arguments)))
128
  }
129
})
130