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

var UPLOAD = process.argv[2] === 'up';

var request = require('request-json');
var fs = require('fs');
var Q = require('q');
var slug = require('slug');
var conf = require('rc')('opthub', {});
var prompt = require('sync-prompt').prompt;
var yesNo = require('yes-no').parse;
var jsdiff = require('diff');
require('colors');

var onError = function (err) {
  console.log('ERROR :( / Docs: https://github.com/tomfuertes/opthub');
  console.log(err);
};

var different = function (start, end) {
  // console.log(start, end);
  var diff = jsdiff.diffLines(start, end);
  diff.forEach(function (part) {
    // green for additions, red for deletions
    // grey for common parts
    var color = part.added ? 'green' :
      part.removed ? 'red' : 'grey';
    process.stderr.write(part.value[color]);
  });
  console.log();
  return start != end; // jshint ignore:line
};

if (!conf.api_token) throw new Error('.opthubrc needs to have an api_token');
if (!conf.experiment_id) throw new Error('.opthubrc needs to have an experiment_id');


var client = request.newClient('https://www.optimizelyapis.com/experiment/v1/', {
  headers: {
    'Token': conf.api_token
  },
  rejectUnauthorized: false
});

var get = function (url) {
  var defer = Q.defer();
  client.get(url, function (err, res, body) {
    if (err) defer.reject(err);
    else defer.resolve(body);
  });
  return defer.promise;
};
var put = function (url, data) {
  var defer = Q.defer();
  client.put(url, data, function (err, res, body) {
    console.log('HERE');
    console.log(arguments);
    if (err) defer.reject(err);
    else defer.resolve(body);
  });
  return defer.promise;
};

var getAnswer = function (q) {
  var a = prompt(q + '? [Y/n]: ');
  if (a === '') a = true;
  else a = yesNo(a);
  return a;
};

var conditionalWriteFile = function (name, txt) {
  fs.readFile(name, function (err, data) {
    if (err) data = '';
    console.log(('############ diff of ' + name + ' ############').magenta);
    if (different(data.toString(), txt)) {<commit_msg>blue stands out<commit_after>#! /usr/bin/env node

var UPLOAD = process.argv[2] === 'up';

var request = require('request-json');
var fs = require('fs');
var Q = require('q');
var slug = require('slug');
var conf = require('rc')('opthub', {});
var prompt = require('sync-prompt').prompt;
var yesNo = require('yes-no').parse;
var jsdiff = require('diff');
require('colors');

var onError = function (err) {
  console.log('ERROR :( / Docs: https://github.com/tomfuertes/opthub');
  console.log(err);
};

var different = function (start, end) {
  // console.log(start, end);
  var diff = jsdiff.diffLines(start, end);
  diff.forEach(function (part) {
    // green for additions, red for deletions
    // grey for common parts
    var color = part.added ? 'green' :
      part.removed ? 'red' : 'grey';
    process.stderr.write(part.value[color]);
  });
  console.log();
  return start != end; // jshint ignore:line
};

if (!conf.api_token) throw new Error('.opthubrc needs to have an api_token');
if (!conf.experiment_id) throw new Error('.opthubrc needs to have an experiment_id');


var client = request.newClient('https://www.optimizelyapis.com/experiment/v1/', {
  headers: {
    'Token': conf.api_token
  },
  rejectUnauthorized: false
});

var get = function (url) {
  var defer = Q.defer();
  client.get(url, function (err, res, body) {
    if (err) defer.reject(err);
    else defer.resolve(body);
  });
  return defer.promise;
};
var put = function (url, data) {
  var defer = Q.defer();
  client.put(url, data, function (err, res, body) {
    console.log('HERE');
    console.log(arguments);
    if (err) defer.reject(err);
    else defer.resolve(body);
  });
  return defer.promise;
};

var getAnswer = function (q) {
  var a = prompt(q + '? [Y/n]: ');
  if (a === '') a = true;
  else a = yesNo(a);
  return a;
};

var conditionalWriteFile = function (name, txt) {
  fs.readFile(name, function (err, data) {
    if (err) data = '';
    console.log(('############ diff of ' + name + ' ############').blue);
    if (different(data.toString(), txt)) {
{'fraction_non_alphanumeric': 0.19295261239368167, 'fraction_numerical': 0.0009720534629404617, 'mean_word_length': 3.69327251995439, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '6141896', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1416, 'n_words': 370}