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

209 lines
5.5 KiB
JavaScript
Raw Normal View History

#! /usr/bin/env node
/**
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')
2015-12-19 17:37:42 +00:00
, FCMD = require( './hackmycmd')
2015-10-26 17:17:58 +00:00
, PKG = require('../package.json')
, FS = require('fs')
, chalk = require('chalk')
, PATH = require('path')
2015-12-29 10:09:05 +00:00
, HACKMYSTATUS = require('./core/status-codes')
2015-11-21 12:59:30 +00:00
, 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('-s, --silent', 'Run in silent mode.')
.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 ) {
2016-01-03 07:39:43 +00:00
FCMD.verbs[ this.name() ].call( null, sources, [], this.opts(), logMsg);
2016-01-03 07:22:26 +00:00
});
// Create the VALIDATE command
program
.command('validate')
.arguments('<sources...>')
.description('Validate a resume in FRESH or JSON RESUME format.')
.action(function(sources) {
2016-01-03 07:39:43 +00:00
FCMD.verbs[ this.name() ].call( null, 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 );
2016-01-03 07:39:43 +00:00
FCMD.verbs[ this.name() ].call( null, 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 ) {
FCMD.verbs[ this.name() ].call( null, 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('...')
.option('-t --theme <theme>', 'Theme name or path')
.option('-p --prettify', 'Preffity HTML output.')
.description('Generate resume to multiple formats')
.action(function( sources, targets, options ) {
var x = splitSrcDest.call( this );
2016-01-03 07:39:43 +00:00
FCMD.verbs[ this.name() ].call( null, 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 }; }
}
/**
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
2016-01-03 07:22:26 +00:00
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('.') );
2015-11-21 15:33:16 +00:00
return;
}
2016-01-03 07:22:26 +00:00
return {
src: params.slice(0, splitAt === -1 ? undefined : splitAt ),
dst: splitAt === -1 ? [] : params.slice( splitAt + 1 )
};
}
2016-01-03 07:22:26 +00:00
/**
Massage command-line args and setup Commander.js.
*/
function initialize() {
// 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-03 07:39:43 +00:00
if( verb && !FCMD.verbs[ verb ] && !FCMD.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) {
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;
}
2015-10-07 14:29:41 +01:00
2016-01-03 07:22:26 +00:00
/**
Simple logging placeholder.
*/
function logMsg( msg ) {
opts.silent || console.log( msg );
}
2016-01-03 07:22:26 +00:00
/**
Fetch options from command line arguments.
*/
function getOpts( args ) {
2015-12-10 02:44:35 +00:00
var noPretty = args.nopretty || args.n;
noPretty = noPretty && (noPretty === true || noPretty === 'true');
return {
theme: args.t || 'modern',
2015-12-02 19:56:36 +00:00
format: args.f || 'FRESH',
prettify: !noPretty,
2015-12-31 00:45:50 +00:00
silent: args.s || args.silent,
css: args.css || 'embed',
help: args.help || undefined
};
}