1 | 'use strict';
|
2 |
|
3 | const pathFn = require('path');
|
4 | const tildify = require('tildify');
|
5 | const prettyHrtime = require('pretty-hrtime');
|
6 | const fs = require('hexo-fs');
|
7 | const chalk = require('chalk');
|
8 |
|
9 | function renderConsole(args) {
|
10 |
|
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 |
|
43 | module.exports = renderConsole;
|