1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-22 08:20:11 +00:00

Support raw JSON in the --options parameter.

This commit is contained in:
hacksalot 2016-01-11 18:07:56 -05:00
parent fbc2e9a4db
commit 11bfcd4bef
2 changed files with 87 additions and 37 deletions

View File

@ -24,6 +24,8 @@ Definition of the `main` function.
, StringUtils = require('../utils/string.js') , StringUtils = require('../utils/string.js')
, _ = require('underscore') , _ = require('underscore')
, OUTPUT = require('./out') , OUTPUT = require('./out')
, SAFELOAD = require('../utils/safe-json-loader')
, PAD = require('string-padding')
, Command = require('commander').Command; , Command = require('commander').Command;
@ -33,20 +35,23 @@ Definition of the `main` function.
@license MIT. See LICENSE.md for details. @license MIT. See LICENSE.md for details.
@module main.js @module main.js
*/ */
var main = module.exports = function( args ) { var main = module.exports = function( rawArgs ) {
args = initialize( args ); var initInfo = initialize( rawArgs );
var args = initInfo.args;
// Create the top-level (application) command... // Create the top-level (application) command...
var program = new Command('hackmyresume') var program = new Command('hackmyresume')
.version(PKG.version) .version(PKG.version)
.description(chalk.yellow.bold('*** HackMyResume ***')) .description(chalk.yellow.bold('*** HackMyResume ***'))
.option('-o --opts <optionsFile>', 'Path to a .hackmyrc options file') //.option('-o --optionsSafe <optionsFile>', 'Path to a .hackmyrc options file', /^\"(.*)\"$/i )
.option('-s --silent', 'Run in silent mode') .option('-s --silent', 'Run in silent mode')
.option('--no-color', 'Disable colors') .option('--no-color', 'Disable colors')
.option('--color', 'Enable colors') .option('--color', 'Enable colors')
.option('-d --debug', 'Enable diagnostics', false) .option('-d --debug', 'Enable diagnostics', false)
.option('-v --version', 'Show the version'); .option('-v --version', 'Show the version')
.allowUnknownOption();
program.jsonArgs = initInfo.options;
//.usage('COMMAND <sources> [TO <targets>]'); //.usage('COMMAND <sources> [TO <targets>]');
// Create the NEW command // Create the NEW command
@ -121,20 +126,12 @@ Definition of the `main` function.
logMsg( title ); logMsg( title );
// Support case-insensitive sub-commands (build, generate, validate, etc.).. var o = initOptions( ar );
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)... // Handle invalid verbs here (a bit easier here than in commander.js)...
if( verb && !HMR.verbs[ verb ] && !HMR.alias[ verb ] ) { if( o.verb && !HMR.verbs[ o.verb ] && !HMR.alias[ o.verb ] ) {
throw { fluenterror: HACKMYSTATUS.invalidCommand, shouldExit: true, throw { fluenterror: HACKMYSTATUS.invalidCommand, shouldExit: true,
attempted: oVerb }; attempted: o.orgVerb };
} }
// Override the .missingArgument behavior // Override the .missingArgument behavior
@ -150,17 +147,56 @@ Definition of the `main` function.
return chalk.green.bold(manPage); return chalk.green.bold(manPage);
}; };
return args; return {
args: o.args,
options: o.json
};
} }
function initOptions( ar ) {
var oVerb, verb = '', args = ar.slice(), cleanArgs = args.slice(2), oJSON;
if( cleanArgs.length ) {
// Support case-insensitive sub-commands (build, generate, validate, etc.)..
var vidx = _.findIndex( cleanArgs, function(v){ return v[0] !== '-'; });
if( vidx !== -1 ) {
oVerb = cleanArgs[ vidx ];
verb = args[ vidx + 2 ] = oVerb.trim().toLowerCase();
}
// Remove --options --opts -o and process separately
var optsIdx = _.findIndex( cleanArgs, function(v){
return v === '-o' || v === '--options' || v === '--opts';
});
if(optsIdx !== -1) {
optStr = cleanArgs[ optsIdx + 1];
args.splice( optsIdx + 2, 2 );
if( optStr && (optStr = optStr.trim()) ) {
//var myJSON = JSON.parse(optStr);
if( optStr[0] === '{')
oJSON = eval('(' + optStr + ')'); // jshint ignore:line
else
oJSON = SAFELOAD.loadSafeJson( optStr );
}
}
}
return {
orgVerb: oVerb,
verb: verb,
json: oJSON,
args: args
};
}
/** /**
Invoke a HackMyResume verb. Invoke a HackMyResume verb.
*/ */
function execute( src, dst, opts, log ) { function execute( src, dst, opts, log ) {
loadOptions.call( this, opts ); loadOptions.call( this, opts, this.parent.jsonArgs );
require( '../core/error-handler' ).init( _opts.debug ); require( '../core/error-handler' ).init( _opts.debug );
var out = new OUTPUT( _opts ); var out = new OUTPUT( _opts );
var v = new HMR.verbs[ this.name() ](); var v = new HMR.verbs[ this.name() ]();
@ -173,21 +209,35 @@ Definition of the `main` function.
/** /**
Initialize HackMyResume options. Initialize HackMyResume options.
TODO: Options loading is a little hacky, for two reasons:
- Commander.js idiosyncracies
- Need to accept JSON inputs from the command line.
*/ */
function loadOptions( o ) { function loadOptions( o, cmdO ) {
o.opts = this.parent.opts;
// o and this.opts() seem to be the same (command-specific options)
// Load the specified options file (if any) and apply options // Load the specified options file (if any) and apply options
if( o.opts && String.is( o.opts )) { if( cmdO )
var json = safeLoadJSON( PATH.relative( process.cwd(), o.opts ) ); o = EXTEND(true, o, cmdO);
json && ( o = EXTEND( true, o, json ) );
if( !json ) {
throw safeLoadJSON.error;
}
}
// Merge in command-line options // Merge in command-line options
o = EXTEND( true, o, this.opts() ); o = EXTEND( true, o, this.opts() );
o.silent = this.parent.silent;
o.debug = this.parent.debug; // Kludge parent-level options until piping issue is resolved
if( this.parent.silent !== undefined && this.parent.silent !== null)
o.silent = this.parent.silent;
if( this.parent.debug !== undefined && this.parent.debug !== null)
o.debug = this.parent.debug;
if( o.debug ) {
logMsg(chalk.cyan('Merged options: '));
_.each(o, function(val, key) {
logMsg(chalk.cyan('%s: %s'), PAD(key,10), val);
});
}
// Cache
_opts = o; _opts = o;
} }
@ -228,9 +278,8 @@ Definition of the `main` function.
/** /**
Simple logging placeholder. Simple logging placeholder.
*/ */
function logMsg( msg ) { function logMsg() {
msg = msg || ''; _opts.silent || console.log.apply( console.log, arguments );
_opts.silent || console.log( msg );
} }

View File

@ -88,7 +88,8 @@ Implementation of the 'build' verb for HackMyResume.
rez = _.reduceRight( sheets, function( a, b, idx ) { rez = _.reduceRight( sheets, function( a, b, idx ) {
return extend( true, b, a ); return extend( true, b, a );
}); });
(sheets.length > 1) && this.stat( HME.afterMerge, { r: rez } ); // TODO: Fix this condition
(sheets.length) && this.stat( HME.afterMerge, { r: rez } );
// Expand output resumes... // Expand output resumes...
var targets = expand( dst, theme ); var targets = expand( dst, theme );
@ -155,11 +156,11 @@ Implementation of the 'build' verb for HackMyResume.
// If targInfo.fmt.files exists, this format is backed by a document. // If targInfo.fmt.files exists, this format is backed by a document.
// Fluent/FRESH themes are handled here. // Fluent/FRESH themes are handled here.
if( targInfo.fmt.files && targInfo.fmt.files.length ) { if( targInfo.fmt.files && targInfo.fmt.files.length ) {
theFormat = _fmts.filter( theFormat = _fmts.filter(
function(fmt) { return fmt.name === targInfo.fmt.outFormat; })[0]; function(fmt) { return fmt.name === targInfo.fmt.outFormat; })[0];
MKDIRP.sync( PATH.dirname( f ) ); // Ensure dest folder exists; MKDIRP.sync( PATH.dirname( f ) ); // Ensure dest folder exists;
_opts.targets = finished; _opts.targets = finished;
return theFormat.gen.generate( rez, f, _opts ); return theFormat.gen.generate( rez, f, _opts );
} }
//Otherwise this is an ad-hoc format (JSON, YML, or PNG) that every theme //Otherwise this is an ad-hoc format (JSON, YML, or PNG) that every theme