From ce75f09210bdbce928d208f890bc79b3a4209384 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Mon, 4 Jan 2016 07:23:20 -0500 Subject: [PATCH] Refactor API interface. --- package.json | 2 +- src/core/default-formats.js | 22 +++++++-------- src/core/error-handler.js | 2 +- src/eng/generic-helpers.js | 3 +- src/hackmyapi.js | 55 +++++++++++++++++++++++++------------ src/hackmycmd.js | 52 ----------------------------------- src/index.js | 6 ++-- src/verbs/analyze.js | 3 +- src/verbs/create.js | 7 +++-- src/verbs/generate.js | 40 +++++++++++++++------------ test/test-cli.js | 2 +- test/test-themes.js | 17 ++++++++++-- 12 files changed, 98 insertions(+), 113 deletions(-) delete mode 100644 src/hackmycmd.js diff --git a/package.json b/package.json index ec33386..5114290 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "bugs": { "url": "https://github.com/hacksalot/HackMyResume/issues" }, - "main": "src/hackmycmd.js", + "main": "src/hackmyapi.js", "bin": { "hackmyresume": "src/index.js" }, diff --git a/src/core/default-formats.js b/src/core/default-formats.js index ce344c3..18eb44a 100644 --- a/src/core/default-formats.js +++ b/src/core/default-formats.js @@ -1,20 +1,20 @@ (function(){ - var FLUENT = require('../hackmyapi'); - /** Supported resume formats. */ + module.exports = [ - { name: 'html', ext: 'html', gen: new FLUENT.HtmlGenerator() }, - { name: 'txt', ext: 'txt', gen: new FLUENT.TextGenerator() }, - { name: 'doc', ext: 'doc', fmt: 'xml', gen: new FLUENT.WordGenerator() }, - { name: 'pdf', ext: 'pdf', fmt: 'html', is: false, gen: new FLUENT.HtmlPdfGenerator() }, - { name: 'png', ext: 'png', fmt: 'html', is: false, gen: new FLUENT.HtmlPngGenerator() }, - { name: 'md', ext: 'md', fmt: 'txt', gen: new FLUENT.MarkdownGenerator() }, - { name: 'json', ext: 'json', gen: new FLUENT.JsonGenerator() }, - { name: 'yml', ext: 'yml', fmt: 'yml', gen: new FLUENT.JsonYamlGenerator() }, - { name: 'latex', ext: 'tex', fmt: 'latex', gen: new FLUENT.LaTeXGenerator() } + + { name: 'html', ext: 'html', gen: new (require('../gen/html-generator'))() }, + { name: 'txt', ext: 'txt', gen: new (require('../gen/text-generator'))() }, + { name: 'doc', ext: 'doc', fmt: 'xml', gen: new (require('../gen/word-generator'))() }, + { name: 'pdf', ext: 'pdf', fmt: 'html', is: false, gen: new (require('../gen/html-pdf-generator'))() }, + { name: 'png', ext: 'png', fmt: 'html', is: false, gen: new (require('../gen/html-png-generator'))() }, + { name: 'md', ext: 'md', fmt: 'txt', gen: new (require('../gen/markdown-generator'))() }, + { name: 'json', ext: 'json', gen: new (require('../gen/json-generator'))() }, + { name: 'yml', ext: 'yml', fmt: 'yml', gen: new (require('../gen/json-yaml-generator'))() }, + { name: 'latex', ext: 'tex', fmt: 'latex', gen: new (require('../gen/latex-generator'))() } ]; diff --git a/src/core/error-handler.js b/src/core/error-handler.js index 839a4af..99bd3b4 100644 --- a/src/core/error-handler.js +++ b/src/core/error-handler.js @@ -13,7 +13,7 @@ Error-handling routines for HackMyResume. var HACKMYSTATUS = require('./status-codes') , PKG = require('../../package.json') , FS = require('fs') - , FCMD = require('../hackmycmd') + , FCMD = require('../hackmyapi') , PATH = require('path') , chalk = require('chalk') , title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***'); diff --git a/src/eng/generic-helpers.js b/src/eng/generic-helpers.js index 3f6d92a..3ed9c10 100644 --- a/src/eng/generic-helpers.js +++ b/src/eng/generic-helpers.js @@ -11,7 +11,8 @@ Generic template helper definitions for HackMyResume / FluentCV. , H2W = require('../utils/html-to-wpml') , XML = require('xml-escape') , moment = require('moment') - , _ = require('underscore'); + , _ = require('underscore') + , unused = require('../utils/string'); /** Generic template helper function definitions. diff --git a/src/hackmyapi.js b/src/hackmyapi.js index a118028..7351b59 100644 --- a/src/hackmyapi.js +++ b/src/hackmyapi.js @@ -4,21 +4,40 @@ External API surface for HackMyResume. @module hackmyapi.js */ -module.exports = { - Sheet: require('./core/fresh-resume'), - FRESHResume: require('./core/fresh-resume'), - JRSResume: require('./core/jrs-resume'), - FRESHTheme: require('./core/fresh-theme'), - JRSTheme: require('./core/jrs-theme'), - FluentDate: require('./core/fluent-date'), - HtmlGenerator: require('./gen/html-generator'), - TextGenerator: require('./gen/text-generator'), - HtmlPdfGenerator: require('./gen/html-pdf-generator'), - WordGenerator: require('./gen/word-generator'), - MarkdownGenerator: require('./gen/markdown-generator'), - JsonGenerator: require('./gen/json-generator'), - YamlGenerator: require('./gen/yaml-generator'), - JsonYamlGenerator: require('./gen/json-yaml-generator'), - LaTeXGenerator: require('./gen/latex-generator'), - HtmlPngGenerator: require('./gen/html-png-generator') -}; +(function() { + + var v = { + build: require('./verbs/generate'), + analyze: require('./verbs/analyze'), + validate: require('./verbs/validate'), + convert: require('./verbs/convert'), + new: require('./verbs/create') + }; + + var HackMyAPI = module.exports = { + verbs: v, + alias: { + generate: v.build, + create: v.build + }, + options: require('./core/default-options'), + formats: require('./core/default-formats'), + Sheet: require('./core/fresh-resume'), + FRESHResume: require('./core/fresh-resume'), + JRSResume: require('./core/jrs-resume'), + FRESHTheme: require('./core/fresh-theme'), + JRSTheme: require('./core/jrs-theme'), + FluentDate: require('./core/fluent-date'), + HtmlGenerator: require('./gen/html-generator'), + TextGenerator: require('./gen/text-generator'), + HtmlPdfGenerator: require('./gen/html-pdf-generator'), + WordGenerator: require('./gen/word-generator'), + MarkdownGenerator: require('./gen/markdown-generator'), + JsonGenerator: require('./gen/json-generator'), + YamlGenerator: require('./gen/yaml-generator'), + JsonYamlGenerator: require('./gen/json-yaml-generator'), + LaTeXGenerator: require('./gen/latex-generator'), + HtmlPngGenerator: require('./gen/html-png-generator') + }; + +}()); diff --git a/src/hackmycmd.js b/src/hackmycmd.js deleted file mode 100644 index 75f69f3..0000000 --- a/src/hackmycmd.js +++ /dev/null @@ -1,52 +0,0 @@ -/** -Internal resume generation logic for HackMyResume. -@license MIT. Copyright (c) 2015 James M. Devlin / FluentDesk. -@module hackmycmd.js -*/ - -(function() { - module.exports = function () { - - var unused = require('./utils/string') - , PATH = require('path') - , FS = require('fs') - , chalk = require('chalk'); - - - /** - Display help documentation. - */ - function help() { - var manPage = FS.readFileSync( PATH.join(__dirname, 'use.txt'), 'utf8' ); - console.log( chalk.green.bold(manPage) ); - } - - /** - Internal module interface. Used by FCV Desktop and HMR. - */ - var v = { - build: require('./verbs/generate'), - analyze: require('./verbs/analyze'), - validate: require('./verbs/validate'), - convert: require('./verbs/convert'), - new: require('./verbs/create'), - help: help - }; - - return { - verbs: v, - alias: { - generate: v.build, - create: v.build - }, - lib: require('./hackmyapi'), - options: require('./core/default-options'), - formats: require('./core/default-formats') - }; - - }(); - -}()); - -// [1]: JSON.parse throws SyntaxError on invalid JSON. See: -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse diff --git a/src/index.js b/src/index.js index 700cfa2..766f7e5 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ Command-line interface (CLI) for HackMyResume. var SPAWNW = require('./core/spawn-watch') - , FCMD = require( './hackmycmd') + , HMR = require( './hackmyapi') , PKG = require('../package.json') , FS = require('fs') , EXTEND = require('./utils/extend') @@ -139,7 +139,7 @@ function initialize() { } // Handle invalid verbs here (a bit easier here than in commander.js)... - if( verb && !FCMD.verbs[ verb ] && !FCMD.alias[ verb ] ) { + if( verb && !HMR.verbs[ verb ] && !HMR.alias[ verb ] ) { throw { fluenterror: HACKMYSTATUS.invalidCommand, shouldExit: true, attempted: oVerb }; } @@ -165,7 +165,7 @@ Invoke a HackMyResume verb. */ function execVerb( src, dst, opts, log ) { loadOptions.call( this, opts ); - FCMD.verbs[ this.name() ].call( null, src, dst, _opts, log ); + HMR.verbs[ this.name() ].call( null, src, dst, _opts, log ); } diff --git a/src/verbs/analyze.js b/src/verbs/analyze.js index 5912d36..110b868 100644 --- a/src/verbs/analyze.js +++ b/src/verbs/analyze.js @@ -10,8 +10,7 @@ Implementation of the 'analyze' verb for HackMyResume. - var FLUENT = require('../hackmyapi') - , MKDIRP = require('mkdirp') + var MKDIRP = require('mkdirp') , PATH = require('path') , _ = require('underscore') , ResumeFactory = require('../core/resume-factory') diff --git a/src/verbs/create.js b/src/verbs/create.js index c2d1c8f..a405949 100644 --- a/src/verbs/create.js +++ b/src/verbs/create.js @@ -6,8 +6,7 @@ Implementation of the 'create' verb for HackMyResume. (function(){ - var FLUENT = require('../hackmyapi') - , MKDIRP = require('mkdirp') + var MKDIRP = require('mkdirp') , PATH = require('path') , chalk = require('chalk'); @@ -22,7 +21,9 @@ Implementation of the 'create' verb for HackMyResume. _log(chalk.green('Creating new ') + chalk.green.bold(safeFormat) + chalk.green(' resume: ') + chalk.green.bold(t)); MKDIRP.sync( PATH.dirname( t ) ); // Ensure dest folder exists; - FLUENT[ safeFormat + 'Resume' ].default().save( t ); + var RezClass = require('../core/' + safeFormat.toLowerCase() + '-resume' ); + RezClass.default().save(t); + //FLUENT[ safeFormat + 'Resume' ].default().save( t ); }); }; diff --git a/src/verbs/generate.js b/src/verbs/generate.js index f55f1e9..6e4f10f 100644 --- a/src/verbs/generate.js +++ b/src/verbs/generate.js @@ -52,7 +52,7 @@ Implementation of the 'generate' verb for HackMyResume. _err = errHandler || error; _opts.theme = (opts.theme && opts.theme.toLowerCase().trim())|| 'modern'; _opts.prettify = opts.prettify === true ? _opts.prettify : false; - _opts.css = opts.css; + _opts.css = opts.css || 'embed'; _opts.pdf = opts.pdf; _opts.wrap = opts.wrap || 60; @@ -131,8 +131,20 @@ Implementation of the 'generate' verb for HackMyResume. , theFormat; var suffix = ''; - if( targInfo.fmt.outFormat === 'pdf' && _opts.pdf ) { - suffix = chalk.green(' (with ' + _opts.pdf + ')'); + if( targInfo.fmt.outFormat === 'pdf' ) { + if( _opts.pdf ) { + if( _opts.pdf !== 'none' ) { + suffix = chalk.green(' (with ' + _opts.pdf + ')'); + } + else { + _log( chalk.gray('Skipping ') + + chalk.white.bold( + pad(targInfo.fmt.outFormat.toUpperCase(),4,null,pad.RIGHT)) + + chalk.gray(' resume') + suffix + chalk.green(': ') + + chalk.white( PATH.relative(process.cwd(), f )) ); + return; + } + } } _log( chalk.green('Generating ') + @@ -244,28 +256,22 @@ Implementation of the 'generate' verb for HackMyResume. var to = PATH.resolve(t), pa = parsePath(to),fmat = pa.extname || '.all'; - var explicitFormats = _.omit( theTheme.formats, function(val, key) { - return !val.freebie; - }); - var implicitFormats = _.omit( theTheme.formats, function(val) { - return val.freebie; - }); targets.push.apply( targets, fmat === '.all' ? - Object.keys( implicitFormats ).map( function( k ) { + Object.keys( theTheme.formats ).map( function( k ) { var z = theTheme.formats[k]; return { file: to.replace( /all$/g, z.outFormat ), fmt: z }; }) : [{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]); - targets.push.apply( - targets, fmat === '.all' ? - Object.keys( explicitFormats ).map( function( k ) { - var z = theTheme.formats[k]; - return { file: to.replace( /all$/g, z.outFormat ), fmt: z }; - }) : - [{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]); + // targets.push.apply( + // targets, fmat === '.all' ? + // Object.keys( explicitFormats ).map( function( k ) { + // var z = theTheme.formats[k]; + // return { file: to.replace( /all$/g, z.outFormat ), fmt: z }; + // }) : + // [{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]); }); diff --git a/test/test-cli.js b/test/test-cli.js index 6e3c518..9dda221 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -5,7 +5,7 @@ var chai = require('chai') , path = require('path') , _ = require('underscore') , FRESHResume = require('../src/core/fresh-resume') - , FCMD = require( '../src/hackmycmd') + , FCMD = require( '../src/hackmyapi') , validator = require('is-my-json-valid'); chai.config.includeStack = false; diff --git a/test/test-themes.js b/test/test-themes.js index b7ff672..94bd161 100644 --- a/test/test-themes.js +++ b/test/test-themes.js @@ -6,7 +6,7 @@ var SPAWNWATCHER = require('../src/core/spawn-watch') , path = require('path') , _ = require('underscore') , FRESHResume = require('../src/core/fresh-resume') - , FCMD = require( '../src/hackmycmd') + , HMR = require( '../src/hackmyapi') , validator = require('is-my-json-valid') , READFILES = require('recursive-readdir-sync') , fileContains = require('../src/utils/file-contains') @@ -31,9 +31,20 @@ function genThemes( title, src, fmt ) { theme: themeLoc, format: fmt, prettify: true, - silent: false + silent: false, + css: 'embed' }; - FCMD.verbs.build( src, dst, opts, function(msg) { console.log(msg); } ); + try { + HMR.verbs.build( src, dst, opts, function(msg) { + msg = msg || ''; + console.log(msg); + }); + } + catch(ex) { + console.log(ex); + console.log(ex.stack); + throw ex; + } } tryOpen.should.not.Throw(); });