1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-05-10 07:47:07 +01:00

Asynchrony.

This commit is contained in:
hacksalot
2016-02-01 21:14:36 -05:00
parent 212b01092c
commit 70f45d468d
77 changed files with 1162 additions and 519 deletions

View File

@ -22,10 +22,8 @@ require 'string.prototype.startswith'
###*
Error handler for HackMyResume. All errors are handled here.
@class ErrorHandler
###
###* Error handler for HackMyResume. All errors are handled here.
@class ErrorHandler ###
ErrorHandler = module.exports =
init: ( debug, assert, silent ) ->
@ -38,7 +36,7 @@ ErrorHandler = module.exports =
err: ( ex, shouldExit ) ->
# Short-circuit logging output if --silent is on
o = if this.silent then () -> else _defaultLog
o = if @silent then () -> else _defaultLog
# Special case; can probably be removed.
throw ex if ex.pass
@ -51,7 +49,7 @@ ErrorHandler = module.exports =
# Output the error message
objError = assembleError.call @, ex
o( this[ 'format_' + objError.etype ]( objError.msg ))
o( @[ 'format_' + objError.etype ]( objError.msg ))
# Output the stack (sometimes)
if objError.withStack
@ -59,20 +57,20 @@ ErrorHandler = module.exports =
stack && o( chalk.gray( stack ) );
# Quit if necessary
if ex.quit || objError.quit
if shouldExit
if @debug
o chalk.cyan('Exiting with error code ' + ex.fluenterror.toString())
if this.assert
if @assert
ex.pass = true
throw ex
process.exit ex.fluenterror
# Handle raw exceptions
else
o( ex )
o ex
stackTrace = ex.stack || (ex.inner && ex.inner.stack)
if stackTrace && this.debug
o( M2C(ex.stack || ex.inner.stack, 'gray') )
o M2C(ex.stack || ex.inner.stack, 'gray')
@ -214,6 +212,11 @@ assembleError = ( ex ) ->
msg = ex
etype = 'error'
when HMSTATUS.createError
# inner.code could be EPERM, EACCES, etc
msg = printf M2C( this.msgs.createError.msg ), ex.inner.path
etype = 'error'
msg: msg # The error message to display
withStack: withStack # Whether to include the stack
quit: quit

View File

@ -23,6 +23,8 @@ Command = require('commander').Command
_opts = { }
_title = chalk.white.bold('\n*** HackMyResume v' +PKG.version+ ' ***')
_out = new OUTPUT( _opts )
_err = require('./error')
_exitCallback = null
@ -33,9 +35,9 @@ line interface as a single method accepting a parameter array.
@param rawArgs {Array} An array of command-line parameters. Will either be
process.argv (in production) or custom parameters (in test).
###
main = module.exports = (rawArgs) ->
main = module.exports = ( rawArgs, exitCallback ) ->
initInfo = initialize( rawArgs )
initInfo = initialize( rawArgs, exitCallback )
args = initInfo.args
# Create the top-level (application) command...
@ -129,10 +131,10 @@ main = module.exports = (rawArgs) ->
### Massage command-line args and setup Commander.js. ###
initialize = ( ar ) ->
o = initOptions( ar );
initialize = ( ar, exitCallback ) ->
_exitCallback = exitCallback || process.exit
o = initOptions ar
o.silent || logMsg( _title )
# Emit debug prelude if --debug was specified
@ -147,9 +149,11 @@ initialize = ( ar ) ->
#_out.log(chalk.cyan(PAD(' fresh-jrs-converter:',25, null, PAD.RIGHT)) + chalk.cyan.bold( PKG.dependencies['fresh-jrs-converter'] ))
_out.log('')
_err.init o.debug, o.assert, o.silent
# Handle invalid verbs here (a bit easier here than in commander.js)...
if o.verb && !HMR.verbs[ o.verb ] && !HMR.alias[ o.verb ]
throw { fluenterror: HMSTATUS.invalidCommand, quit: true, attempted: o.orgVerb }
_err.err fluenterror: HMSTATUS.invalidCommand, quit: true, attempted: o.orgVerb, true
# Override the .missingArgument behavior
Command.prototype.missingArgument = (name) ->
@ -205,23 +209,17 @@ initOptions = ( ar ) ->
oJSON = inf.json
# TODO: Error handling
# Grab the --debug flag
isDebug = _.some( args, (v) ->
return v == '-d' || v == '--debug'
)
# Grab the --silent flag
isSilent = _.some( args, (v) ->
return v == '-s' || v == '--silent'
)
# Grab the --no-color flag
# Grab the --debug flag, --silent, --assert and --no-color flags
isDebug = _.some args, (v) -> v == '-d' || v == '--debug'
isSilent = _.some args, (v) -> v == '-s' || v == '--silent'
isAssert = _.some args, (v) -> v == '-a' || v == '--assert'
isMono = _.some args, (v) -> v == '--no-color'
return {
color: !isMono,
debug: isDebug,
silent: isSilent,
assert: isAssert,
orgVerb: oVerb,
verb: verb,
json: oJSON,
@ -233,19 +231,29 @@ initOptions = ( ar ) ->
### Invoke a HackMyResume verb. ###
execute = ( src, dst, opts, log ) ->
loadOptions.call( this, opts, this.parent.jsonArgs )
hand = require( './error' )
hand.init( _opts.debug, _opts.assert, _opts.silent )
v = new HMR.verbs[ this.name() ]()
_opts.errHandler = v
_out.init( _opts )
v.on( 'hmr:status', -> _out.do.apply( _out, arguments ) )
v.on( 'hmr:error', -> hand.err.apply( hand, arguments ) )
v.invoke.call( v, src, dst, _opts, log )
if v.errorCode
console.log 'Exiting with error code ' + v.errorCode
process.exit(v.errorCode)
# Create the verb
v = new HMR.verbs[ @name() ]()
# Initialize command-specific options
loadOptions.call( this, opts, this.parent.jsonArgs )
# Set up error/output handling
_opts.errHandler = v
_out.init _opts
# Hook up event notifications
v.on 'hmr:status', -> _out.do.apply _out, arguments
v.on 'hmr:error', -> _err.err.apply _err, arguments
# Invoke the verb! Returns a promise
prom = v.invoke.call v, src, dst, _opts, log
# Resolved or rejected?
onFail = (err) ->
_exitCallback( if err.fluenterror then err.fluenterror else err )
return
prom.then (->), onFail
return

View File

@ -3,6 +3,8 @@ events:
msg: Invoking **%s** command.
beforeCreate:
msg: Creating new **%s** resume: **%s**
afterCreate:
msg: Creating new **%s** resume: **%s**
afterRead:
msg: Reading **%s** resume: **%s**
beforeTheme:
@ -96,3 +98,5 @@ errors:
msg: "Invalid number of parameters. Expected: **%s**."
missingParam:
msg: The '**%s**' parameter was needed but not supplied.
createError:
msg: Failed to create **'%s'**.

View File

@ -50,8 +50,12 @@ OutputHandler = module.exports = Class.extend
this.opts.debug &&
L( M2C( this.msgs.begin.msg, dbgStyle), evt.cmd.toUpperCase() )
when HME.beforeCreate
L( M2C( this.msgs.beforeCreate.msg, 'green' ), evt.fmt, evt.file )
#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 )
break;
when HME.beforeTheme