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

Add support for external options file.

This commit is contained in:
hacksalot 2016-01-04 02:50:00 -05:00
parent c8d4a3deb3
commit f65cf8880e
3 changed files with 63 additions and 9 deletions

View File

@ -14,9 +14,11 @@ var SPAWNW = require('./core/spawn-watch')
, FCMD = require( './hackmycmd')
, PKG = require('../package.json')
, FS = require('fs')
, EXTEND = require('./utils/extend')
, chalk = require('chalk')
, PATH = require('path')
, HACKMYSTATUS = require('./core/status-codes')
, safeLoadJSON = require('./utils/safe-json-loader')
, _opts = { }
, title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***')
, StringUtils = require('./utils/string.js')
@ -40,13 +42,6 @@ function main() {
var args = initialize();
function execVerb( src, dst, opts, log ) {
_opts = opts;
_opts.silent = this.parent.silent;
_opts.opts = this.parent.opts;
FCMD.verbs[ this.name() ].call( null, src, dst, opts, log );
}
// Create the top-level (application) command...
var program = new Command('hackmyresume')
.version(PKG.version)
@ -100,7 +95,7 @@ function main() {
.alias('generate')
//.arguments('<sources> TO [targets]')
//.usage('...')
.option('-t --theme <theme>', 'Theme name or path', 'modern')
.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')
@ -165,6 +160,40 @@ function initialize() {
/**
Invoke a HackMyResume verb.
*/
function execVerb( src, dst, opts, log ) {
loadOptions.call( this, opts );
FCMD.verbs[ this.name() ].call( null, src, dst, _opts, log );
}
/**
Initialize HackMyResume options.
*/
function loadOptions( opts ) {
opts.opts = this.parent.opts;
// Load the specified options file (if any) and apply options
if( opts.opts && String.is( opts.opts )) {
var json = safeLoadJSON( PATH.relative( process.cwd(), opts.opts ) );
json && ( opts = EXTEND( true, opts, json ) );
if( !json ) {
throw safeLoadJSON.error;
}
}
// Merge in command-line options
opts = EXTEND( true, opts, this.opts() )
opts.silent = this.parent.silent;
_opts = opts;
}
/**
Split multiple command-line filenames by the 'TO' keyword
*/

View File

@ -0,0 +1,24 @@
/**
Definition of the SafeJsonLoader class.
@module syntax-error-ex.js
@license MIT. See LICENSE.md for details.
*/
(function() {
var FS = require('fs');
module.exports = function loadSafeJson( file ) {
try {
return JSON.parse( FS.readFileSync( file ) );
}
catch(ex) {
loadSafeJson.error = ex;
}
return null;
};
}());

View File

@ -1,6 +1,7 @@
/**
Definition of the SyntaxErrorEx class.
@module syntax-error-ex.js
@license MIT. See LICENSE.md for details.
*/
(function() {
@ -26,7 +27,7 @@ Definition of the SyntaxErrorEx class.
colNum = ex.columnNumber;
}
if( lineNum === null || colNum === null ) {
var JSONLint = require('json-lint');
var JSONLint = require('json-lint'); // TODO: json-lint or is-my-json-valid?
var lint = JSONLint( rawData, { comments: false } );
if( lineNum === null ) lineNum = (lint.error ? lint.line : '???');
if( colNum === null ) colNum = (lint.error ? lint.character : '???');