1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 01:56:21 +00:00

Modularize messages.

...and move strings out of error.js.
This commit is contained in:
hacksalot 2016-01-15 23:46:43 -05:00
parent fc67f680ee
commit 17f2ebb753
4 changed files with 117 additions and 75 deletions

View File

@ -18,6 +18,8 @@ Error-handling routines for HackMyResume.
, WRAP = require('word-wrap')
, M2C = require('../utils/md2chalk.js')
, chalk = require('chalk')
, YAML = require('yamljs')
, printf = require('printf')
, SyntaxErrorEx = require('../utils/syntax-error-ex');
require('string.prototype.startswith');
@ -32,18 +34,23 @@ Error-handling routines for HackMyResume.
init: function( debug, assert ) {
this.debug = debug;
this.assert = assert;
this.msgs = require('./msg.js').errors;
return this;
},
err: function( ex, shouldExit ) {
if( !this.msgs ) {
this.msgs = require('./msg.js').errors;
}
if( ex.pass )
throw ex;
if( ex.fluenterror ) {
// Output the error message
var objError = assembleError( ex );
var objError = assembleError.call( this, ex );
o( this[ 'format' + (objError.warning ? 'Warning' : 'Error')](
objError.msg
));
@ -95,25 +102,20 @@ Error-handling routines for HackMyResume.
switch( ex.fluenterror ) {
case HACKMYSTATUS.themeNotFound:
msg =
chalk.bold("Couldn't find the '" + ex.data + "' theme."),
" Please specify the name of a preinstalled FRESH theme " +
"or the path to a locally installed FRESH or JSON Resume theme.";
msg = printf( M2C( this.msgs.themeNotFound.msg, 'yellow' ), ex.data);
break;
case HACKMYSTATUS.copyCSS:
msg = "Couldn't copy CSS file to destination folder.";
msg = M2C( this.msgs.copyCSS.msg, 'red' );
quit = false;
break;
case HACKMYSTATUS.resumeNotFound:
msg = 'Please ' + chalk.bold('feed me a resume') +
' in FRESH or JSON Resume format.';
msg = M2C( this.msgs.resumeNotFound.msg, 'yellow' );
break;
case HACKMYSTATUS.missingCommand:
msg = "Please " +chalk.bold("give me a command") + " (";
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());
@ -124,49 +126,41 @@ Error-handling routines for HackMyResume.
break;
case HACKMYSTATUS.invalidCommand:
msg = 'Invalid command: "'+chalk.bold(ex.attempted)+'"';
msg = printf( M2C( this.msgs.invalidCommand.msg, 'yellow'), ex.attempted );
break;
case HACKMYSTATUS.resumeNotFoundAlt:
msg = 'Please ' + chalk.bold('feed me a resume') +
' in either FRESH or JSON Resume format.';
msg = M2C( this.msgs.resumeNotFoundAlt.msg, 'yellow' );
break;
case HACKMYSTATUS.inputOutputParity:
msg = 'Please ' +
chalk.bold('specify an output file name') +
' for every input file you wish to convert.';
msg = M2C( this.msgs.inputOutputParity.msg );
break;
case HACKMYSTATUS.createNameMissing:
msg = 'Please ' +
chalk.bold('specify the filename of the resume') + ' to create.';
msg = M2C( this.msgs.createNameMissing.msg );
break;
case HACKMYSTATUS.pdfGeneration:
msg = chalk.bold('PDF generation failed. ') +
'Make sure wkhtmltopdf is installed and accessible from your path.';
msg = M2C( this.msgs.pdfGeneration.msg, 'bold' );
if( ex.inner ) msg += chalk.red('\n' + ex.inner);
withStack = true; quit = false; warn = false;
break;
case HACKMYSTATUS.invalid:
msg = 'Validation failed and the --assert option was ' +
'specified.';
msg = M2C( this.msgs.invalid.msg, 'red' );
warn = false;
break;
case HACKMYSTATUS.invalidFormat:
ex.data.forEach(function(d){ msg +=
'The ' + chalk.bold(ex.theme.name.toUpperCase()) +
" theme doesn't support the " + chalk.bold(d.format.toUpperCase()) +
" format.\n";
ex.data.forEach(function(d){
msg += printf( M2C( this.msgs.invalidFormat.msg, 'bold' ),
ex.theme.name.toUpperCase(), d.format.toUpperCase());
});
break;
case HACKMYSTATUS.notOnPath:
msg = ex.engine + " wasn't found on your system path or" +
" is inaccessible. PDF not generated.";
msg = printf( M2C(this.msgs.notOnPath, 'bold'), ex.engine);
quit = false;
warn = false;
break;
@ -179,8 +173,8 @@ Error-handling routines for HackMyResume.
case HACKMYSTATUS.parseError:
if( SyntaxErrorEx.is( ex.inner )) {
var se = new SyntaxErrorEx( ex, ex.raw );
msg = 'Invalid or corrupt JSON on line ' + se.line +
' column ' + se.col + '.';
msg = printf( M2C( this.msgs.parseError.msg, 'red' ),
se.line, se.col);
}
else {
msg = ex;

18
src/cli/msg.js Normal file
View File

@ -0,0 +1,18 @@
/**
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') );
}();
}());

View File

@ -1,46 +1,76 @@
beforeCreate:
msg: Creating new **%s** resume: **%s**
beforeRead:
msg: Reading resume: **%s**
beforeTheme:
msg: Verifying theme: **%s**
afterTheme:
msg: Verifying outputs: ???
beforeMerge:
msg:
- Merging **%s**
- " onto **%s**"
afterMerge:
msg: Applying **%s** theme (%s format%s)
afterBuild:
msg:
- "The **%s** theme says:"
- |
"For best results view JSON Resume themes over a
local or remote HTTP connection. For example:
events:
beforeCreate:
msg: Creating new **%s** resume: **%s**
beforeRead:
msg: Reading resume: **%s**
beforeTheme:
msg: Verifying theme: **%s**
afterTheme:
msg: Verifying outputs: ???
beforeMerge:
msg:
- Merging **%s**
- " onto **%s**"
afterMerge:
msg: Applying **%s** theme (%s format%s)
afterBuild:
msg:
- "The **%s** theme says:"
- |
"For best results view JSON Resume themes over a
local or remote HTTP connection. For example:
npm install http-server -g
http-server <resume-folder>
npm install http-server -g
http-server <resume-folder>
For more information, see the README."
beforeGenerate:
msg:
- " (with %s)"
- "Skipping %s resume: %s"
- "Generating **%s** resume: **%s**"
beforeAnalyze:
msg: "Analyzing **%s** resume: **%s**"
beforeConvert:
msg: "Converting **%s** (**%s**) to **%s** (**%s**)"
afterValidate:
msg:
- "Validating **%s** against the **%s** schema: "
- "VALID!"
- "INVALID"
- "BROKEN"
beforePeek:
msg:
- Peeking at **%s** in **%s**
- Peeking at **%s**
afterPeek:
msg: "The specified key **%s** was not found in **%s**."
For more information, see the README."
beforeGenerate:
msg:
- " (with %s)"
- "Skipping %s resume: %s"
- "Generating **%s** resume: **%s**"
beforeAnalyze:
msg: "Analyzing **%s** resume: **%s**"
beforeConvert:
msg: "Converting **%s** (**%s**) to **%s** (**%s**)"
afterValidate:
msg:
- "Validating **%s** against the **%s** schema: "
- "VALID!"
- "INVALID"
- "BROKEN"
beforePeek:
msg:
- Peeking at **%s** in **%s**
- Peeking at **%s**
afterPeek:
msg: "The specified key **%s** was not found in **%s**."
errors:
themeNotFound:
msg: >
Couldn't find the '%s' theme. Please specify the name of a preinstalled
FRESH theme or the path to a locally installed FRESH or JSON Resume theme.
copyCSS:
msg: Couldn't copy CSS file to destination folder.
resumeNotFound:
msg: Please **feed me a resume** in FRESH or JSON Resume format.
missingCommand:
msg: Please **give me a command**
invalidCommand:
msg: Invalid command: '%s'
resumeNotFoundAlt:
msg: Please **feed me a resume** in either FRESH or JSON Resume format.
inputOutputParity:
msg: Please **specify an output file name** for every input file you wish to convert.
createNameMissing:
msg: Please **specify the filename** of the resume to create.
pdfGeneration:
msg: PDF generation failed. Make sure wkhtmltopdf is installed and accessible from your path.
invalid:
msg: Validation failed and the --assert option was specified.
invalidFormat:
msg: The **%s** theme doesn't support the **%s** format.
notOnPath:
msg: %s wasn't found on your system path or is inaccessible. PDF not generated.
parseError:
msg: Invalid or corrupt JSON on line %s column %s.

View File

@ -35,7 +35,7 @@ Output routines for HackMyResume.
init: function( opts ) {
this.opts = EXTEND( true, this.opts || { }, opts );
this.msgs = YAML.load(PATH.join( __dirname, 'msg.yml' ));
this.msgs = YAML.load(PATH.join( __dirname, 'msg.yml' )).events;
},