<commit_before>import size from 'window-size'
import { relative } from 'path'
import { Writable } from 'stream'
import { createLogger, INFO, FATAL, DEBUG } from 'bunyan'
import strip from 'strip-ansi'

export default function create({ debug = false, format: type = 'normal' }) {
  const device = createLogger(
    { name: 'ez-build'
    , level: debug? 'trace' : 'info'
    , src: !!debug
    , stream: format(type, process.stdout, process.stderr, debug && process.stderr)
    }
  )

  const console = {}

  console.log = device.info.bind(device)
  console.info = device.info.bind(device)
  console.warn = device.warn.bind(device)
  console.error = device.error.bind(device)
  console.debug = device.debug.bind(device)

  return console
}

const location = /^\s+(?:at) (?:[^\s]+ \()?([^:]+):(\d+):(\d+)\)?$/

function format(type, stdout, stderr, stddbg) {
  const write = writers[type.toLowerCase()] || writers.normal

  return new Writable(
    { write(chunk, enc, next) {
        let record

        try {
          record = JSON.parse(chunk)
        } catch(e) {
          return stderr.write(chunk, enc), next()
        }

        const out
          = record.level <= DEBUG ? stddbg
          : record.level <= INFO  ? stdout
          : record.level <= FATAL ? stderr
          : undefined

        if (out) {
          write(out, record)
        }

        next()
      }
    }
  )
}

const writers =
{ json(out, record, enc) {
    out.write(`${JSON.stringify(record)}\n`, 'utf8')
  }
, normal(out, record, enc) {
    let msg = record.msg

    if (record.src) {
      let file = relative(`${__dirname}/../../`, record.src.file)
        , line = record.src.line
        , last = strip(msg).split('\n').slice(-1)[0]
        , src = `${file}:${line}`
        , pre = record.level <= DEBUG? '# ' : ''
        , pad = Math.max(0, size.width - last.length - src.length - pre.length - 2)

      msg = `${pre}${msg} ${' '.repeat(pad)} ${src}`
    }

    out.write(`${msg}\n`, 'utf8')
  }
}<commit_msg>Fix node 0.12 / 4.x stream incompatibility<commit_after>import size from 'window-size'
import { relative } from 'path'
import { Writable } from 'stream'
import { createLogger, INFO, FATAL, DEBUG } from 'bunyan'
import strip from 'strip-ansi'

export default function create({ debug = false, format: type = 'normal' }) {
  const device = createLogger(
    { name: 'ez-build'
    , level: debug? 'trace' : 'info'
    , src: !!debug
    , stream: format(type, process.stdout, process.stderr, debug && process.stderr)
    }
  )

  const console = {}

  console.log = device.info.bind(device)
  console.info = device.info.bind(device)
  console.warn = device.warn.bind(device)
  console.error = device.error.bind(device)
  console.debug = device.debug.bind(device)

  return console
}

const location = /^\s+(?:at) (?:[^\s]+ \()?([^:]+):(\d+):(\d+)\)?$/

function format(type, stdout, stderr, stddbg) {
  const write = writers[type.toLowerCase()] || writers.normal

  let stream = new Writable({ write: doWrite })
  stream._write = doWrite

  return stream

  function doWrite(chunk, enc, next) {
    let record

    try {
      record = JSON.parse(chunk)
    } catch(e) {
      return stderr.write(chunk, enc), next()
    }

    const out
      = record.level <= DEBUG ? stddbg
      : record.level <= INFO  ? stdout
      : record.level <= FATAL ? stderr
      : undefined

    if (out) {
      write(out, record)
    }

    next()
  }
}

const writers =
{ json(out, record, enc) {
    out.write(`${JSON.stringify(record)}\n`, 'utf8')
  }
, normal(out, record, enc) {
    let msg = record.msg

    if (record.src) {
      let file = relative(`${__dirname}/../../`, record.src.file)
        , line = record.src.line
        , last = strip(msg).split('\n').slice(-1)[0]
        , src = `${file}:${line}`
        , pre = record.level <= DEBUG? '# ' : ''
        , pad = Math.max(0, size.width - last.length - src.length - pre.length - 2)

      msg = `${pre}${msg} ${' '.repeat(pad)} ${src}`
    }

    out.write(`${msg}\n`, 'utf8')
  }
}
{'fraction_non_alphanumeric': 0.17927101413339946, 'fraction_numerical': 0.003967270022315894, 'mean_word_length': 2.560458958517211, 'pattern_counts': {'":': 0, '<': 11, '<?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': '1828548', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1349, 'n_words': 315}