Code coverage report for lib/bundlers/watchify.js

Statements: 100% (47 / 47)      Branches: 100% (10 / 10)      Functions: 100% (14 / 14)      Lines: 100% (47 / 47)      Ignored: none     

All files » lib/bundlers/ » watchify.js
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 99 100 101 102 103 104 1051   1         1 40   40   1 40           40 6     40           40 1     40   1 7     1 8     8 8 8   8   1 7       7   7   7     1 12 12           1 3   3 2       1 8     8 4 4   4 2   4     8 4 4   4 3   4            
module.exports = setupWatchify
 
var concat = require('concat-stream')
  , minimist = require('minimist')
  , resolve = require('resolve')
  , through = require('through')
 
function setupWatchify(dir, entryPoints, flags, ready) {
  var watchify = require(dir)
 
  resolve('browserify/bin/args.js', {basedir: dir}, onmodule)
 
  function onmodule(err, browserifyModule) {
    var parseArgs = require(browserifyModule)
      , watchifies = {}
      , lastOut = {}
      , lastErr = {}
      , aborted
 
    for(var key in entryPoints) {
      watchifies[entryPoints[key]] = buildWatchify(entryPoints[key])
    }
 
    handlePath.bundler = dir
 
    // NB: this is a backdoor way of preventing retries
    // in the tests. eventually all bundlers should grow
    // a `.close()` that allows end users to `.close` a
    // beefy server.
    handlePath._abort = function() {
      aborted = true
    }
 
    return ready(null, handlePath)
 
    function handlePath(entryPath) {
      return watchifies[entryPath]()
    }
 
    function buildWatchify(entry) {
      var watcher = watchify(parseArgs([entry].concat(flags)))
        , waiting = []
 
      watcher.on('update', onupdate)
      watcher.on('error', onerror)
      onupdate()
 
      return onrequest
 
      function onrequest() {
        var stdout = through()
          , stderr = through()
          , io
 
        io = {stderr: stderr, stdout: stdout}
 
        fulfillRequest(io)
 
        return io
      }
 
      function fulfillRequest(io) {
        process.nextTick(function() {
          lastOut[entry] ? (io.stdout.end(lastOut[entry]), io.stderr.end()) :
          lastErr[entry] ? (io.stderr.end(lastErr[entry]), io.stdout.end()) :
          waiting.push(fulfillRequest.bind(null, io))
        })
      }
 
      function onerror(err) {
        watcher.removeListener('update', onupdate)
 
        if(!aborted) {
          setTimeout(buildWatchify, minimist(flags).delay || 600, entry)
        }
      }
 
      function onupdate() {
        var build = watcher.bundle()
          , caught
 
        build.on('error', function(err) {
          lastErr[entry] = err.stack || (err + '')
          lastOut[entry] = null
 
          waiting.slice().forEach(function(fulfill) {
            fulfill()
          })
          waiting.length = 0
        })
 
        build.pipe(concat(function(data) {
          lastErr[entry] = null
          lastOut[entry] = data
 
          waiting.slice().forEach(function(fulfill) {
            fulfill()
          })
          waiting.length = 0
        }))
      }
    }
  }
}