index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. callback(null, bundledJS);
  35. // @TODO REMOVE THIS MASSIVE HACK!
  36. // Once this is resolved: https://github.com/hexojs/hexo/issues/1663
  37. var finalDest = data.path.replace('/src/', '/public/')
  38. finalDest = finalDest.replace('.es6', '.js');
  39. setTimeout(function(){
  40. hexo.log.i('hexo-renderer-uppyexamples: applying hack for: ' + finalDest);
  41. fs.writeFileSync(finalDest, bundledJS);
  42. }, 1000)
  43. });
  44. });
  45. });