From 80c36b96bc8d1b3d02e098f2660cecbb22360130 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Tue, 26 Jan 2016 10:58:10 -0500 Subject: [PATCH] ... --- dist/cli/error.js | 306 ++++++++++++++++++------------------------- dist/cli/main.js | 6 +- dist/cli/msg.js | 19 ++- dist/cli/out.js | 3 +- package.json | 3 +- src/cli/error.coffee | 221 +++++++++++++++++++++++++++++++ src/cli/error.js | 280 --------------------------------------- src/cli/main.coffee | 22 ++-- src/cli/msg.coffee | 10 ++ src/cli/msg.js | 18 --- src/cli/out.js | 3 +- test/test-stdout.js | 4 +- 12 files changed, 396 insertions(+), 499 deletions(-) create mode 100644 src/cli/error.coffee delete mode 100644 src/cli/error.js create mode 100644 src/cli/msg.coffee delete mode 100644 src/cli/msg.js diff --git a/dist/cli/error.js b/dist/cli/error.js index cc94d54..1841b35 100644 --- a/dist/cli/error.js +++ b/dist/cli/error.js @@ -1,280 +1,234 @@ + /** Error-handling routines for HackMyResume. @module cli/error @license MIT. See LICENSE.md for details. -*/ - - + */ (function() { + var ErrorHandler, FCMD, FS, HMSTATUS, M2C, PATH, PKG, SyntaxErrorEx, WRAP, YAML, _defaultLog, assembleError, chalk, extend, printf; + HMSTATUS = require('hackmycore/dist/core/status-codes'); + PKG = require('../../package.json'); - var HMSTATUS = require('hackmycore/dist/core/status-codes') - , PKG = require('../../package.json') - , FS = require('fs') - , FCMD = require('hackmycore') - , PATH = require('path') - , WRAP = require('word-wrap') - , M2C = require('hackmycore/dist/utils/md2chalk.js') - , chalk = require('chalk') - , extend = require('extend') - , YAML = require('yamljs') - , printf = require('printf') - , SyntaxErrorEx = require('hackmycore/dist/utils/syntax-error-ex'); - require('string.prototype.startswith'); + FS = require('fs'); + FCMD = require('hackmycore'); + + PATH = require('path'); + + WRAP = require('word-wrap'); + + M2C = require('hackmycore/dist/utils/md2chalk.js'); + + chalk = require('chalk'); + + extend = require('extend'); + + YAML = require('yamljs'); + + printf = require('printf'); + + SyntaxErrorEx = require('hackmycore/dist/utils/syntax-error-ex'); + + require('string.prototype.startswith'); /** Error handler for HackMyResume. All errors are handled here. @class ErrorHandler - */ - var ErrorHandler = module.exports = { + */ - - - init: function( debug, assert, silent ) { + ErrorHandler = module.exports = { + init: function(debug, assert, silent) { this.debug = debug; this.assert = assert; this.silent = silent; - this.msgs = require('./msg.js').errors; + this.msgs = require('./msg').errors; return this; }, - - - - err: function( ex, shouldExit ) { - - // Short-circuit logging output if --silent is on - var o = this.silent ? function() { } : _defaultLog; - - // Special case; can probably be removed. - if( ex.pass ) throw ex; - - // Load error messages - this.msgs = this.msgs || require('./msg.js').errors; - - // Handle packaged HMR exceptions - if( ex.fluenterror ) { - - // Output the error message - var objError = assembleError.call( this, ex ); - o( this[ 'format_' + objError.etype ]( objError.msg )); - - // Output the stack (sometimes) - if( objError.withStack ) { - var stack = ex.stack || (ex.inner && ex.inner.stack); - stack && o( chalk.gray( stack ) ); - } - - // Quit if necessary - if( ex.quit || objError.quit ) { - this.debug && o( - chalk.cyan('Exiting with error code ' + ex.fluenterror.toString())); - if( this.assert ) { ex.pass = true; throw ex; } - process.exit( ex.fluenterror ); - } - + err: function(ex, shouldExit) { + var o, objError, stack, stackTrace; + o = this.silent ? function() {} : _defaultLog; + if (ex.pass) { + throw ex; } - - // Handle raw exceptions - else { - o( ex ); - var stackTrace = ex.stack || (ex.inner && ex.inner.stack); - if( stackTrace && this.debug ) - o( M2C(ex.stack || ex.inner.stack, 'gray') ); + this.msgs = this.msgs || require('./msg').errors; + if (ex.fluenterror) { + objError = assembleError.call(this, ex); + o(this['format_' + objError.etype](objError.msg)); + if (objError.withStack) { + stack = ex.stack || (ex.inner && ex.inner.stack); + stack && o(chalk.gray(stack)); + } + if (ex.quit || objError.quit) { + if (this.debug) { + o(chalk.cyan('Exiting with error code ' + ex.fluenterror.toString())); + } + if (this.assert) { + ex.pass = true; + throw ex; + } + return process.exit(ex.fluenterror); + } + } else { + o(ex); + stackTrace = ex.stack || (ex.inner && ex.inner.stack); + if (stackTrace && this.debug) { + return o(M2C(ex.stack || ex.inner.stack, 'gray')); + } } - }, - - - - format_error: function( msg ) { + format_error: function(msg) { msg = msg || ''; - return chalk.red.bold( - msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg ); + return chalk.red.bold(msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg); }, - - - - format_warning: function( brief, msg ) { + format_warning: function(brief, msg) { return chalk.yellow(brief) + chalk.yellow(msg || ''); }, - - - format_custom: function( msg ) { + format_custom: function(msg) { return msg; } - - - }; + _defaultLog = function() { + return console.log.apply(console.log, arguments); + }; - - function _defaultLog() { - console.log.apply( console.log, arguments ); - } - - - - function assembleError( ex ) { - - var msg = '', withStack = false, quit = false, etype = 'warning'; - if( this.debug ) withStack = true; - - switch( ex.fluenterror ) { - + assembleError = function(ex) { + var etype, msg, quit, se, withStack; + msg = ''; + withStack = false; + quit = false; + etype = 'warning'; + if (this.debug) { + withStack = true; + } + switch (ex.fluenterror) { case HMSTATUS.themeNotFound: - msg = printf( M2C( this.msgs.themeNotFound.msg, 'yellow' ), ex.data); + msg = printf(M2C(this.msgs.themeNotFound.msg, 'yellow'), ex.data); break; - case HMSTATUS.copyCSS: - msg = M2C( this.msgs.copyCSS.msg, 'red' ); + msg = M2C(this.msgs.copyCSS.msg, 'red'); quit = false; break; - case HMSTATUS.resumeNotFound: - msg = M2C( this.msgs.resumeNotFound.msg, 'yellow' ); + msg = M2C(this.msgs.resumeNotFound.msg, 'yellow'); break; - case HMSTATUS.missingCommand: - msg = M2C( this.msgs.missingCommand.msg + " (", 'yellow'); - msg += Object.keys( FCMD.verbs ).map( function(v, idx, ar) { - return (idx === ar.length - 1 ? chalk.yellow('or ') : '') + - chalk.yellow.bold(v.toUpperCase()); - }).join( chalk.yellow(', ')) + chalk.yellow(").\n\n"); - - msg += chalk.gray(FS.readFileSync( - PATH.resolve(__dirname, '../cli/use.txt'), 'utf8' )); + msg = M2C(this.msgs.missingCommand.msg + " (", 'yellow'); + msg += Object.keys(FCMD.verbs).map(function(v, idx, ar) { + return (idx === ar.length - 1 ? chalk.yellow('or ') : '') + chalk.yellow.bold(v.toUpperCase()); + }).join(chalk.yellow(', ')) + chalk.yellow(").\n\n"); + msg += chalk.gray(FS.readFileSync(PATH.resolve(__dirname, '../cli/use.txt'), 'utf8')); break; - case HMSTATUS.invalidCommand: - msg = printf( M2C( this.msgs.invalidCommand.msg, 'yellow'), ex.attempted ); + msg = printf(M2C(this.msgs.invalidCommand.msg, 'yellow'), ex.attempted); break; - case HMSTATUS.resumeNotFoundAlt: - msg = M2C( this.msgs.resumeNotFoundAlt.msg, 'yellow' ); + msg = M2C(this.msgs.resumeNotFoundAlt.msg, 'yellow'); break; - case HMSTATUS.inputOutputParity: - msg = M2C( this.msgs.inputOutputParity.msg ); + msg = M2C(this.msgs.inputOutputParity.msg); break; - case HMSTATUS.createNameMissing: - msg = M2C( this.msgs.createNameMissing.msg ); + msg = M2C(this.msgs.createNameMissing.msg); break; - case HMSTATUS.pdfGeneration: - msg = M2C( this.msgs.pdfGeneration.msg, 'bold' ); - if( ex.inner ) msg += chalk.red('\n' + ex.inner); - withStack = true; quit = false; etype = 'error'; - break; - - case HMSTATUS.invalid: - msg = M2C( this.msgs.invalid.msg, 'red' ); + msg = M2C(this.msgs.pdfGeneration.msg, 'bold'); + if (ex.inner) { + msg += chalk.red('\n' + ex.inner); + } + withStack = true; + quit = false; + etype = 'error'; + break; + case HMSTATUS.invalid: + msg = M2C(this.msgs.invalid.msg, 'red'); etype = 'error'; break; - case HMSTATUS.generateError: msg = (ex.inner && ex.inner.toString()) || ex; quit = false; etype = 'error'; break; - case HMSTATUS.fileSaveError: - msg = printf( M2C( this.msgs.fileSaveError.msg ), (ex.inner || ex).toString() ); + msg = printf(M2C(this.msgs.fileSaveError.msg), (ex.inner || ex).toString()); etype = 'error'; quit = false; break; - case HMSTATUS.invalidFormat: - ex.data.forEach(function(d){ - msg += printf( M2C( this.msgs.invalidFormat.msg, 'bold' ), - ex.theme.name.toUpperCase(), d.format.toUpperCase()); + ex.data.forEach(function(d) { + return msg += printf(M2C(this.msgs.invalidFormat.msg, 'bold'), ex.theme.name.toUpperCase(), d.format.toUpperCase()); }, this); break; - case HMSTATUS.missingParam: - msg = printf( M2C( this.msgs.missingParam.msg ), ex.expected, ex.helper ); + msg = printf(M2C(this.msgs.missingParam.msg), ex.expected, ex.helper); break; - case HMSTATUS.invalidHelperUse: - msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper ); - if( ex.error ) { - msg += '\n--> ' + assembleError.call( this, extend( true, {}, ex, {fluenterror: ex.error} )).msg; - //msg += printf( '\n--> ' + M2C( this.msgs.invalidParamCount.msg ), ex.expected ); + msg = printf(M2C(this.msgs.invalidHelperUse.msg), ex.helper); + if (ex.error) { + msg += '\n--> ' + assembleError.call(this, extend(true, {}, ex, { + fluenterror: ex.error + })).msg; } quit = false; etype = 'warning'; break; - case HMSTATUS.notOnPath: - msg = printf( M2C(this.msgs.notOnPath.msg, 'bold'), ex.engine); + msg = printf(M2C(this.msgs.notOnPath.msg, 'bold'), ex.engine); quit = false; etype = 'error'; break; - case HMSTATUS.readError: - if( !ex.quiet ) - console.error(printf( M2C(this.msgs.readError.msg, 'red'), ex.file)); + if (!ex.quiet) { + console.error(printf(M2C(this.msgs.readError.msg, 'red'), ex.file)); + } msg = ex.inner.toString(); etype = 'error'; break; - case HMSTATUS.mixedMerge: - msg = M2C( this.msgs.mixedMerge.msg ); + msg = M2C(this.msgs.mixedMerge.msg); quit = false; break; - case HMSTATUS.invokeTemplate: - msg = M2C( this.msgs.invokeTemplate.msg, 'red' ); - msg += M2C( '\n' + WRAP(ex.inner.toString(), { width: 60, indent: ' ' }), 'gray' ); + msg = M2C(this.msgs.invokeTemplate.msg, 'red'); + msg += M2C('\n' + WRAP(ex.inner.toString(), { + width: 60, + indent: ' ' + }), 'gray'); etype = 'custom'; break; - case HMSTATUS.compileTemplate: etype = 'error'; break; - case HMSTATUS.themeLoad: - msg = M2C( printf( this.msgs.themeLoad.msg, ex.attempted.toUpperCase() ), 'red'); - if( ex.inner && ex.inner.fluenterror ) { - msg += M2C('\nError: ', 'red') + assembleError.call( this, ex.inner ).msg; + msg = M2C(printf(this.msgs.themeLoad.msg, ex.attempted.toUpperCase()), 'red'); + if (ex.inner && ex.inner.fluenterror) { + msg += M2C('\nError: ', 'red') + assembleError.call(this, ex.inner).msg; } quit = true; etype = 'custom'; break; - case HMSTATUS.parseError: - if( SyntaxErrorEx.is( ex.inner )) { - console.error( printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) ); - var se = new SyntaxErrorEx( ex, ex.raw ); - msg = printf( M2C( this.msgs.parseError.msg, 'red' ), - se.line, se.col); - } - else if( ex.inner && ex.inner.line !== undefined && ex.inner.col !== undefined ) { - msg = printf( M2C( this.msgs.parseError.msg, 'red' ), - ex.inner.line, ex.inner.col); - } - else { + if (SyntaxErrorEx.is(ex.inner)) { + console.error(printf(M2C(this.msgs.readError.msg, 'red'), ex.file)); + se = new SyntaxErrorEx(ex, ex.raw); + msg = printf(M2C(this.msgs.parseError.msg, 'red'), se.line, se.col); + } else if (ex.inner && ex.inner.line !== void 0 && ex.inner.col !== void 0) { + msg = printf(M2C(this.msgs.parseError.msg, 'red'), ex.inner.line, ex.inner.col); + } else { msg = ex; } etype = 'error'; - break; - } - return { - msg: msg, // The error message to display - withStack: withStack, // Whether to include the stack + msg: msg, + withStack: withStack, quit: quit, etype: etype }; - } + }; - - - -}()); +}).call(this); diff --git a/dist/cli/main.js b/dist/cli/main.js index 15c1222..1ab08bb 100644 --- a/dist/cli/main.js +++ b/dist/cli/main.js @@ -136,7 +136,7 @@ Definition of the `main` function. initOptions = function(ar) { oVerb; - var args, cleanArgs, inf, isDebug, isSilent, oJSON, oVerb, optStr, optsIdx, verb, vidx; + var args, cleanArgs, inf, isDebug, isMono, isSilent, oJSON, oVerb, optStr, optsIdx, verb, vidx; verb = ''; args = ar.slice(); cleanArgs = args.slice(2); @@ -177,7 +177,11 @@ Definition of the `main` function. isSilent = _.some(args, function(v) { return v === '-s' || v === '--silent'; }); + isMono = _.some(args, function(v) { + return v === '--no-color'; + }); return { + color: !isMono, debug: isDebug, silent: isSilent, orgVerb: oVerb, diff --git a/dist/cli/msg.js b/dist/cli/msg.js index ce4d2b5..c8038a1 100644 --- a/dist/cli/msg.js +++ b/dist/cli/msg.js @@ -1,18 +1,17 @@ + /** Message-handling routines for HackMyResume. -@module msg.js +@module cli/msg @license MIT. See LICENSE.md for details. -*/ - - + */ (function() { + var PATH, YAML; - var PATH = require('path'); - var YAML = require('yamljs'); + PATH = require('path'); - var cache = module.exports = function() { - return cache ? cache : YAML.load( PATH.join(__dirname, 'msg.yml') ); - }(); + YAML = require('yamljs'); -}()); + module.exports = YAML.load(PATH.join(__dirname, 'msg.yml')); + +}).call(this); diff --git a/dist/cli/out.js b/dist/cli/out.js index e3815ad..c06d1b3 100644 --- a/dist/cli/out.js +++ b/dist/cli/out.js @@ -25,6 +25,7 @@ Output routines for HackMyResume. , pad = require('string-padding') , dbgStyle = 'cyan'; + chalk.enabled = false; /** @@ -159,7 +160,7 @@ Output routines for HackMyResume. case HME.afterAnalyze: var info = evt.info; var rawTpl = FS.readFileSync( PATH.join( __dirname, 'analyze.hbs' ), 'utf8'); - HANDLEBARS.registerHelper( require('hackmycore/src/helpers/console-helpers') ); + HANDLEBARS.registerHelper( require('hackmycore/dist/helpers/console-helpers') ); var template = HANDLEBARS.compile(rawTpl, { strict: false, assumeObjects: false }); var tot = 0; info.keywords.forEach(function(g) { tot += g.count; }); diff --git a/package.json b/package.json index 6e96e47..a44115e 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "jsonresume-theme-modern": "0.0.18", "jsonresume-theme-sceptile": "^1.0.5", "mocha": "*", - "resample": "fluentdesk/resample" + "resample": "fluentdesk/resample", + "stripcolorcodes": "^0.1.0" } } diff --git a/src/cli/error.coffee b/src/cli/error.coffee new file mode 100644 index 0000000..f657545 --- /dev/null +++ b/src/cli/error.coffee @@ -0,0 +1,221 @@ +###* +Error-handling routines for HackMyResume. +@module cli/error +@license MIT. See LICENSE.md for details. +### + + + +HMSTATUS = require('hackmycore/dist/core/status-codes') +PKG = require('../../package.json') +FS = require('fs') +FCMD = require('hackmycore') +PATH = require('path') +WRAP = require('word-wrap') +M2C = require('hackmycore/dist/utils/md2chalk.js') +chalk = require('chalk') +extend = require('extend') +YAML = require('yamljs') +printf = require('printf') +SyntaxErrorEx = require('hackmycore/dist/utils/syntax-error-ex') +require('string.prototype.startswith') + + + +###* +Error handler for HackMyResume. All errors are handled here. +@class ErrorHandler +### +ErrorHandler = module.exports = + + init: ( debug, assert, silent ) -> + @debug = debug + @assert = assert + @silent = silent + @msgs = require('./msg').errors + @ + + err: ( ex, shouldExit ) -> + + # Short-circuit logging output if --silent is on + o = if this.silent then () -> else _defaultLog + + # Special case; can probably be removed. + throw ex if ex.pass + + # Load error messages + @msgs = @msgs || require('./msg').errors + + # Handle packaged HMR exceptions + if ex.fluenterror + + # Output the error message + objError = assembleError.call @, ex + o( this[ 'format_' + objError.etype ]( objError.msg )) + + # Output the stack (sometimes) + if objError.withStack + stack = ex.stack || (ex.inner && ex.inner.stack); + stack && o( chalk.gray( stack ) ); + + # Quit if necessary + if ex.quit || objError.quit + if @debug + o chalk.cyan('Exiting with error code ' + ex.fluenterror.toString()) + if this.assert + ex.pass = true + throw ex + process.exit ex.fluenterror + + # Handle raw exceptions + else + o( ex ) + stackTrace = ex.stack || (ex.inner && ex.inner.stack) + if stackTrace && this.debug + o( M2C(ex.stack || ex.inner.stack, 'gray') ) + + + + format_error: ( msg ) -> + msg = msg || '' + chalk.red.bold( if msg.toUpperCase().startsWith('ERROR:') then msg else 'Error: ' + msg ) + + + format_warning: ( brief, msg ) -> + chalk.yellow(brief) + chalk.yellow(msg || '') + + + format_custom: ( msg ) -> msg + + + +_defaultLog = () -> console.log.apply console.log, arguments + + + + +assembleError = ( ex ) -> + + msg = '' + withStack = false + quit = false + etype = 'warning' + withStack = true if @debug + + switch ex.fluenterror + + when HMSTATUS.themeNotFound + msg = printf( M2C( this.msgs.themeNotFound.msg, 'yellow' ), ex.data) + + when HMSTATUS.copyCSS + msg = M2C( this.msgs.copyCSS.msg, 'red' ) + quit = false + + when HMSTATUS.resumeNotFound + msg = M2C( this.msgs.resumeNotFound.msg, 'yellow' ); + + when HMSTATUS.missingCommand + msg = M2C( this.msgs.missingCommand.msg + " (", 'yellow'); + msg += Object.keys( FCMD.verbs ).map( (v, idx, ar) -> + return ( if idx == ar.length - 1 then chalk.yellow('or ') else '') + + chalk.yellow.bold(v.toUpperCase()); + ).join( chalk.yellow(', ')) + chalk.yellow(").\n\n"); + + msg += chalk.gray(FS.readFileSync( + PATH.resolve(__dirname, '../cli/use.txt'), 'utf8' )) + + when HMSTATUS.invalidCommand + msg = printf( M2C( this.msgs.invalidCommand.msg, 'yellow'), ex.attempted ) + + when HMSTATUS.resumeNotFoundAlt + msg = M2C( this.msgs.resumeNotFoundAlt.msg, 'yellow' ) + + when HMSTATUS.inputOutputParity + msg = M2C( this.msgs.inputOutputParity.msg ) + + when HMSTATUS.createNameMissing + msg = M2C( this.msgs.createNameMissing.msg ) + + when HMSTATUS.pdfGeneration + msg = M2C( this.msgs.pdfGeneration.msg, 'bold' ) + msg += chalk.red('\n' + ex.inner) if ex.inner + withStack = true + quit = false + etype = 'error' + + when HMSTATUS.invalid + msg = M2C( this.msgs.invalid.msg, 'red' ) + etype = 'error' + + when HMSTATUS.generateError + msg = (ex.inner && ex.inner.toString()) || ex + quit = false + etype = 'error' + + when HMSTATUS.fileSaveError + msg = printf( M2C( this.msgs.fileSaveError.msg ), (ex.inner || ex).toString() ) + etype = 'error' + quit = false + + when HMSTATUS.invalidFormat + ex.data.forEach( (d) -> + msg += printf( M2C( this.msgs.invalidFormat.msg, 'bold' ), + ex.theme.name.toUpperCase(), d.format.toUpperCase()) + , @); + + when HMSTATUS.missingParam + msg = printf(M2C( this.msgs.missingParam.msg ), ex.expected, ex.helper) + + when HMSTATUS.invalidHelperUse + msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper ) + if ex.error + msg += '\n--> ' + assembleError.call( this, extend( true, {}, ex, {fluenterror: ex.error} )).msg; + #msg += printf( '\n--> ' + M2C( this.msgs.invalidParamCount.msg ), ex.expected ); + quit = false + etype = 'warning' + + when HMSTATUS.notOnPath + msg = printf( M2C(this.msgs.notOnPath.msg, 'bold'), ex.engine) + quit = false + etype = 'error' + + when HMSTATUS.readError + if !ex.quiet + console.error(printf( M2C(this.msgs.readError.msg, 'red'), ex.file)) + msg = ex.inner.toString() + etype = 'error' + + when HMSTATUS.mixedMerge + msg = M2C this.msgs.mixedMerge.msg + quit = false + + when HMSTATUS.invokeTemplate + msg = M2C this.msgs.invokeTemplate.msg, 'red' + msg += M2C( '\n' + WRAP(ex.inner.toString(), { width: 60, indent: ' ' }), 'gray' ); + etype = 'custom' + + when HMSTATUS.compileTemplate + etype = 'error' + + when HMSTATUS.themeLoad + msg = M2C( printf( this.msgs.themeLoad.msg, ex.attempted.toUpperCase() ), 'red'); + if ex.inner && ex.inner.fluenterror + msg += M2C('\nError: ', 'red') + assembleError.call( this, ex.inner ).msg + quit = true + etype = 'custom' + + when HMSTATUS.parseError + if SyntaxErrorEx.is( ex.inner ) + console.error printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) + se = new SyntaxErrorEx ex, ex.raw + msg = printf M2C( this.msgs.parseError.msg, 'red' ), se.line, se.col + else if ex.inner && ex.inner.line != undefined && ex.inner.col != undefined + msg = printf( M2C( this.msgs.parseError.msg, 'red' ), ex.inner.line, ex.inner.col) + else + msg = ex + etype = 'error' + + msg: msg # The error message to display + withStack: withStack # Whether to include the stack + quit: quit + etype: etype diff --git a/src/cli/error.js b/src/cli/error.js deleted file mode 100644 index cc94d54..0000000 --- a/src/cli/error.js +++ /dev/null @@ -1,280 +0,0 @@ -/** -Error-handling routines for HackMyResume. -@module cli/error -@license MIT. See LICENSE.md for details. -*/ - - - -(function() { - - - - var HMSTATUS = require('hackmycore/dist/core/status-codes') - , PKG = require('../../package.json') - , FS = require('fs') - , FCMD = require('hackmycore') - , PATH = require('path') - , WRAP = require('word-wrap') - , M2C = require('hackmycore/dist/utils/md2chalk.js') - , chalk = require('chalk') - , extend = require('extend') - , YAML = require('yamljs') - , printf = require('printf') - , SyntaxErrorEx = require('hackmycore/dist/utils/syntax-error-ex'); - require('string.prototype.startswith'); - - - - /** - Error handler for HackMyResume. All errors are handled here. - @class ErrorHandler - */ - var ErrorHandler = module.exports = { - - - - init: function( debug, assert, silent ) { - this.debug = debug; - this.assert = assert; - this.silent = silent; - this.msgs = require('./msg.js').errors; - return this; - }, - - - - err: function( ex, shouldExit ) { - - // Short-circuit logging output if --silent is on - var o = this.silent ? function() { } : _defaultLog; - - // Special case; can probably be removed. - if( ex.pass ) throw ex; - - // Load error messages - this.msgs = this.msgs || require('./msg.js').errors; - - // Handle packaged HMR exceptions - if( ex.fluenterror ) { - - // Output the error message - var objError = assembleError.call( this, ex ); - o( this[ 'format_' + objError.etype ]( objError.msg )); - - // Output the stack (sometimes) - if( objError.withStack ) { - var stack = ex.stack || (ex.inner && ex.inner.stack); - stack && o( chalk.gray( stack ) ); - } - - // Quit if necessary - if( ex.quit || objError.quit ) { - this.debug && o( - chalk.cyan('Exiting with error code ' + ex.fluenterror.toString())); - if( this.assert ) { ex.pass = true; throw ex; } - process.exit( ex.fluenterror ); - } - - } - - // Handle raw exceptions - else { - o( ex ); - var stackTrace = ex.stack || (ex.inner && ex.inner.stack); - if( stackTrace && this.debug ) - o( M2C(ex.stack || ex.inner.stack, 'gray') ); - } - - }, - - - - format_error: function( msg ) { - msg = msg || ''; - return chalk.red.bold( - msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg ); - }, - - - - format_warning: function( brief, msg ) { - return chalk.yellow(brief) + chalk.yellow(msg || ''); - }, - - - format_custom: function( msg ) { - return msg; - } - - - - }; - - - - function _defaultLog() { - console.log.apply( console.log, arguments ); - } - - - - function assembleError( ex ) { - - var msg = '', withStack = false, quit = false, etype = 'warning'; - if( this.debug ) withStack = true; - - switch( ex.fluenterror ) { - - case HMSTATUS.themeNotFound: - msg = printf( M2C( this.msgs.themeNotFound.msg, 'yellow' ), ex.data); - break; - - case HMSTATUS.copyCSS: - msg = M2C( this.msgs.copyCSS.msg, 'red' ); - quit = false; - break; - - case HMSTATUS.resumeNotFound: - msg = M2C( this.msgs.resumeNotFound.msg, 'yellow' ); - break; - - case HMSTATUS.missingCommand: - msg = M2C( this.msgs.missingCommand.msg + " (", 'yellow'); - msg += Object.keys( FCMD.verbs ).map( function(v, idx, ar) { - return (idx === ar.length - 1 ? chalk.yellow('or ') : '') + - chalk.yellow.bold(v.toUpperCase()); - }).join( chalk.yellow(', ')) + chalk.yellow(").\n\n"); - - msg += chalk.gray(FS.readFileSync( - PATH.resolve(__dirname, '../cli/use.txt'), 'utf8' )); - break; - - case HMSTATUS.invalidCommand: - msg = printf( M2C( this.msgs.invalidCommand.msg, 'yellow'), ex.attempted ); - break; - - case HMSTATUS.resumeNotFoundAlt: - msg = M2C( this.msgs.resumeNotFoundAlt.msg, 'yellow' ); - break; - - case HMSTATUS.inputOutputParity: - msg = M2C( this.msgs.inputOutputParity.msg ); - break; - - case HMSTATUS.createNameMissing: - msg = M2C( this.msgs.createNameMissing.msg ); - break; - - case HMSTATUS.pdfGeneration: - msg = M2C( this.msgs.pdfGeneration.msg, 'bold' ); - if( ex.inner ) msg += chalk.red('\n' + ex.inner); - withStack = true; quit = false; etype = 'error'; - break; - - case HMSTATUS.invalid: - msg = M2C( this.msgs.invalid.msg, 'red' ); - etype = 'error'; - break; - - case HMSTATUS.generateError: - msg = (ex.inner && ex.inner.toString()) || ex; - quit = false; - etype = 'error'; - break; - - case HMSTATUS.fileSaveError: - msg = printf( M2C( this.msgs.fileSaveError.msg ), (ex.inner || ex).toString() ); - etype = 'error'; - quit = false; - break; - - case HMSTATUS.invalidFormat: - ex.data.forEach(function(d){ - msg += printf( M2C( this.msgs.invalidFormat.msg, 'bold' ), - ex.theme.name.toUpperCase(), d.format.toUpperCase()); - }, this); - break; - - case HMSTATUS.missingParam: - msg = printf( M2C( this.msgs.missingParam.msg ), ex.expected, ex.helper ); - break; - - case HMSTATUS.invalidHelperUse: - msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper ); - if( ex.error ) { - msg += '\n--> ' + assembleError.call( this, extend( true, {}, ex, {fluenterror: ex.error} )).msg; - //msg += printf( '\n--> ' + M2C( this.msgs.invalidParamCount.msg ), ex.expected ); - } - quit = false; - etype = 'warning'; - break; - - case HMSTATUS.notOnPath: - msg = printf( M2C(this.msgs.notOnPath.msg, 'bold'), ex.engine); - quit = false; - etype = 'error'; - break; - - case HMSTATUS.readError: - if( !ex.quiet ) - console.error(printf( M2C(this.msgs.readError.msg, 'red'), ex.file)); - msg = ex.inner.toString(); - etype = 'error'; - break; - - case HMSTATUS.mixedMerge: - msg = M2C( this.msgs.mixedMerge.msg ); - quit = false; - break; - - case HMSTATUS.invokeTemplate: - msg = M2C( this.msgs.invokeTemplate.msg, 'red' ); - msg += M2C( '\n' + WRAP(ex.inner.toString(), { width: 60, indent: ' ' }), 'gray' ); - etype = 'custom'; - break; - - case HMSTATUS.compileTemplate: - etype = 'error'; - break; - - case HMSTATUS.themeLoad: - msg = M2C( printf( this.msgs.themeLoad.msg, ex.attempted.toUpperCase() ), 'red'); - if( ex.inner && ex.inner.fluenterror ) { - msg += M2C('\nError: ', 'red') + assembleError.call( this, ex.inner ).msg; - } - quit = true; - etype = 'custom'; - break; - - case HMSTATUS.parseError: - if( SyntaxErrorEx.is( ex.inner )) { - console.error( printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) ); - var se = new SyntaxErrorEx( ex, ex.raw ); - msg = printf( M2C( this.msgs.parseError.msg, 'red' ), - se.line, se.col); - } - else if( ex.inner && ex.inner.line !== undefined && ex.inner.col !== undefined ) { - msg = printf( M2C( this.msgs.parseError.msg, 'red' ), - ex.inner.line, ex.inner.col); - } - else { - msg = ex; - } - etype = 'error'; - break; - - } - - return { - msg: msg, // The error message to display - withStack: withStack, // Whether to include the stack - quit: quit, - etype: etype - }; - } - - - - -}()); diff --git a/src/cli/main.coffee b/src/cli/main.coffee index e718552..d65e920 100644 --- a/src/cli/main.coffee +++ b/src/cli/main.coffee @@ -33,7 +33,7 @@ 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) -> initInfo = initialize( rawArgs ) args = initInfo.args @@ -175,21 +175,20 @@ initOptions = ( ar ) -> oVerb verb = '' args = ar.slice() - cleanArgs = args.slice(2) + cleanArgs = args.slice( 2 ) oJSON if cleanArgs.length # Support case-insensitive sub-commands (build, generate, validate, etc) - vidx = _.findIndex( cleanArgs, (v) -> return v[0] != '-' ) + vidx = _.findIndex cleanArgs, (v) -> v[0] != '-' if vidx != -1 oVerb = cleanArgs[ vidx ] verb = args[ vidx + 2 ] = oVerb.trim().toLowerCase() # Remove --options --opts -o and process separately - optsIdx = _.findIndex( cleanArgs, (v) -> - return v == '-o' || v == '--options' || v == '--opts' - ); + optsIdx = _.findIndex cleanArgs, (v) -> + v == '-o' || v == '--options' || v == '--opts' if optsIdx != -1 optStr = cleanArgs[ optsIdx + 1] @@ -206,7 +205,6 @@ initOptions = ( ar ) -> oJSON = inf.json # TODO: Error handling - # Grab the --debug flag isDebug = _.some( args, (v) -> return v == '-d' || v == '--debug' @@ -217,7 +215,11 @@ initOptions = ( ar ) -> return v == '-s' || v == '--silent' ) + # Grab the --no-color flag + isMono = _.some args, (v) -> v == '--no-color' + return { + color: !isMono, debug: isDebug, silent: isSilent, orgVerb: oVerb, @@ -279,11 +281,11 @@ loadOptions = ( o, cmdO ) -> logMsg(''); # Cache - EXTEND(true, _opts, o) + EXTEND( true, _opts, o ) return ### Split multiple command-line filenames by the 'TO' keyword ### -splitSrcDest = -> +splitSrcDest = () -> params = this.parent.args.filter((j) -> return String.is(j) ) if params.length == 0 @@ -309,5 +311,5 @@ splitSrcDest = -> ### Simple logging placeholder. ### -logMsg = -> +logMsg = () -> _opts.silent || console.log.apply( console.log, arguments ) diff --git a/src/cli/msg.coffee b/src/cli/msg.coffee new file mode 100644 index 0000000..1cf9021 --- /dev/null +++ b/src/cli/msg.coffee @@ -0,0 +1,10 @@ +###* +Message-handling routines for HackMyResume. +@module cli/msg +@license MIT. See LICENSE.md for details. +### + + +PATH = require 'path' +YAML = require 'yamljs' +module.exports = YAML.load PATH.join __dirname, 'msg.yml' diff --git a/src/cli/msg.js b/src/cli/msg.js deleted file mode 100644 index ce4d2b5..0000000 --- a/src/cli/msg.js +++ /dev/null @@ -1,18 +0,0 @@ -/** -Message-handling routines for HackMyResume. -@module msg.js -@license MIT. See LICENSE.md for details. -*/ - - - -(function() { - - var PATH = require('path'); - var YAML = require('yamljs'); - - var cache = module.exports = function() { - return cache ? cache : YAML.load( PATH.join(__dirname, 'msg.yml') ); - }(); - -}()); diff --git a/src/cli/out.js b/src/cli/out.js index e3815ad..c06d1b3 100644 --- a/src/cli/out.js +++ b/src/cli/out.js @@ -25,6 +25,7 @@ Output routines for HackMyResume. , pad = require('string-padding') , dbgStyle = 'cyan'; + chalk.enabled = false; /** @@ -159,7 +160,7 @@ Output routines for HackMyResume. case HME.afterAnalyze: var info = evt.info; var rawTpl = FS.readFileSync( PATH.join( __dirname, 'analyze.hbs' ), 'utf8'); - HANDLEBARS.registerHelper( require('hackmycore/src/helpers/console-helpers') ); + HANDLEBARS.registerHelper( require('hackmycore/dist/helpers/console-helpers') ); var template = HANDLEBARS.compile(rawTpl, { strict: false, assumeObjects: false }); var tot = 0; info.keywords.forEach(function(g) { tot += g.count; }); diff --git a/test/test-stdout.js b/test/test-stdout.js index c2f396c..bfdac30 100644 --- a/test/test-stdout.js +++ b/test/test-stdout.js @@ -12,6 +12,7 @@ var chai = require('chai') , FS = require('fs') , PATH = require('path') , PKG = require('../package.json') + , STRIPCOLOR = require('stripcolorcodes') , _ = require('underscore'); @@ -27,7 +28,8 @@ describe('Testing Ouput interface', function () { // TODO: use sinon // Replacement for console.log function MyConsoleLog( msg ) { - gather += Array.prototype.slice.call(arguments).join(' '); + var tx = Array.prototype.slice.call(arguments).join(' '); + gather += STRIPCOLOR( tx ); ConsoleLogOrg.apply(this, arguments); }