-
Notifications
You must be signed in to change notification settings - Fork 3
/
support.coffee
44 lines (38 loc) · 1.38 KB
/
support.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Micon, dependency injector, will create global `app` variable.
Micon = require 'micon'
module.exports = app = new Micon()
app.app = app
# Printing.
app.p = console.log.bind console
# Logging.
withZero = (number) -> if number < 10 then "0#{number}" else number
removeNewlines = (list) ->
(obj || 'null').toString().replace(/\n/g, "\\n") for obj in list
timestamp = ->
date = new Date()
"#{date.getFullYear()}/#{withZero(date.getMonth() + 1)}/#{withZero(date.getDate())}" +
" #{withZero(date.getHours())}:#{withZero(date.getMinutes())}:#{withZero(date.getSeconds())}"
for name in ['log', 'info', 'warn', 'error']
do (name) ->
app[name] = (args...) ->
# Formatting console messages according to use case, giving more details in production.
if app.environment == 'development'
if name == 'error' then console[name] args...
else console[name] ' ', args...
else
args = removeNewlines args
if name == 'error' then console[name] name, timestamp(), args...
else console[name] ' ', timestamp(), args...
# Underscore.
app._ = require './underscore'
# Underscore for string.
app._s = require './underscore.string'
# Sync.
app.sync = require 'synchronize'
# User error.
UserError = (message) ->
error = Error.call this, message
error.name = "UserError"
return error
UserError.prototype.__proto__ = Error.prototype
global.UserError = UserError