<commit_before>'use strict';

require('colors')

var st = require('st')
  , http = require('http')
  , path = require('path')
  , url = require('url')
  , fs = require('fs')
  , os = require('os')
  , js = require('atomify-js')
  , css = require('atomify-css')
  , cssFiles = require('atomify-css/css')
  , lessFiles = require('atomify-css/less')
  , open = require('open')
  , through = require('through')
  , gaze = require('gaze')
  , browserSync = require('browser-sync')
  , defaultBundlePath = __dirname + '/_bundle.js'
  , baseUrl

module.exports = function (args) {

  console.log('')

  var hostname = args.server.hostname || args.server.h ? os.hostname() + '.local' : 'localhost'
  args.server.url = args.server.url || 'http://' + hostname + ':1337/default'

  var mount = st(args.server.st || { path: process.cwd(), cache: false })
    , parsedUrl = url.parse(args.server.url)
    , port = args.server.port || parsedUrl.port
    , serverPath = getNormalizedPath(args.server.path || parsedUrl.path)
    , lr = args.server.lr || args.server.l
    , launch = args.server.open || args.server.o
    , handler = args.server.handler || Function();

  if (args.server.sync || args.server.s) lr = {sync: true}

  // the router checks these, so they have to exist
  args.js = args.js || {}
  args.js.alias = getNormalizedPath(args.js.alias || args.js.entry)
  args.css = args.css || {}
  args.css.alias = getNormalizedPath(args.css.alias || args.css.entry)

  baseUrl = parsedUrl.protocol + '//' + parsedUrl.hostname

  if (lr) {
    // if live reloading, use watchify
    args.js.watch = true
    args.js.output = args.js.output || defaultBundlePath
    // listen for initial bundle completion before opening
    js(args.js).on('end', function () {
      if (launch) open(baseUrl + ':' + bs.options.port + serverPath)
    })

    if (!lr.quiet) {
      js.emitter.on('changed', function (filepath) {
        console.log(path.relative(process.cwd(), filepath), 'changed'.grey);
        browserSync.notify('Compiling, please wait!')
      })

      js.emitter.on('bundle', function (time) {
        console.log(('bundle updated in ' + time + 'ms').grey);
        browserSync.reload()
      })
    }

    lr = typeof lr === 'boolean' ? {} : lr
    lr.patterns = lr.patterns ? lr.patterns.concat(args.js.output) : ['*.html', '*.css', args.js.output]

    lr.port = lr.port || 3000
    lr.sync = parseSyncOptions(lr.sync || lr.s)

    if (port >= lr.port && port <= lr.port + 2) {
      throw new Error('Ports ' + lr.port + ' through ' + (lr.port + 2) + 'in use for Live Reload')
    }

    startFileWatch(lr, args)
    console.log(('Live reload server listening on port ' + lr.port).grey)

    var bs = browserSync({
      proxy: parsedUrl.hostname + ':' + port
      , port: lr.port
      , injectChanges: true
      , ghostMode: lr.sync
      , notify: !!lr.sync
      , logSnippet: false
      , open: false
      , logLevel: !!lr.verbose ? 'debug' : 'info'
      // don't minify for speed!
      , minify: false
      , startPath: '/default'
    })
  }

  http.createServer(function (req, res) {

    switch (req.url.split('?')[0]) {
      case args.js.alias || args.js.entry:
        if (lr) {
          res.setHeader('Content-Type', 'text/javascript')
          res.end(fs.readFileSync(args.js.output, {encoding: 'utf8'}))
        } else {
          js(args.js, responder('javascript', res))
        }
        break

      case args.css.alias || args.css.entry:
        css(args.css, responder('css', res))
        break

      case '/default':
        serveDefaultPage(res, args)
        break

      default:
        if (handler(req, res, args)) {
          return
        }<commit_msg>Prevent a race condition in JS server watching<commit_after>'use strict';

require('colors')

var st = require('st')
  , http = require('http')
  , path = require('path')
  , url = require('url')
  , fs = require('fs')
  , os = require('os')
  , js = require('atomify-js')
  , css = require('atomify-css')
  , cssFiles = require('atomify-css/css')
  , lessFiles = require('atomify-css/less')
  , open = require('open')
  , through = require('through')
  , gaze = require('gaze')
  , browserSync = require('browser-sync')
  , defaultBundlePath = __dirname + '/_bundle.js'
  , baseUrl

module.exports = function (args) {

  console.log('')

  var hostname = args.server.hostname || args.server.h ? os.hostname() + '.local' : 'localhost'
  args.server.url = args.server.url || 'http://' + hostname + ':1337/default'

  var mount = st(args.server.st || { path: process.cwd(), cache: false })
    , parsedUrl = url.parse(args.server.url)
    , port = args.server.port || parsedUrl.port
    , serverPath = getNormalizedPath(args.server.path || parsedUrl.path)
    , lr = args.server.lr || args.server.l
    , launch = args.server.open || args.server.o
    , handler = args.server.handler || Function();

  if (args.server.sync || args.server.s) lr = {sync: true}

  // the router checks these, so they have to exist
  args.js = args.js || {}
  args.js.alias = getNormalizedPath(args.js.alias || args.js.entry)
  args.css = args.css || {}
  args.css.alias = getNormalizedPath(args.css.alias || args.css.entry)

  baseUrl = parsedUrl.protocol + '//' + parsedUrl.hostname

  if (lr) {
    // if live reloading, use watchify
    args.js.watch = true
    args.js.output = args.js.output || defaultBundlePath

    if (!lr.quiet) {
      js.emitter.on('changed', function (filepath) {
        console.log(path.relative(process.cwd(), filepath), 'changed'.grey);
        browserSync.notify('Compiling, please wait!')
      })

      js.emitter.on('bundle', function (time) {
        console.log(('bundle updated in ' + time + 'ms').grey);
        browserSync.reload()
      })
    }

    lr = typeof lr === 'boolean' ? {} : lr
    lr.patterns = lr.patterns ? lr.patterns.concat(args.js.output) : ['*.html', '*.css', args.js.output]

    lr.port = lr.port || 3000
    lr.sync = parseSyncOptions(lr.sync || lr.s)

    if (port >= lr.port && port <= lr.port + 2) {
      throw new Error('Ports ' + lr.port + ' through ' + (lr.port + 2) + 'in use for Live Reload')
    }

    startFileWatch(lr, args)
    console.log(('Live reload server listening on port ' + lr.port).grey)

    var bs = browserSync({
      proxy: parsedUrl.hostname + ':' + port
      , port: lr.port
      , injectChanges: true
      , ghostMode: lr.sync
      , notify: !!lr.sync
      , logSnippet: false
      , open: false
      , logLevel: !!lr.verbose ? 'debug' : 'info'
      // don't minify for speed!
      , minify: false
      , startPath: '/default'
    })

    // listen for initial bundle completion before opening
    js(args.js).on('end', function onJSBundled(){
      if (launch && bs.options && bs.options.port)
        open(baseUrl + ':' + bs.options.port + serverPath)
    })
  }

  http.createServer(function (req, res) {

    switch (req.url.split('?')[0]) {
      case args.js.alias || args.js.entry:
        if (lr) {
          res.setHeader('Content-Type', 'text/javascript')
          res.end(fs.readFileSync(args.js.output, {encoding: 'utf8'}))
        } else {
          js(args.js, responder('javascript', res))
        }
        break

      case args.css.alias || args.css.entry:
        css(args.css, responder('css', res))
        break

      case '/default':
        serveDefaultPage(res, args)
        break

      default:
        if (handler(req, res, args)) {
          return
        }
{'fraction_non_alphanumeric': 0.16829562190386932, 'fraction_numerical': 0.003213281563797028, 'mean_word_length': 2.5965334617236397, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '709549', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2461, 'n_words': 534}