<commit_before>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
        }))
      }
    }
  }
}
<commit_msg>if there was an error, return it<commit_after>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) {
    if(err) {
      return ready(err)
    }

    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
        }))
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.12681713848508033, 'fraction_numerical': 0.0019127773527161439, 'mean_word_length': 2.148103552077062, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6925786', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1532, 'n_words': 319}