index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 webRoot = path.dirname(path.dirname(__dirname));
  9. var uppyRoot = path.dirname(webRoot);
  10. var browserifyScript = webRoot + '/build-examples.js'
  11. hexo.extend.renderer.register('es6', 'js', function(data, options, callback) {
  12. if (!data || !data.path) {
  13. return callback(null);
  14. }
  15. if (!data.path.match(/\/examples\//)) {
  16. callback(null, data.text);
  17. }
  18. var slug = data.path.replace(/[^a-zA-Z0-9\_\.]/g, '-');
  19. var dstPath = '/tmp/' + slug + '.js';
  20. var cmd = 'node ' + browserifyScript + ' ' + data.path + ' ' + dstPath + ' --colors';
  21. // hexo.log.i('hexo-renderer-uppyexamples: change detected in examples. running: ' + cmd);
  22. exec(cmd, function(err, stdout, stderr) {
  23. if (err) {
  24. return callback(err);
  25. }
  26. hexo.log.i('hexo-renderer-uppyexamples: ' + stdout.trim());
  27. fs.readFile(dstPath, 'utf-8', function(err, tmpJs) {
  28. if (err) {
  29. return callback(err);
  30. }
  31. hexo.log.i('hexo-renderer-uppyexamples: read: ' + dstPath);
  32. callback(null, tmpJs);
  33. });
  34. });
  35. });