index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // We listen for hexo changes on *.es6 extensions.
  2. // We fire our own build-examples.js and tell it which example to build -
  3. // that script then writes temporary js files
  4. // which we return via the callback.
  5. var exec = require('child_process').exec;
  6. var path = require('path');
  7. var fs = require('fs');
  8. var uuid = require('uuid');
  9. var webRoot = path.dirname(path.dirname(__dirname));
  10. var uppyRoot = path.dirname(webRoot);
  11. var browserifyScript = webRoot + '/build-examples.js'
  12. hexo.extend.renderer.register('es6', 'js', function(data, options, callback) {
  13. if (!data || !data.path) {
  14. return callback(null);
  15. }
  16. if (!data.path.match(/\/examples\//)) {
  17. callback(null, data.text);
  18. }
  19. var slug = data.path.replace(/[^a-zA-Z0-9\_\.]/g, '-');
  20. var slug = uuid.v4();
  21. var tmpFile = '/tmp/' + slug + '.js';
  22. var cmd = 'node ' + browserifyScript + ' ' + data.path + ' ' + tmpFile + ' --colors';
  23. // hexo.log.i('hexo-renderer-uppyexamples: change detected in examples. running: ' + cmd);
  24. exec(cmd, function(err, stdout, stderr) {
  25. if (err) {
  26. return callback(err);
  27. }
  28. hexo.log.i('hexo-renderer-uppyexamples: ' + stdout.trim());
  29. fs.readFile(tmpFile, 'utf-8', function(err, bundledJS) {
  30. if (err) {
  31. return callback(err);
  32. }
  33. // hexo.log.i('hexo-renderer-uppyexamples: read: ' + tmpFile);
  34. // @TODO remove this hack
  35. // once this is resolved: https://github.com/hexojs/hexo/issues/1663
  36. bundledJS = bundledJS.replace(/</g, ' < ');
  37. callback(null, bundledJS);
  38. });
  39. });
  40. });