<commit_before>
import path from 'path'
import {partial} from 'underscore'

import noop from './noop'
import spawn from './spawn'
import mkdirp from '../promised/mkdirp'

let self = {
  parse_butler_status: function (opts, token) {
    let {onprogress = noop} = opts

    let status = JSON.parse(token)
    if (status.Percent) {
      onprogress({percent: status.Percent})
    }
  },

  /*
   * Uses https://github.com/itchio/butler to download a file
   */
  request: function (opts) {
    let {url, dest} = opts

    return mkdirp(path.dirname(dest))
      .then(() => spawn({
        command: 'butler',
        args: ['dl', url, dest],
        ontoken: partial(self.parse_butler_status, opts)
      }))
  }
}

export default self<commit_msg>handle messages & errors from butler<commit_after>
import path from 'path'
import {partial} from 'underscore'

import noop from './noop'
import spawn from './spawn'
import mkdirp from '../promised/mkdirp'

let self = {
  parse_butler_status: function (opts, onerror, token) {
    let {onprogress = noop} = opts

    let status = JSON.parse(token)
    if (status.Percent) {
      onprogress({percent: status.Percent})
    } else if (status.Message) {
      console.log(`Message from butler: ${status.Message}`)
    } else if (status.Error) {
      onerror(status.Error)
    }
  },

  /*
   * Uses https://github.com/itchio/butler to download a file
   */
  request: function (opts) {
    let {url, dest} = opts
    let err = null
    let onerror = (e) => err = e

    return mkdirp(path.dirname(dest))
      .then(() => spawn({
        command: 'butler',
        args: ['dl', url, dest],
        ontoken: partial(self.parse_butler_status, opts, onerror)
      })).then((res) => {
        if (err) { throw err }
        return res
      })
  }
}

export default self
{'fraction_non_alphanumeric': 0.1415929203539823, 'fraction_numerical': 0.0, 'mean_word_length': 2.706967213114754, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3989823', 'n_tokens_mistral': 601, 'n_tokens_neox': 587, 'n_words': 165}