Guest User

Untitled

a guest
Jan 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. var sys = require('sys')
  2. var fs = require('fs')
  3. var http = require('http')
  4. var haml = require('hamljs');
  5. var exec = require('child_process').exec
  6.  
  7. var argv = process.argv
  8. var target = argv[2]
  9. var parser = argv[3] || 'ruby'
  10.  
  11. if(!target){
  12. console.error('usage: ' + argv.join(' ') + ' <filename>')
  13. process.exit(1)
  14. }
  15.  
  16. fs.stat(target, function(e, stat){
  17. if(e){ throw e }
  18. if(!stat.isFile()) {
  19. console.error(target + ' is not file.')
  20. process.exit(1)
  21. }
  22.  
  23. fs.watchFile(target, { interval: 500 }, function(curr, prev){
  24. Parser[parser](target)
  25. })
  26. })
  27.  
  28. var Parser = {
  29. ruby: function(target){
  30. var output = target.replace('.haml', '.html'),
  31. cmd = 'haml -f html5 --trace ' + target + ' ' + output
  32.  
  33. exec(cmd, function (e, stdout, stderr) {
  34. if(e){ console.error(e) }
  35. console.log('updated ', output)
  36. })
  37. },
  38.  
  39. hamljs: function(target){
  40. fs.readFile(target, 'utf8', function (e, text) {
  41. if(e){ throw e }
  42.  
  43. try{
  44. var html = haml.render(text)
  45.  
  46. var output = target.replace('.haml', '.html')
  47. fs.writeFile(output, html, function(e){
  48. if(e){ throw e }
  49. console.log('updated ', output)
  50. })
  51. }catch(e){
  52. console.error(e)
  53. }
  54. })
  55. }
  56. }
Add Comment
Please, Sign In to add comment