UNPKG

1.18 kBJavaScriptView Raw
1'use strict';
2
3const pathFn = require('path');
4const tildify = require('tildify');
5const prettyHrtime = require('pretty-hrtime');
6const fs = require('hexo-fs');
7const chalk = require('chalk');
8
9function renderConsole(args) {
10 // Display help message if user didn't input any arguments
11 if (!args._.length) {
12 return this.call('help', {_: 'render'});
13 }
14
15 const baseDir = this.base_dir;
16 const src = pathFn.resolve(baseDir, args._[0]);
17 const output = args.o || args.output;
18 const start = process.hrtime();
19 const log = this.log;
20
21 return this.render.render({
22 path: src,
23 engine: args.engine
24 }).then(result => {
25 if (typeof result === 'object') {
26 if (args.pretty) {
27 result = JSON.stringify(result, null, ' ');
28 } else {
29 result = JSON.stringify(result);
30 }
31 }
32
33 if (!output) return console.log(result);
34
35 const dest = pathFn.resolve(baseDir, output);
36 const interval = prettyHrtime(process.hrtime(start));
37
38 log.info('Rendered in %s: %s -> %s', chalk.cyan(interval), chalk.magenta(tildify(src)), chalk.magenta(tildify(dest)));
39 return fs.writeFile(dest, result);
40 });
41}
42
43module.exports = renderConsole;