render.js 1.1 KB

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