1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-07-05 09:30:04 +01:00
HackMyResume/src/index.js

246 lines
6.4 KiB
JavaScript
Raw Normal View History

#! /usr/bin/env node
2016-01-03 08:18:56 +00:00
/**
2015-12-19 17:37:42 +00:00
Command-line interface (CLI) for HackMyResume.
@license MIT. Copyright (c) 2015 hacksalot (https://github.com/hacksalot)
2015-12-17 15:15:59 +00:00
@module index.js
*/
var SPAWNW = require('./core/spawn-watch')
2016-01-04 12:23:20 +00:00
, HMR = require( './hackmyapi')
2015-10-26 17:17:58 +00:00
, PKG = require('../package.json')
, FS = require('fs')
2016-01-04 07:50:00 +00:00
, EXTEND = require('./utils/extend')
, chalk = require('chalk')
, PATH = require('path')
2015-12-29 10:09:05 +00:00
, HACKMYSTATUS = require('./core/status-codes')
2016-01-04 07:50:00 +00:00
, safeLoadJSON = require('./utils/safe-json-loader')
, _opts = { }
2016-01-02 05:15:46 +00:00
, title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***')
2016-01-03 07:22:26 +00:00
, StringUtils = require('./utils/string.js')
, _ = require('underscore')
, Command = require('commander').Command;
2015-10-26 17:17:58 +00:00
try {
main();
}
catch( ex ) {
require('./core/error-handler').err( ex, true );
}
2015-10-26 17:17:58 +00:00
2016-01-03 07:22:26 +00:00
/**
Kick off the HackMyResume application.
*/
function main() {
2016-01-03 07:22:26 +00:00
var args = initialize();
2016-01-03 07:22:26 +00:00
// 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>]');
2016-01-03 07:22:26 +00:00
// 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);
2016-01-03 07:22:26 +00:00
});
// Create the VALIDATE command
program
.command('validate')
.arguments('<sources...>')
.option('-a --assert', 'Treat validation warnings as errors', false)
2016-01-03 07:22:26 +00:00
.description('Validate a resume in FRESH or JSON RESUME format.')
.action(function(sources) {
execVerb.call( this, sources, [], this.opts(), logMsg);
2016-01-03 07:22:26 +00:00
});
// 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);
2016-01-03 07:22:26 +00:00
});
// Create the ANALYZE command
program
.command('analyze')
.arguments('<sources...>')
.description('Analyze one or more resumes.')
2016-01-03 07:39:43 +00:00
.action(function( sources ) {
execVerb.call( this, sources, [], this.opts(), logMsg);
2016-01-03 07:22:26 +00:00
});
// Create the BUILD command
program
.command('build')
.alias('generate')
//.arguments('<sources> TO [targets]')
//.usage('...')
2016-01-04 07:50:00 +00:00
.option('-t --theme <theme>', 'Theme name or path')
.option('-n --no-prettify', 'Disable HTML prettification', true)
2016-01-03 08:18:56 +00:00
.option('-c --css <option>', 'CSS linking / embedding', 'embed')
.option('-p --pdf <engine>', 'PDF generation engine')
.option('--no-tips', 'Disable theme tips and warnings.', false)
2016-01-03 07:22:26 +00:00
.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);
2016-01-03 07:22:26 +00:00
});
// 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 }; }
}
2016-01-03 07:22:26 +00:00
/**
Massage command-line args and setup Commander.js.
*/
function initialize() {
2016-01-05 14:38:21 +00:00
logMsg( title );
2016-01-03 07:22:26 +00:00
// Support case-insensitive sub-commands (build, generate, validate, etc.)..
var oVerb, verb = '', args = process.argv.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)...
2016-01-04 12:23:20 +00:00
if( verb && !HMR.verbs[ verb ] && !HMR.alias[ verb ] ) {
2016-01-03 07:22:26 +00:00
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 };
2016-01-03 07:22:26 +00:00
};
// 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;
}
2015-10-07 14:29:41 +01:00
2016-01-03 07:22:26 +00:00
2016-01-04 07:50:00 +00:00
/**
Invoke a HackMyResume verb.
*/
function execVerb( src, dst, opts, log ) {
loadOptions.call( this, opts );
require('./core/error-handler').init( _opts.debug );
2016-01-04 12:23:20 +00:00
HMR.verbs[ this.name() ].call( null, src, dst, _opts, log );
2016-01-04 07:50:00 +00:00
}
/**
Initialize HackMyResume options.
*/
function loadOptions( o ) {
2016-01-04 07:50:00 +00:00
o.opts = this.parent.opts;
2016-01-04 07:50:00 +00:00
// 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 ) );
2016-01-04 07:50:00 +00:00
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;
2016-01-04 07:50:00 +00:00
}
2016-01-03 07:22:26 +00:00
/**
2016-01-03 08:18:56 +00:00
Split multiple command-line filenames by the 'TO' keyword
2016-01-03 07:22:26 +00:00
*/
2016-01-03 08:18:56 +00:00
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 )
};
}
2016-01-03 07:22:26 +00:00
/**
2016-01-03 08:18:56 +00:00
Simple logging placeholder.
2016-01-03 07:22:26 +00:00
*/
2016-01-03 08:18:56 +00:00
function logMsg( msg ) {
2016-01-03 10:03:31 +00:00
msg = msg || '';
_opts.silent || console.log( msg );
}