From 4a98e0bb258f128d5cf5b8e2b0d7466cca382e8b Mon Sep 17 00:00:00 2001 From: devlinjd Date: Mon, 26 Oct 2015 08:01:01 -0400 Subject: [PATCH] Multiple things. 1. Load themes directly in FCMD instead of only through FluentLib. 2. Add support for silent mode (`-s` or `--silent`). 3. Silently create output folder if not present (mkdirp). --- package.json | 4 +++- src/fluentcmd.js | 41 ++++++++++++++++++++++++++++++---------- src/index.js | 16 ++++++++++++---- src/utils/file-exists.js | 18 ++++++++++++++++++ 4 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 src/utils/file-exists.js diff --git a/package.json b/package.json index 3f04a31..489f416 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,8 @@ "dependencies": { "fluentlib": "fluentdesk/fluentlib#v0.4.0", "minimist": "^1.2.0", - "underscore": "^1.8.3" + "mkdirp": "^0.5.1", + "underscore": "^1.8.3", + "watermark": "fluentdesk/watermark#v0.3.0-alpha" } } diff --git a/src/fluentcmd.js b/src/fluentcmd.js index 4ffb627..6410b47 100644 --- a/src/fluentcmd.js +++ b/src/fluentcmd.js @@ -12,6 +12,8 @@ module.exports = function () { , fs = require('fs') , _ = require('underscore') , FLUENT = require('fluentlib') + , PATH = require('path') + , MKDIRP = require('mkdirp') , rez, _log, _err; /** @@ -47,17 +49,32 @@ module.exports = function () { }); msg && _log(msg); + // Load the active theme + // Verify the specified theme name/path + var tFolder = PATH.resolve( __dirname, '../node_modules/watermark/themes', _opts.theme ); + var exists = require('./utils/file-exists'); + if (!exists( tFolder )) { + tFolder = PATH.resolve( _opts.theme ); + if (!exists( tFolder )) { + throw { fluenterror: 1, data: _opts.theme }; + } + } + var theTheme = new FLUENT.Theme().open( tFolder ); + _opts.themeObj = theTheme; + _log( 'Applying ' + theTheme.name.toUpperCase() + ' theme (' + Object.keys(theTheme.formats).length + ' formats)' ); + // Expand output resumes... (can't use map() here) var targets = []; + var that = this; ( (dst && dst.length && dst) || ['resume.all'] ).forEach( function(t) { var to = path.resolve(t), pa = path.parse(to), fmat = pa.ext || '.all'; targets.push.apply(targets, fmat === '.all' ? - _fmts.map(function(z){ return { file: to.replace(/all$/g,z.ext), fmt: z } }) - : [{ file: to, fmt: _.findWhere( _fmts, { ext: fmat.substring(1) }) }]); + Object.keys( theTheme.formats ).map(function(k){ var z = theTheme.formats[k]; return { file: to.replace(/all$/g,z.pre), fmt: z } }) + : [{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]); }); // Run the transformation! - var finished = targets.map( single ); + var finished = targets.map( function(t) { return single(t, theTheme); } ); // Don't send the client back empty-handed return { sheet: rez, targets: targets, processed: finished }; @@ -68,13 +85,17 @@ module.exports = function () { @param f Full path to the destination resume to generate, for example, "/foo/bar/resume.pdf" or "c:\foo\bar\resume.txt". */ - function single( fi ) { + function single( fi, theme ) { try { var f = fi.file, fType = fi.fmt.ext, fName = path.basename( f, '.' + fType ); - var fObj = _fmts.filter( function(_f) { return _f.ext === fType; } )[0]; - var fOut = path.join( f.substring( 0, f.lastIndexOf('.') + 1 ) + fObj.ext ); - _log( 'Generating ' + fi.fmt.name.toUpperCase() + ' resume: ' + path.relative(process.cwd(), f ) ); - return fObj.gen.generate( rez, fOut, _opts ); + var fObj = _.property( fi.fmt.pre )( theme.formats ); + var fOut = path.join( f.substring( 0, f.lastIndexOf('.') + 1 ) + fObj.pre ); + _log( 'Generating ' + fi.fmt.title.toUpperCase() + ' resume: ' + path.relative(process.cwd(), f ) ); + var theFormat = _fmts.filter( function( fmt ) { + return fmt.name === fi.fmt.pre; + })[0]; + MKDIRP( path.dirname(fOut) ); // Ensure dest folder exists; don't bug user + theFormat.gen.generate( rez, fOut, _opts ); } catch( ex ) { _err( ex ); @@ -96,9 +117,9 @@ module.exports = function () { { 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: 'markdown', ext: 'md', fmt: 'txt', gen: new FLUENT.MarkdownGenerator() }, + { name: 'md', ext: 'md', fmt: 'txt', gen: new FLUENT.MarkdownGenerator() }, { name: 'json', ext: 'json', gen: new FLUENT.JsonGenerator() }, - { name: 'yaml', ext: 'yml', fmt: 'yml', gen: new FLUENT.JsonYamlGenerator() } + { name: 'yml', ext: 'yml', fmt: 'yml', gen: new FLUENT.JsonYamlGenerator() } ]; /** diff --git a/src/index.js b/src/index.js index 5ef1637..7d94fb4 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,7 @@ var ARGS = require( 'minimist' ) , PKG = require('../package.json'); try { + var opts = { }; main(); } catch( ex ) { @@ -17,27 +18,34 @@ catch( ex ) { } function main() { + // Setup. - console.log( '*** FluentCMD v' + PKG.version + ' ***' ); if( process.argv.length <= 2 ) { throw { fluenterror: 3 }; } + var args = ARGS( process.argv.slice(2) ); + opts = getOpts( args ); + logMsg( '*** FluentCMD v' + PKG.version + ' ***' ); // Convert arguments to source files, target files, options - var args = ARGS( process.argv.slice(2) ); var src = args._ || []; var dst = (args.o && ((typeof args.o === 'string' && [ args.o ]) || args.o)) || []; dst = (dst === true) ? [] : dst; // Handle -o with missing output file // Generate! - FCMD.generate( src, dst, getOpts( args ) ); + FCMD.generate( src, dst, opts, logMsg ); process.platform !== 'win32' && console.log('\n'); } +function logMsg( msg ) { + opts.silent || console.log( msg ); +} + function getOpts( args ) { var noPretty = args['nopretty'] || args.n; noPretty = noPretty && (noPretty === true || noPretty === 'true'); return { theme: args.t || 'modern', - prettify: !noPretty + prettify: !noPretty, + silent: args.s || args.silent }; } diff --git a/src/utils/file-exists.js b/src/utils/file-exists.js new file mode 100644 index 0000000..2b61535 --- /dev/null +++ b/src/utils/file-exists.js @@ -0,0 +1,18 @@ +/** +File-exists checker for Node.js. +@license Copyright (c) 2015 | James M. Devlin +*/ + +var FS = require('fs'); + +// Yup, this is now the recommended way to check for file existence on Node. +// fs.exists is deprecated and the recommended fs.statSync/lstatSync throws +// exceptions on non-existent paths :) +module.exports = function (path) { + try { + FS.statSync( path ); + return true; + } catch( err ) { + return !(err && err.code === 'ENOENT'); + } +};