<commit_before>"use strict";
/* eslint no-unused-vars: "off" */

// Check that the deps in node_modules match what's in package.json.
var safestart = require('safestart');
safestart(__dirname);

var fs = require('fs'),
    path = require('path'),
    argv = require('yargs').argv,
    app = require('app'),
    electron = require('electron'),
    browserWindow = require('browser-window'),
    request = require('request'),
    os = require('os'),
    autoUpdater = require('auto-updater'),
    menu = require('menu'),
    tray = require('tray'),
    ini = require('ini'),
    dialog = require('dialog'),
    ipcMain = require('ipc-main');

var launched_from_installer = false;
var platform = os.platform();
switch (platform) {
  case "darwin":
    platform = "mac";
}
var version = app.getVersion();
var trayMenu = null;
var subpy = null;

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
var mainWindow = null;

var open_url = null; // This is for if someone opens a URL before the client is open

if (argv.userData) {
  try {
    app.setPath('userData', argv.userData);
  } catch (e) {
    throw new Error('The passed in userData directory does not appear to be valid: ' + e);
  }
}

var handleStartupEvent = function() {
  if (process.platform !== 'win32') {
    return false;
  }

  var squirrelCommand = process.argv[1];

  function exeSquirrelCommand(args, cb) {
    var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
    var child = require('child_process').spawn(updateDotExe, args, { detached: true });
    child.on('close', function() {
      cb();
    });
  }

  function install(cb) {
    var target = path.basename(process.execPath);
    exeSquirrelCommand(["--createShortcut", target], cb);
  }

  function uninstall(cb) {
    var target = path.basename(process.execPath);
    exeSquirrelCommand(["--removeShortcut", target], cb);
  }

  switch (squirrelCommand) {
  case '--squirrel-install':
    install(app.quit);
    break;

  case '--squirrel-updated':
    // Always quit when done
    app.quit();
    return true;

  case '--squirrel-uninstall':
    // Always quit when done
    uninstall(app.quit);
    return true;

  case '--squirrel-obsolete':
    // This is called on the outgoing version of your app before
    // we update to the new version - it's the opposite of
    // --squirrel-updated
    app.quit();
    return true;
  }
};

if (handleStartupEvent()) {
  console.log('Client started on Windows...');
}

// Set daemon binary name
var daemon = "openbazaard.exe";
if (platform == "mac" || platform == "linux") {
  daemon = "openbazaard";
}

var serverPath = __dirname + path.sep + '..' + path.sep + 'OpenBazaar-Server' + path.sep,
    serverOut = '',
    serverRunning = false,
    pendingKill,
    startAfterClose;

var kill_local_server = function() {
  if (subpy) {
    if (pendingKill) {
      startAfterClose && pendingKill.removeListener('close', startAfterClose);
      return;
    } else if (!serverRunning) {
      return;
    }
    pendingKill = subpy;
    pendingKill.once('close', () => {
      pendingKill = null;
    });

    console.log('Shutting down server daemon');

    if (platform == "mac" || platform == "linux") {
      subpy.kill('SIGINT');
    } else {
      require('child_process').spawn("taskkill", ["/pid", subpy.pid, '/f', '/t']);
    }
  }
};

var start_local_server = function() {
  if(fs.existsSync(serverPath)) {
    if (pendingKill) {
      pendingKill.once('close', startAfterClose = () => {
        start_local_server();
      });

      return;
    }

    if (serverRunning) return;

    console.log('Starting OpenBazaar Server');

    var random_port = Math.floor((Math.random() * 10000) + 30000);

    subpy = require('child_process').spawn(serverPath + daemon, ['start', '--loglevel', 'debug', '-p', random_port], {
      detach: false,
      cwd: __dirname + path.sep + '..' + path.sep + 'OpenBazaar-Server'
    });

    serverRunning = true;

    var stdout = '';
    var stderr = '';

    subpy.stdout.on('data', function (buf) {
      console.log('[STR] stdout "%s"', String(buf));
      stdout += buf;
      serverOut += buf;
    });
    subpy.stderr.on('data', function (buf) {
      console.log('[STR] stderr "%s"', String(buf));
      fs.appendFile(__dirname + path.sep + "python_error.log", String(buf), function(err) {
          if(err) {
              return console.log(err);
          }
      });
      stderr += buf;
    });
    subpy.on('error', function (err) {
      console.log('Python error %s', String(err));
    });
    subpy.on('close', function (code) {
      console.log('exited with ' + code);
      console.log('[END] stdout "%s"', stdout);
      console.log('[END] stderr "%s"', stderr);
      serverRunning = false;
    });
    subpy.unref();
  } else {
    mainWindow.webContents.executeJavaScript("console.log('Unable to find OpenBazaar-Server at: '" + serverPath + "')");
  }
  if (fs.existsSync(__dirname + path.sep + '..' + path.sep + 'gpg')) {
       process.env.PATH = __dirname + path.sep + '..' + path.sep + 'gpg' + path.sep + 'pub' + path.sep + ';' + process.env.PATH;
   }
};<commit_msg>Added error capture to regular error event<commit_after>"use strict";
/* eslint no-unused-vars: "off" */

// Check that the deps in node_modules match what's in package.json.
var safestart = require('safestart');
safestart(__dirname);

var fs = require('fs'),
    path = require('path'),
    argv = require('yargs').argv,
    app = require('app'),
    electron = require('electron'),
    browserWindow = require('browser-window'),
    request = require('request'),
    os = require('os'),
    autoUpdater = require('auto-updater'),
    menu = require('menu'),
    tray = require('tray'),
    ini = require('ini'),
    dialog = require('dialog'),
    ipcMain = require('ipc-main');

var launched_from_installer = false;
var platform = os.platform();
switch (platform) {
  case "darwin":
    platform = "mac";
}
var version = app.getVersion();
var trayMenu = null;
var subpy = null;

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
var mainWindow = null;

var open_url = null; // This is for if someone opens a URL before the client is open

if (argv.userData) {
  try {
    app.setPath('userData', argv.userData);
  } catch (e) {
    throw new Error('The passed in userData directory does not appear to be valid: ' + e);
  }
}

var handleStartupEvent = function() {
  if (process.platform !== 'win32') {
    return false;
  }

  var squirrelCommand = process.argv[1];

  function exeSquirrelCommand(args, cb) {
    var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
    var child = require('child_process').spawn(updateDotExe, args, { detached: true });
    child.on('close', function() {
      cb();
    });
  }

  function install(cb) {
    var target = path.basename(process.execPath);
    exeSquirrelCommand(["--createShortcut", target], cb);
  }

  function uninstall(cb) {
    var target = path.basename(process.execPath);
    exeSquirrelCommand(["--removeShortcut", target], cb);
  }

  switch (squirrelCommand) {
  case '--squirrel-install':
    install(app.quit);
    break;

  case '--squirrel-updated':
    // Always quit when done
    app.quit();
    return true;

  case '--squirrel-uninstall':
    // Always quit when done
    uninstall(app.quit);
    return true;

  case '--squirrel-obsolete':
    // This is called on the outgoing version of your app before
    // we update to the new version - it's the opposite of
    // --squirrel-updated
    app.quit();
    return true;
  }
};

if (handleStartupEvent()) {
  console.log('Client started on Windows...');
}

// Set daemon binary name
var daemon = "openbazaard.exe";
if (platform == "mac" || platform == "linux") {
  daemon = "openbazaard";
}

var serverPath = __dirname + path.sep + '..' + path.sep + 'OpenBazaar-Server' + path.sep,
    serverOut = '',
    serverRunning = false,
    pendingKill,
    startAfterClose;

var kill_local_server = function() {
  if (subpy) {
    if (pendingKill) {
      startAfterClose && pendingKill.removeListener('close', startAfterClose);
      return;
    } else if (!serverRunning) {
      return;
    }
    pendingKill = subpy;
    pendingKill.once('close', () => {
      pendingKill = null;
    });

    console.log('Shutting down server daemon');

    if (platform == "mac" || platform == "linux") {
      subpy.kill('SIGINT');
    } else {
      require('child_process').spawn("taskkill", ["/pid", subpy.pid, '/f', '/t']);
    }
  }
};

var start_local_server = function() {
  if(fs.existsSync(serverPath)) {
    if (pendingKill) {
      pendingKill.once('close', startAfterClose = () => {
        start_local_server();
      });

      return;
    }

    if (serverRunning) return;

    console.log('Starting OpenBazaar Server');

    var random_port = Math.floor((Math.random() * 10000) + 30000);

    subpy = require('child_process').spawn(serverPath + daemon, ['start', '--loglevel', 'debug', '-p', random_port], {
      detach: false,
      cwd: __dirname + path.sep + '..' + path.sep + 'OpenBazaar-Server'
    });

    serverRunning = true;

    var stdout = '';
    var stderr = '';

    subpy.stdout.on('data', function (buf) {
      console.log('[STR] stdout "%s"', String(buf));
      stdout += buf;
      serverOut += buf;
    });
    subpy.stderr.on('data', function (buf) {
      console.log('[STR] stderr "%s"', String(buf));
      fs.appendFile(__dirname + path.sep + "python_error.log", String(buf), function(err) {
          if(err) {
              return console.log(err);
          }
      });
      stderr += buf;
    });
    subpy.on('error', function (err) {
      console.log('Python error %s', String(err));
      fs.appendFile(__dirname + path.sep + "python_error.log", String(err), function(error) {
          if(error) {
              return console.log(error);
          }
      });
    });
    subpy.on('close', function (code) {
      console.log('exited with ' + code);
      console.log('[END] stdout "%s"', stdout);
      console.log('[END] stderr "%s"', stderr);
      serverRunning = false;
    });
    subpy.unref();
  } else {
    mainWindow.webContents.executeJavaScript("console.log('Unable to find OpenBazaar-Server at: '" + serverPath + "')");
  }
  if (fs.existsSync(__dirname + path.sep + '..' + path.sep + 'gpg')) {
       process.env.PATH = __dirname + path.sep + '..' + path.sep + 'gpg' + path.sep + 'pub' + path.sep + ';' + process.env.PATH;
   }
};
{'fraction_non_alphanumeric': 0.1651031894934334, 'fraction_numerical': 0.0024390243902439024, 'mean_word_length': 2.9735370853522176, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, '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': '2378293', 'n_tokens_mistral': 3528, 'n_tokens_neox': 3415, 'n_words': 832}