Skip to content

Commit

Permalink
[template] Wrap cb using Promise #7
Browse files Browse the repository at this point in the history
- use dustjs as template engine
- wrap its callback based API using node native promise
  • Loading branch information
at15 committed Sep 12, 2016
1 parent c96cc8f commit 885f29b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
16 changes: 16 additions & 0 deletions cmd/render.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions cmd/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -25,5 +26,6 @@ rootCmd.addFlag(verboseFlag);

// register commands
rootCmd.registerCommand(versionCmd);
rootCmd.registerCommand(renderCmd);

module.exports = rootCmd;
8 changes: 8 additions & 0 deletions example/tmpl/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>{title}</title>
</head>
<body>

</body>
</html>
14 changes: 7 additions & 7 deletions lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})}
`;
Expand Down
31 changes: 31 additions & 0 deletions lib/template/render.js
Original file line number Diff line number Diff line change
@@ -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);
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 885f29b

Please sign in to comment.