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

Move CLI-related assets to subfolder.

This commit is contained in:
hacksalot
2016-01-09 16:14:28 -05:00
parent bece335a64
commit c5b8eec33a
6 changed files with 18 additions and 17 deletions

245
src/cli/main.js Normal file
View File

@ -0,0 +1,245 @@
/**
Definition of the `main` function.
@module main.js
@license MIT. See LICENSE.md for details.
*/
(function(){
var HMR = require( '../hackmyapi')
, PKG = require('../../package.json')
, FS = require('fs')
, EXTEND = require('../utils/extend')
, chalk = require('chalk')
, PATH = require('path')
, HACKMYSTATUS = require('../core/status-codes')
, HME = require('../core/event-codes')
, safeLoadJSON = require('../utils/safe-json-loader')
, _opts = { }
, title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***')
, StringUtils = require('../utils/string.js')
, _ = require('underscore')
, OUTPUT = require('./out')
, Command = require('commander').Command;
/**
Main function for HackMyResume
@license MIT. See LICENSE.md for details.
@module main.js
*/
var main = module.exports = function( args ) {
args = initialize( args );
// Create the top-level (application) command...
var program = new Command('hackmyresume')
.version(PKG.version)
.description(chalk.yellow.bold('*** HackMyResume ***'))
.option('-o --opts <optionsFile>', 'Path to a .hackmyrc options file')
.option('-s --silent', 'Run in silent mode')
.option('--no-color', 'Disable colors')
.option('--color', 'Enable colors')
.option('-d --debug', 'Enable diagnostics', false);
//.usage('COMMAND <sources> [TO <targets>]');
// Create the NEW command
program
.command('new')
.arguments('<sources...>')
.option('-f --format <fmt>', 'FRESH or JRS format', 'FRESH')
.alias('create')
.description('Create resume(s) in FRESH or JSON RESUME format.')
.action(function( sources ) {
execVerb.call( this, sources, [], this.opts(), logMsg);
});
// Create the VALIDATE command
program
.command('validate')
.arguments('<sources...>')
.option('-a --assert', 'Treat validation warnings as errors', false)
.description('Validate a resume in FRESH or JSON RESUME format.')
.action(function(sources) {
execVerb.call( this, sources, [], this.opts(), logMsg);
});
// Create the CONVERT command
program
.command('convert')
//.arguments('<sources...>')
.description('Convert a resume to/from FRESH or JSON RESUME format.')
.action(function() {
var x = splitSrcDest.call( this );
execVerb.call( this, x.src, x.dst, this.opts(), logMsg);
});
// Create the ANALYZE command
program
.command('analyze')
.arguments('<sources...>')
.description('Analyze one or more resumes.')
.action(function( sources ) {
execVerb.call( this, sources, [], this.opts(), logMsg);
});
// Create the BUILD command
program
.command('build')
.alias('generate')
//.arguments('<sources> TO [targets]')
//.usage('...')
.option('-t --theme <theme>', 'Theme name or path')
.option('-n --no-prettify', 'Disable HTML prettification', true)
.option('-c --css <option>', 'CSS linking / embedding', 'embed')
.option('-p --pdf <engine>', 'PDF generation engine')
.option('--no-tips', 'Disable theme tips and warnings.', false)
.description('Generate resume to multiple formats')
.action(function( sources, targets, options ) {
var x = splitSrcDest.call( this );
execVerb.call( this, x.src, x.dst, this.opts(), logMsg);
});
// program.on('--help', function(){
// console.log(' Examples:');
// console.log('');
// console.log(' $ custom-help --help');
// console.log(' $ custom-help -h');
// console.log('');
// });
program.parse( args );
if (!program.args.length) { throw { fluenterror: 4 }; }
};
/**
Massage command-line args and setup Commander.js.
*/
function initialize( ar ) {
logMsg( title );
// Support case-insensitive sub-commands (build, generate, validate, etc.)..
var oVerb, verb = '', args = ar.slice(), cleanArgs = args.slice(2);
if( cleanArgs.length ) {
var verbIdx = _.findIndex( cleanArgs, function(v){ return v[0] !== '-'; });
if( verbIdx !== -1 ) {
oVerb = cleanArgs[ verbIdx ];
verb = args[ verbIdx + 2 ] = oVerb.trim().toLowerCase();
}
}
// Handle invalid verbs here (a bit easier here than in commander.js)...
if( verb && !HMR.verbs[ verb ] && !HMR.alias[ verb ] ) {
throw { fluenterror: HACKMYSTATUS.invalidCommand, shouldExit: true,
attempted: oVerb };
}
// Override the .missingArgument behavior
Command.prototype.missingArgument = function(name) {
if( this.name() !== 'new' )
throw { fluenterror: HACKMYSTATUS.resumeNotFound };
};
// Override the .helpInformation behavior
Command.prototype.helpInformation = function() {
var manPage = FS.readFileSync(
PATH.join(__dirname, 'use.txt'), 'utf8' );
return chalk.green.bold(manPage);
};
return args;
}
/**
Invoke a HackMyResume verb.
*/
function execVerb( src, dst, opts, log ) {
loadOptions.call( this, opts );
require( '../core/error-handler' ).init( _opts.debug );
var out = new OUTPUT( _opts );
var v = new HMR.verbs[ this.name() ]();
v.on( 'hmr:status', function() { out.do.apply( out, arguments ); });
v.invoke.call( v, src, dst, _opts, log );
}
/**
Initialize HackMyResume options.
*/
function loadOptions( o ) {
o.opts = this.parent.opts;
// Load the specified options file (if any) and apply options
if( o.opts && String.is( o.opts )) {
var json = safeLoadJSON( PATH.relative( process.cwd(), o.opts ) );
json && ( o = EXTEND( true, o, json ) );
if( !json ) {
throw safeLoadJSON.error;
}
}
// Merge in command-line options
o = EXTEND( true, o, this.opts() );
o.silent = this.parent.silent;
o.debug = this.parent.debug;
_opts = o;
}
/**
Split multiple command-line filenames by the 'TO' keyword
*/
function splitSrcDest() {
var params = this.parent.args.filter(function(j) { return String.is(j); });
if( params.length === 0 )
throw { fluenterror: HACKMYSTATUS.resumeNotFound };
// Find the TO keyword, if any
var splitAt = _.findIndex( params, function(p) {
return p.toLowerCase() === 'to';
});
// TO can't be the last keyword
if( splitAt === params.length - 1 && splitAt !== -1 ) {
logMsg(chalk.yellow('Please ') +
chalk.yellow.bold('specify an output file') +
chalk.yellow(' for this operation or ') +
chalk.yellow.bold('omit the TO keyword') +
chalk.yellow('.') );
return;
}
return {
src: params.slice(0, splitAt === -1 ? undefined : splitAt ),
dst: splitAt === -1 ? [] : params.slice( splitAt + 1 )
};
}
/**
Simple logging placeholder.
*/
function logMsg( msg ) {
msg = msg || '';
_opts.silent || console.log( msg );
}
}());

187
src/cli/out.js Normal file
View File

@ -0,0 +1,187 @@
/**
Output routines for HackMyResume.
@license MIT. See LICENSE.md for details.
@module out.js
*/
(function() {
var chalk = require('chalk')
, HME = require('../core/event-codes')
, _ = require('underscore')
, Class = require('../utils/class.js')
, PATH = require('path')
, pad = require('string-padding');
/**
A stateful output handler. All HMR console output handled here.
*/
var OutputHandler = module.exports = Class.extend({
init: function( opts ) {
this.opts = opts;
},
log: function( msg ) {
msg = msg || '';
this.opts.silent || console.log( msg );
},
do: function( evt ) {
switch( evt.sub ) {
case HME.beforeCreate:
this.log( chalk.green('Creating new ') +
chalk.green.bold(evt.fmt) +
chalk.green(' resume: ') + chalk.green.bold(evt.file));
break;
case HME.afterTheme:
this.theme = evt.theme;
break;
case HME.beforeMerge:
var msg = '';
evt.f.reverse().forEach( function( a, idx ) {
msg += ((idx === 0) ? chalk.cyan('Merging ') :
chalk.cyan(' onto ')) + chalk.cyan.bold(a.i().file);
});
this.log( msg );
break;
case HME.afterMerge:
var numFormats = Object.keys(this.theme.formats).length;
this.log( chalk.yellow('Applying ') +
chalk.yellow.bold( this.theme.name.toUpperCase() ) +
chalk.yellow(' theme (' + numFormats + ' format' +
( evt.numFormats === 1 ? ')' : 's)') ));
break;
case HME.end:
if( evt.cmd === 'build' ) {
var themeName = this.theme.name.toUpperCase();
if( this.opts.tips && (this.theme.message || this.theme.render) ) {
var WRAP = require('word-wrap');
if( this.theme.message ) {
this.log( WRAP( chalk.gray('The ' + themeName + ' theme says: "') +
chalk.white(this.theme.message) + chalk.gray('"'),
{ width: this.opts.wrap, indent: '' } ));
}
else if ( this.theme.render ) {
this.log( WRAP( chalk.gray('The ' + themeName +
' theme says: "') + chalk.white('For best results view JSON ' +
'Resume themes over a local or remote HTTP connection. For ' +
'example:'), { width: this.opts.wrap, indent: '' }
));
this.log( '');
this.log(
' npm install http-server -g\r' +
' http-server <resume-folder>' );
this.log('');
this.log(chalk.white('For more information, see the README."'),
{ width: this.opts.wrap, indent: '' } );
}
}
}
break;
case HME.beforeGenerate:
var suffix = '';
if( evt.fmt === 'pdf' ) {
if( this.opts.pdf ) {
if( this.opts.pdf !== 'none' ) {
suffix = chalk.green(' (with ' + this.opts.pdf + ')');
}
else {
this.log( chalk.gray('Skipping ') +
chalk.white.bold( pad(evt.fmt.toUpperCase(),4,null,pad.RIGHT)) +
chalk.gray(' resume') + suffix + chalk.green(': ') +
chalk.white( evt.file ));
return;
}
}
}
this.log( chalk.green('Generating ') +
chalk.green.bold(
pad(evt.fmt.toUpperCase(),4,null,pad.RIGHT)) +
chalk.green(' resume') + suffix + chalk.green(': ') +
chalk.green.bold( PATH.relative(process.cwd(), evt.file )) );
break;
case HME.beforeAnalyze:
this.log(chalk.cyan('Analyzing ') + chalk.cyan.bold(evt.fmt) +
chalk.cyan(' resume: ') + chalk.cyan.bold(evt.file));
break;
case HME.afterAnalyze:
// TODO: templatize all of this
var info = evt.info;
var padding = 20;
this.log(chalk.cyan.bold('\nSECTIONS') + chalk.cyan(' (') +
chalk.white.bold(_.keys(info.totals).length) + chalk.cyan('):\n'));
_.each( info.totals, function(tot, key) {
this.log(chalk.cyan(pad(key + ': ',20)) +
chalk.cyan.bold(pad(tot.toString(),5)));
}, this);
this.log();
this.log(chalk.cyan.bold('COVERAGE') + chalk.cyan(' (') + chalk.white.bold( info.coverage.pct ) + chalk.cyan('):\n'));
this.log(chalk.cyan(pad('Total Days: ', padding)) + chalk.cyan.bold( pad(info.coverage.duration.total.toString(),5) ));
this.log(chalk.cyan(pad('Employed: ', padding)) + chalk.cyan.bold( pad((info.coverage.duration.total - info.coverage.duration.gaps).toString(),5) ));
this.log(chalk.cyan(pad('Gaps: ', padding + 4)) + chalk.cyan.bold(info.coverage.gaps.length) + chalk.cyan(' [') + info.coverage.gaps.map(function(g) {
var clr = 'green';
if( g.duration > 35 ) clr = 'yellow';
if( g.duration > 90 ) clr = 'red';
return chalk[clr].bold( g.duration) ;
}).join(', ') + chalk.cyan(']') );
this.log(chalk.cyan(pad('Overlaps: ', padding + 4)) + chalk.cyan.bold(info.coverage.overlaps.length) + chalk.cyan(' [') + info.coverage.overlaps.map(function(ol) {
var clr = 'green';
if( ol.duration > 35 ) clr = 'yellow';
if( ol.duration > 90 ) clr = 'red';
return chalk[clr].bold( ol.duration) ;
}).join(', ') + chalk.cyan(']') );
var tot = 0;
this.log();
this.log( chalk.cyan.bold('KEYWORDS') + chalk.cyan(' (') + chalk.white.bold( info.keywords.length ) +
chalk.cyan('):\n\n') +
info.keywords.map(function(g) {
tot += g.count;
return chalk.cyan( pad(g.name + ': ', padding) ) + chalk.cyan.bold( pad( g.count.toString(), 5 )) + chalk.cyan(' mentions');
}).join('\n'));
this.log(chalk.cyan( pad('TOTAL: ', padding) ) + chalk.white.bold( pad( tot.toString(), 5 )) + chalk.cyan(' mentions'));
break;
case HME.beforeConvert:
// TODO: Core should not log
this.log( chalk.green('Converting ') + chalk.green.bold(evt.srcFile) +
chalk.green(' (' + evt.srcFmt + ') to ') + chalk.green.bold(evt.dstFile) +
chalk.green(' (' + evt.dstFmt + ').'));
break;
}
}
});
}());

27
src/cli/use.txt Normal file
View File

@ -0,0 +1,27 @@
Usage:
hackmyresume <COMMAND> <SOURCES> [TO <TARGETS>] [-t <THEME>] [-f <FORMAT>]
<COMMAND> should be BUILD, NEW, CONVERT, VALIDATE, ANALYZE or HELP. <SOURCES>
should be the path to one or more FRESH or JSON Resume format resumes. <TARGETS>
should be the name of the destination resume to be created, if any. The <THEME>
parameter should be the name of a predefined theme (for example: COMPACT,
MINIMIST, MODERN, or HELLO-WORLD) or the relative path to a custom theme.
<FORMAT> should be either FRESH (for a FRESH-format resume) or JRS (for a JSON
Resume-format resume).
hackmyresume BUILD resume.json TO out/resume.all
hackmyresume NEW resume.json
hackmyresume CONVERT resume.json TO resume-jrs.json
hackmyresume ANALYZE resume.json
hackmyresume VALIDATE resume.json
Both SOURCES and TARGETS can accept multiple files:
hackmyresume BUILD r1.json r2.json TO out/resume.all out2/resume.html
hackmyresume NEW r1.json r2.json r3.json
hackmyresume ANALYZE r1.json r2.json r3.json
hackmyresume VALIDATE resume.json resume2.json resume3.json
See https://github.com/hacksalot/hackmyresume/blob/master/README.md for more
information.