Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 99 lines (86 sloc) 2.552 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#!/bin/sh
':' //; exec "`command -v nodejs || command -v node`" "$0"

var server = require('http').createServer(httpHandler),
    spawn = require('child_process').spawn,
    exec = require('child_process').exec,
    io = require('socket.io').listen(server),
    send = require('send'),
    server,
    socket;

server.listen(8090);

function httpHandler(req, res) {
  switch(req.method)
  {
    case 'GET':
      // Example: /my-repo/raw/master/sub-dir/some.png
      var githubUrl = req.url.match(/\/[^\/]+\/raw\/[^\/]+\/(.+)/);
      if (githubUrl) {
         // Serve the file out of the current working directory
        send(req, githubUrl[1])
         .root(process.cwd())
         .pipe(res);
        return;
      }

      // Otherwise serve the file from the directory this module is in
      send(req, req.url)
        .root(__dirname)
        .pipe(res);
      break;

    // case 'HEAD':
      // res.writeHead(200);
      // res.end();
      // exec('open -g http://localhost:8090', function(error, stdout, stderr){
        // http.request({port: 8090})
      // });
      // break;

    case 'DELETE':
      socket.emit('die');
      process.exit();
      break;

    case 'PUT':
      var newHTML = '';
      var gfm = spawn(__dirname + '/node_modules/docter/bin/github-flavored-markdown.rb', ['--unstyled']);
      req.on('data', function(chunk){
        gfm.stdin.write(chunk);
      });
      req.on('end', function(){
        gfm.stdin.end();
        res.writeHead(200);
        res.end();
      });
      gfm.stdout.on('data', function(data) {
        newHTML += data;
      });
      gfm.on('exit',function(ecode){
        socket.emit('newContent', newHTML);
      });
      break;

    default:
  }
}

io.set('log level', 1);
io.sockets.on('connection', function(sock){
  socket = sock;
  process.stdout.write('connection established!');

  var newHTML = '';
  var gfm = spawn(__dirname + '/node_modules/docter/bin/github-flavored-markdown.rb', ['--unstyled']);
  process.stdin.on('data', function(chunk){
    gfm.stdin.write(chunk);
  });
  process.stdin.on('end', function(){
    gfm.stdin.end();
  });
  gfm.stdout.on('data', function(data) {
    newHTML += data;
  });
  gfm.on('exit',function(ecode){
    socket.emit('newContent', newHTML);
  });
  process.stdin.resume();
});


if (process.platform.toLowerCase().indexOf('darwin') >= 0){
  exec('open -g http://localhost:8090', function(error, stdout, stderr){});
}
else { // assume unix/linux
  exec('xdg-open http://localhost:8090', function(error, stdout, stderr){});
}
Something went wrong with that request. Please try again.