1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-09-28 20:19:12 +01:00
HackMyResume/src/cli/out.coffee

183 lines
5.9 KiB
CoffeeScript
Raw Permalink Normal View History

2016-01-26 16:39:24 +00:00
###*
Output routines for HackMyResume.
@license MIT. See LICENSE.md for details.
@module cli/out
###
chalk = require('chalk')
HME = require('../core/event-codes')
2016-01-26 16:39:24 +00:00
_ = require('underscore')
M2C = require('../utils/md2chalk.js')
2016-01-26 16:39:24 +00:00
PATH = require('path')
FS = require('fs')
EXTEND = require('extend')
HANDLEBARS = require('handlebars')
YAML = require('yamljs')
printf = require('printf')
pad = require('string-padding')
dbgStyle = 'cyan';
2016-01-26 16:39:24 +00:00
###* A stateful output module. All HMR console output handled here. ###
2018-02-12 09:01:00 +00:00
class OutputHandler
constructor: ( opts ) ->
@init opts
return
2016-01-26 16:39:24 +00:00
init: (opts) ->
@opts = EXTEND( true, @opts || { }, opts )
2016-01-26 16:39:24 +00:00
@msgs = YAML.load(PATH.join( __dirname, 'msg.yml' )).events
2016-01-27 10:29:26 +00:00
return
2016-01-26 16:39:24 +00:00
2018-02-12 09:01:00 +00:00
log: ->
2016-01-26 16:39:24 +00:00
printf = require('printf')
finished = printf.apply( printf, arguments )
2018-02-12 09:01:00 +00:00
@opts.silent || console.log( finished ) # eslint-disable-line no-console
2016-01-26 16:39:24 +00:00
2016-01-26 16:39:24 +00:00
do: ( evt ) ->
that = @
L = () -> that.log.apply( that, arguments )
switch evt.sub
when HME.begin
this.opts.debug &&
2016-01-26 19:43:48 +00:00
L( M2C( this.msgs.begin.msg, dbgStyle), evt.cmd.toUpperCase() )
2016-01-26 16:39:24 +00:00
2016-02-02 02:14:36 +00:00
#when HME.beforeCreate
#L( M2C( this.msgs.beforeCreate.msg, 'green' ), evt.fmt, evt.file )
#break;
when HME.afterCreate
L( M2C( @msgs.beforeCreate.msg, if evt.isError then 'red' else 'green' ), evt.fmt, evt.file )
2016-01-26 16:39:24 +00:00
break;
when HME.beforeTheme
this.opts.debug &&
2016-01-26 19:43:48 +00:00
L( M2C( this.msgs.beforeTheme.msg, dbgStyle), evt.theme.toUpperCase() )
2016-01-26 16:39:24 +00:00
when HME.afterParse
2016-01-26 19:43:48 +00:00
L( M2C( this.msgs.afterRead.msg, 'gray', 'white.dim'), evt.fmt.toUpperCase(), evt.file )
2016-01-26 16:39:24 +00:00
when HME.beforeMerge
msg = ''
2016-01-26 19:43:48 +00:00
evt.f.reverse().forEach ( a, idx ) ->
2016-01-26 16:39:24 +00:00
msg += printf( (if idx == 0 then @msgs.beforeMerge.msg[0] else @msgs.beforeMerge.msg[1]), a.file )
2016-01-26 19:43:48 +00:00
, @
L( M2C(msg, (if evt.mixed then 'yellow' else 'gray'), 'white.dim') )
2016-01-26 16:39:24 +00:00
when HME.applyTheme
@theme = evt.theme;
numFormats = Object.keys( evt.theme.formats ).length;
L( M2C(this.msgs.applyTheme.msg,
if evt.status == 'error' then 'red' else 'gray',
if evt.status == 'error' then 'bold' else 'white.dim'),
evt.theme.name.toUpperCase(),
2016-01-26 19:43:48 +00:00
numFormats, if numFormats == 1 then '' else 's' )
2016-01-26 16:39:24 +00:00
when HME.end
if evt.cmd == 'build'
themeName = this.theme.name.toUpperCase()
if this.opts.tips && (this.theme.message || this.theme.render)
if this.theme.message
L( M2C( this.msgs.afterBuild.msg[0], 'cyan' ), themeName )
L( M2C( this.theme.message, 'white' ))
else if this.theme.render
L( M2C( this.msgs.afterBuild.msg[0], 'cyan'), themeName)
L( M2C( this.msgs.afterBuild.msg[1], 'white'))
when HME.afterGenerate
suffix = ''
if evt.fmt == 'pdf'
if this.opts.pdf
if this.opts.pdf != 'none'
2016-01-26 16:43:49 +00:00
suffix = printf( M2C( this.msgs.afterGenerate.msg[0], if evt.error then 'red' else 'green' ), this.opts.pdf )
2016-01-26 16:39:24 +00:00
else
2016-01-26 16:43:49 +00:00
L( M2C( this.msgs.afterGenerate.msg[1], 'gray' ), evt.fmt.toUpperCase(), evt.file )
2016-01-26 16:39:24 +00:00
return
2016-01-26 16:43:49 +00:00
L( M2C( this.msgs.afterGenerate.msg[2] + suffix, if evt.error then 'red' else 'green' ),
pad( evt.fmt.toUpperCase(),4,null,pad.RIGHT ),
PATH.relative( process.cwd(), evt.file ) );
2016-01-26 16:39:24 +00:00
when HME.beforeAnalyze
L( M2C( this.msgs.beforeAnalyze.msg, 'green' ), evt.fmt, evt.file)
when HME.afterAnalyze
info = evt.info
rawTpl = FS.readFileSync( PATH.join( __dirname, 'analyze.hbs' ), 'utf8')
HANDLEBARS.registerHelper( require('../helpers/console-helpers') )
2016-01-26 16:39:24 +00:00
template = HANDLEBARS.compile(rawTpl, { strict: false, assumeObjects: false })
tot = 0
info.keywords.forEach (g) -> tot += g.count
info.keywords.totalKeywords = tot
output = template( info )
@log( chalk.cyan(output) )
when HME.beforeConvert
2018-01-30 07:34:58 +00:00
L( M2C( this.msgs.beforeConvert.msg, if evt.error then 'red' else 'green' ),
2016-01-26 16:39:24 +00:00
evt.srcFile, evt.srcFmt, evt.dstFile, evt.dstFmt
);
when HME.afterInlineConvert
L( M2C( this.msgs.afterInlineConvert.msg, 'gray', 'white.dim' ),
evt.file, evt.fmt );
when HME.afterValidate
style = 'red'
adj = ''
msgs = @msgs.afterValidate.msg;
switch evt.status
when 'valid' then style = 'green'; adj = msgs[1]
when 'invalid' then style = 'yellow'; adj = msgs[2]
when 'broken' then style = 'red'; adj = msgs[3]
when 'missing' then style = 'red'; adj = msgs[4]
2016-02-13 05:11:52 +00:00
when 'unknown' then style = 'red'; adj = msgs[5]
evt.schema = evt.schema.replace('jars','JSON Resume').toUpperCase()
2016-02-13 04:47:08 +00:00
L(M2C( msgs[0], 'white' ) + chalk[style].bold(adj), evt.file, evt.schema)
2016-02-13 04:47:08 +00:00
if evt.violations
2018-02-12 09:01:00 +00:00
_.each evt.violations, (err) ->
L( chalk.yellow.bold('--> ') +
chalk.yellow(err.field.replace('data.','resume.').toUpperCase() +
' ' + err.message))
return
, @
return
2016-01-26 16:39:24 +00:00
when HME.afterPeek
sty = if evt.error then 'red' else ( if evt.target != undefined then 'green' else 'yellow' )
2016-02-02 22:34:10 +00:00
# "Peeking at 'someKey' in 'someFile'."
2016-01-26 16:39:24 +00:00
if evt.requested
L(M2C(this.msgs.beforePeek.msg[0], sty), evt.requested, evt.file)
else
L(M2C(this.msgs.beforePeek.msg[1], sty), evt.file)
2016-02-02 22:34:10 +00:00
# If the key was present, print it
2016-02-04 01:08:17 +00:00
if evt.target != undefined and !evt.error
2018-02-12 09:01:00 +00:00
# eslint-disable-next-line no-console
2016-01-26 16:39:24 +00:00
console.dir( evt.target, { depth: null, colors: true } )
2016-02-02 22:34:10 +00:00
# If the key was not present, but no error occurred, print it
2016-01-26 16:39:24 +00:00
else if !evt.error
2016-02-04 01:08:17 +00:00
L M2C( this.msgs.afterPeek.msg, 'yellow'), evt.requested, evt.file
2016-02-02 22:34:10 +00:00
else if evt.error
2016-02-04 01:08:17 +00:00
L chalk.red( evt.error.inner.inner )
2018-02-12 09:01:00 +00:00
module.exports = OutputHandler