var http = require('http');
var fs = require('fs');
var util = require('util');
var connect = require('connect');
var gulp = require('gulp');
var browserify = require('gulp-browserify');
var less = require('gulp-less');
var concat = require('gulp-concat');
var template = require('gulp-template');
var send = require('./lib/gulp-send');
var jsonToObject = require('./lib/gulp-json2obj');

var pkg = require('./package.json');
var files = require('./files');
process.env.MOCK_DATA_DIR = process.env.MOCK_DATA_DIR || 'data/sample';
process.env.IMAGES_ENDPOINT = 'images';

var app = connect();

app.use('/app.js', function(req, res, next) {
  res.setHeader('Content-Type', 'text/javascript');

  gulp.src('app/app.js')
    .pipe(browserify({
      transform: ['reactify'],
      debug: true
    }))
    // Error handling: can't just pass `next`,
    // need to explicitly give it a function
    .on('error', function(err) {
      next(err);
    })
    .pipe(concat('app.js'))
    .pipe(send(res));
});

app.use('/start.js', function(req, res) {
  res.setHeader('Content-Type', 'text/javascript');
  
  var stream = fs.createReadStream(__dirname + '/app/start.js');
  stream.pipe(res);
});

app.use('/tidepoolplatform.js', function(req, res) {
  res.setHeader('Content-Type', 'text/javascript');

  var stream = fs.createReadStream(__dirname + '/app/tidepoolplatform.js');
  stream.pipe(res);
});

app.use('/config.js', function(req, res) {
  res.setHeader('Content-Type', 'text/javascript');

  gulp.src('app/config.js')
    .pipe(template({
      process: {env: process.env},
      pkg: pkg
    }))
    .on('error', function(err) {
      next(err);
    })
    .pipe(send(res));
});

app.use('/mock.js', function(req, res, next) {
  res.setHeader('Content-Type', 'text/javascript');

  gulp.src('mock/index.js')
    .pipe(browserify({
      debug: true
    }))
    .on('error', function(err) {
      next(err);
    })
    .pipe(concat('mock.js'))
    .pipe(send(res));
});

app.use('/data.js', function(req, res, next) {
  res.setHeader('Content-Type', 'text/javascript');

  var data = {};

  gulp.src(process.env.MOCK_DATA_DIR + '/**/*.json')
    .pipe(jsonToObject(data))
    .on('end', function() {
      var contents = 'window.data = ';
      contents = contents + util.inspect(data, {depth: null});
      contents = contents + ';';
      res.end(contents);
    });
});

app.use('/bower_components', connect.static(__dirname + '/bower_components'));

app.use('/style.css', function(req, res, next) {
  res.setHeader('Content-Type', 'text/css');

  gulp.src('app/style.less')
    .pipe(less())
    .on('error', function(err) {
      next(err);
    })
    .pipe(concat('style.css'))
    .pipe(send(res));
});

app.use('/fonts', connect.static(__dirname + '/app/core/fonts'));

files.images.forEach(function(image) {
  var endpoint = '/images/' + image.endpoint;
  var dir = __dirname + '/' + image.dir;
  app.use(endpoint, connect.static(dir));
});

app.use('/', function(req, res, next) {
  if (req.url !== '/') {
    return next();
  }

  res.setHeader('Content-Type', 'text/html');

  gulp.src('app/index.html')
    .pipe(template({
      production: false,
      pkg: pkg,
      files: files,
      mock: process.env.MOCK
    }))
    .on('error', function(err) {
      next(err);
    })
    .pipe(send(res));
});

app.use(function(req, res, next) {
  res.writeHead(404, {'Content-Type': 'text/plain'});
  res.end('Not found');
});

app.use(connect.errorHandler());

var server = http.createServer(app).listen(process.env.PORT || 3000, function listening ( ) {
  console.log('Connect server started on port', server.address( ).port);
});

@@ -69,24 +69,52 @@
 re('util');%0A
+var path = require('path');%0A
 var connect 
@@ -131,24 +131,24 @@
 'connect');%0A
-
 var gulp = r
@@ -590,16 +590,38 @@
 images';
+%0Avar ROOT = __dirname;
 %0A%0Avar ap
@@ -742,24 +742,40 @@
 %0A  gulp.src(
+path.join(ROOT, 
 'app/app.js'
@@ -771,24 +771,25 @@
 app/app.js')
+)
 %0A    .pipe(b
@@ -1558,24 +1558,40 @@
 %0A  gulp.src(
+path.join(ROOT, 
 'app/config.
@@ -1590,24 +1590,25 @@
 /config.js')
+)
 %0A    .pipe(t
@@ -1870,16 +1870,32 @@
 ulp.src(
+path.join(ROOT, 
 'mock/in
@@ -1902,16 +1902,17 @@
 dex.js')
+)
 %0A    .pi
@@ -2650,24 +2650,40 @@
 %0A  gulp.src(
+path.join(ROOT, 
 'app/style.l
@@ -2687,16 +2687,17 @@
 e.less')
+)
 %0A    .pi
@@ -3209,16 +3209,32 @@
 ulp.src(
+path.join(ROOT, 
 'app/ind
@@ -3242,16 +3242,17 @@
 x.html')
+)
 %0A    .pi

Make asset lookups relative to project directory