From 885f29b3a1b1aa6928b8c07124794a887b610d4f Mon Sep 17 00:00:00 2001 From: at15 Date: Mon, 12 Sep 2016 11:31:14 -0700 Subject: [PATCH] [template] Wrap cb using Promise #7 - use dustjs as template engine - wrap its callback based API using node native promise --- cmd/render.js | 16 ++++++++++++++++ cmd/root.js | 2 ++ example/tmpl/index.html | 8 ++++++++ lib/cli/command.js | 14 +++++++------- lib/template/render.js | 31 +++++++++++++++++++++++++++++++ package.json | 2 ++ 6 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 cmd/render.js create mode 100644 example/tmpl/index.html create mode 100644 lib/template/render.js diff --git a/cmd/render.js b/cmd/render.js new file mode 100644 index 0000000..ca927a6 --- /dev/null +++ b/cmd/render.js @@ -0,0 +1,16 @@ +/** + * Created by at15 on 2016/9/12. + */ +'use strict'; + +const Command = require('../lib/cli/command'); +const logger = require('../lib/logger'); + +let renderCmd = new Command(); +renderCmd.name = 'render'; +renderCmd.description = 'render template to html with data from config file and markdown'; +renderCmd.setFunc((app, args, flags) => { + logger.info('try to render file', {file: args[0]}); +}); + +module.exports = renderCmd; \ No newline at end of file diff --git a/cmd/root.js b/cmd/root.js index 2ee8273..8dcccfd 100644 --- a/cmd/root.js +++ b/cmd/root.js @@ -6,6 +6,7 @@ const Command = require('../lib/cli/command'); const Flag = require('../lib/cli/flag'); const versionCmd = require('./version'); +const renderCmd = require('./render'); let rootCmd = new Command(); rootCmd.name = 'about-cli'; @@ -25,5 +26,6 @@ rootCmd.addFlag(verboseFlag); // register commands rootCmd.registerCommand(versionCmd); +rootCmd.registerCommand(renderCmd); module.exports = rootCmd; \ No newline at end of file diff --git a/example/tmpl/index.html b/example/tmpl/index.html new file mode 100644 index 0000000..42eb832 --- /dev/null +++ b/example/tmpl/index.html @@ -0,0 +1,8 @@ + + + {title} + + + + + \ No newline at end of file diff --git a/lib/cli/command.js b/lib/cli/command.js index ac2206f..22030a4 100644 --- a/lib/cli/command.js +++ b/lib/cli/command.js @@ -161,23 +161,23 @@ class Command { `${this.description} Usage: - Not supported + Not supported Available Commands: - ${_.map(this.subCommands, (cmd)=> { +${_.map(this.subCommands, (cmd)=> { // pad the command name for better output - return `${_.padEnd(cmd.name, 10)} ${cmd.description}`; + return ` ${_.padEnd(cmd.name, 10)} ${cmd.description}`; }).join('\n')} Flags: - ${_.map(this.supportedFlags, (flag) => { +${_.map(this.supportedFlags, (flag) => { // FIXME: not all flags have alias, a strange - will appear in that case - return _.padEnd(`-${flag.alias}, --${flag.name}`, 20) + flag.description; + return ' ' + _.padEnd(`-${flag.alias}, --${flag.name}`, 20) + flag.description; })} Global Flags: - ${_.map(this.supportedGlobalFlags, (flag) => { - return _.padEnd(`-${flag.alias}, --${flag.name}`, 20) + flag.description; +${_.map(this.supportedGlobalFlags, (flag) => { + return ' ' + _.padEnd(`-${flag.alias}, --${flag.name}`, 20) + flag.description; })} `; diff --git a/lib/template/render.js b/lib/template/render.js new file mode 100644 index 0000000..f9c54a3 --- /dev/null +++ b/lib/template/render.js @@ -0,0 +1,31 @@ +/** + * Created by at15 on 2016/9/12. + */ +'use strict'; + +const dust = require('dustjs-linkedin'); +let compiled = dust.compile('this {title}', 'hello'); +dust.loadSource(compiled); + +// based on http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises +function renderAsync() { + // Promise is already in node + return new Promise((resolve, reject) => { + dust.render('hello', {title: 'foo'}, function (err, out) { + if (err != null) { + // TODO: is return necessary and which position should it be? + // return reject(err); + reject(err); + return; + } + resolve(out); + }); + }); +} + +let p = renderAsync(); +p.then((out) => { + console.log(out); +}).catch((e) => { + console.error(e); +}); diff --git a/package.json b/package.json index 2a33dca..2f36d4d 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,8 @@ "homepage": "https://github.com/tongquhq/about#readme", "dependencies": { "chalk": "^1.1.3", + "dustjs-helpers": "^1.7.3", + "dustjs-linkedin": "^2.7.3", "js-yaml": "^3.6.1", "lodash": "^4.15.0", "minimist": "^1.2.0",