<commit_before>#!/usr/bin/env node

var explain = require('explain-error')
var mapLimit = require('map-limit')
var resolve = require('resolve')
var garnish = require('garnish')
var mkdirp = require('mkdirp')
var subarg = require('subarg')
var bole = require('bole')
var http = require('http')
var path = require('path')
var pump = require('pump')
var opn = require('opn')
var fs = require('fs')

var bankai = require('./')

var argv = subarg(process.argv.slice(2), {
  string: [ 'open', 'port' ],
  boolean: [ 'optimize', 'verbose', 'help', 'version', 'debug' ],
  default: {
    optimize: false,
    open: false,
    port: 8080,
    debug: false
  },
  alias: {
    css: 'c',
    debug: 'd',
    help: 'h',
    html: 'H',
    js: 'j',
    open: 'o',
    optimize: 'O',
    port: 'p',
    verbose: 'V',
    version: 'v'
  }
})

var usage = `
  Usage:
    $ bankai <command> [options]

  Commands:
    <default>                      Run 'bankai start'
    start <filename>               Start a bankai server
    build <filename> <directory>   Compile and export files to a directory

    Options:
      -c, --css=<subargs>     Pass subarguments to sheetify
      -d, --debug             Include sourcemaps [default: false]
      -h, --help              Print usage
      -H, --html=<subargs>    Pass subarguments to create-html
      -j, --js=<subargs>      Pass subarguments to browserify
      -o, --open=<browser>    Open html in a browser [default: system default]
      -O, --optimize          Optimize assets served by bankai [default: false]
      -p, --port=<n>          Bind bankai to a port [default: 8080]
      -V, --verbose           Include debug messages

  Examples:
    $ bankai index.js -p 8080            # start bankai on port 8080
    $ bankai index.js --open             # open html in the browser
    $ bankai -c [ -u sheetify-cssnext ]  # use cssnext in sheetify
    $ bankai -j [ -t brfs ]              # use brfs in browserify
    $ bankai build index.js dist/        # compile and export to dist/
    $ bankai build -O index.js dist/     # optimize compiled files
`

main(argv)

function main (argv) {
  if ((argv._[0] !== 'build') && (argv._[0] !== 'start')) {
    argv._.unshift('start')
  }
  var cmd = argv._[0]
  var _entry = argv._[1] || 'index.js'
  var localEntry = './' + _entry.replace(/^.\//, '')
  var entry = resolve.sync(localEntry, { basedir: process.cwd() })
  var outputDir = argv._[2] || 'dist'
  startLogging(argv.verbose)

  if (argv.h) {
    console.info(usage)
    return process.exit()
  }

  if (argv.v) {
    console.info(require('../package.json').version)
    return process.exit()
  }

  if (cmd === 'start') {
    start(entry, argv, handleError)
  } else if (cmd === 'build') {
    build(entry, outputDir, argv, function (err) {
      if (err) throw err
      process.exit()
    })
  } else {
    console.error(usage)
    return process.exit(1)
  }

  function handleError (err) {
    if (err) throw err
  }
}

function start (entry, argv, done) {
  var assets = bankai(entry, argv)
  var port = argv.port

  http.createServer((req, res) => {
    switch (req.url) {
      case '/': return assets.html(req, res).pipe(res)
      case '/bundle.js': return assets.js(req, res).pipe(res)
      case '/bundle.css': return assets.css(req, res).pipe(res)
      default: return (res.statusCode = 404 && res.end('404 not found'))
    }
  }).listen(port, function () {
    var relative = path.relative(process.cwd(), entry)
    var addr = 'http://localhost:' + port
    console.info('Started bankai for', relative, 'on', addr)
    if (argv.open !== false) {
      var app = (argv.open.length) ? argv.open : 'system browser'
      opn(addr, { app: argv.open || null })
        .catch(function (err) {
          done(explain(err, `err running ${app}`))
        })
        .then(done)
    }
  })
}

function build (entry, outputDir, argv, done) {
  mkdirp.sync(outputDir)
  var assets = bankai(entry, argv)
  var files = [ 'index.html', 'bundle.js', 'bundle.css' ]
  mapLimit(files, Infinity, iterator, done)
  function iterator (file, done) {
    var file$ = fs.createWriteStream(path.join(outputDir, file))
    var source$ = assets[file.replace(/^.*\./, '')]()
    pump(source$, file$, done)
  }
}

function startLogging (verbose) {
  var level = (verbose) ? 'debug' : 'info'
  var pretty = garnish({ level: level, name: 'bankai' })
  pretty.pipe(process.stdout)
  bole.output({ stream: pretty, level: level })
}
<commit_msg>Always return HTML when browser ACCEPT wants it (#122)<commit_after>#!/usr/bin/env node

var explain = require('explain-error')
var mapLimit = require('map-limit')
var resolve = require('resolve')
var garnish = require('garnish')
var mkdirp = require('mkdirp')
var subarg = require('subarg')
var bole = require('bole')
var http = require('http')
var path = require('path')
var pump = require('pump')
var opn = require('opn')
var fs = require('fs')

var bankai = require('./')

var argv = subarg(process.argv.slice(2), {
  string: [ 'open', 'port' ],
  boolean: [ 'optimize', 'verbose', 'help', 'version', 'debug' ],
  default: {
    optimize: false,
    open: false,
    port: 8080,
    debug: false
  },
  alias: {
    css: 'c',
    debug: 'd',
    help: 'h',
    html: 'H',
    js: 'j',
    open: 'o',
    optimize: 'O',
    port: 'p',
    verbose: 'V',
    version: 'v'
  }
})

var usage = `
  Usage:
    $ bankai <command> [options]

  Commands:
    <default>                      Run 'bankai start'
    start <filename>               Start a bankai server
    build <filename> <directory>   Compile and export files to a directory

    Options:
      -c, --css=<subargs>     Pass subarguments to sheetify
      -d, --debug             Include sourcemaps [default: false]
      -h, --help              Print usage
      -H, --html=<subargs>    Pass subarguments to create-html
      -j, --js=<subargs>      Pass subarguments to browserify
      -o, --open=<browser>    Open html in a browser [default: system default]
      -O, --optimize          Optimize assets served by bankai [default: false]
      -p, --port=<n>          Bind bankai to a port [default: 8080]
      -V, --verbose           Include debug messages

  Examples:
    $ bankai index.js -p 8080            # start bankai on port 8080
    $ bankai index.js --open             # open html in the browser
    $ bankai -c [ -u sheetify-cssnext ]  # use cssnext in sheetify
    $ bankai -j [ -t brfs ]              # use brfs in browserify
    $ bankai build index.js dist/        # compile and export to dist/
    $ bankai build -O index.js dist/     # optimize compiled files
`

main(argv)

function main (argv) {
  if ((argv._[0] !== 'build') && (argv._[0] !== 'start')) {
    argv._.unshift('start')
  }
  var cmd = argv._[0]
  var _entry = argv._[1] || 'index.js'
  var localEntry = './' + _entry.replace(/^.\//, '')
  var entry = resolve.sync(localEntry, { basedir: process.cwd() })
  var outputDir = argv._[2] || 'dist'
  startLogging(argv.verbose)

  if (argv.h) {
    console.info(usage)
    return process.exit()
  }

  if (argv.v) {
    console.info(require('../package.json').version)
    return process.exit()
  }

  if (cmd === 'start') {
    start(entry, argv, handleError)
  } else if (cmd === 'build') {
    build(entry, outputDir, argv, function (err) {
      if (err) throw err
      process.exit()
    })
  } else {
    console.error(usage)
    return process.exit(1)
  }

  function handleError (err) {
    if (err) throw err
  }
}

function start (entry, argv, done) {
  var assets = bankai(entry, argv)
  var port = argv.port

  http.createServer((req, res) => {
    switch (req.url) {
      case '/': return assets.html(req, res).pipe(res)
      case '/bundle.js': return assets.js(req, res).pipe(res)
      case '/bundle.css': return assets.css(req, res).pipe(res)
      default:
        if (req.headers['accept'].indexOf('html') > 0) {
          return assets.html(req, res).pipe(res)
        }
        res.statusCode = 404
        res.end('404 not found')
    }
  }).listen(port, function () {
    var relative = path.relative(process.cwd(), entry)
    var addr = 'http://localhost:' + port
    console.info('Started bankai for', relative, 'on', addr)
    if (argv.open !== false) {
      var app = (argv.open.length) ? argv.open : 'system browser'
      opn(addr, { app: argv.open || null })
        .catch(function (err) {
          done(explain(err, `err running ${app}`))
        })
        .then(done)
    }
  })
}

function build (entry, outputDir, argv, done) {
  mkdirp.sync(outputDir)
  var assets = bankai(entry, argv)
  var files = [ 'index.html', 'bundle.js', 'bundle.css' ]
  mapLimit(files, Infinity, iterator, done)
  function iterator (file, done) {
    var file$ = fs.createWriteStream(path.join(outputDir, file))
    var source$ = assets[file.replace(/^.*\./, '')]()
    pump(source$, file$, done)
  }
}

function startLogging (verbose) {
  var level = (verbose) ? 'debug' : 'info'
  var pretty = garnish({ level: level, name: 'bankai' })
  pretty.pipe(process.stdout)
  bole.output({ stream: pretty, level: level })
}

{'fraction_non_alphanumeric': 0.16034729091108912, 'fraction_numerical': 0.006813935597318387, 'mean_word_length': 2.5463756819953236, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, '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': '686963', 'n_tokens_mistral': 3098, 'n_tokens_neox': 3020, 'n_words': 857}