<commit_before>'use strict';

var child_process = require('child_process');
var fs = require('fs');
var os = require('os');
var path = require('path');

var AccountManager = require('./account-manager');
var AtomHelper = require('./atom-helper');
var StateController = require('./state-controller');
var utils = require('./utils');
const Logger = require('./logger');

var Tracker = require('../ext/telemetry/metrics').Tracker;

var Installer = class {
  static get INTERVAL() {
    return 2500;
  }

  constructor(paths) {
    paths = paths || [];
    this.flow = null;
    this.path = paths.length ? paths[0] : os.homedir();
  }

  instrument(fn) {
    this.flow.disable();
    fn.call(this)
    .then(() => this.flow.enable())
    .catch(() => this.flow.enable());
  }

  init(flow, onFinish) {
    this.flow = flow;
    this.flow.onInstall(() => this.instrument(this.install));
    this.flow.onCreateAccount(() => this.instrument(this.createAccount));
    this.flow.onLogin(() => this.instrument(this.login));
    this.flow.onResetPassword(() => this.instrument(this.resetPassword));
    this.flow.onLoginBack(() => this.flow.showCreateAccount());
    this.flow.onWhitelist(() => this.instrument(this.whitelist));
    this.flow.onSkipWhitelist(() => this.instrument(this.skipWhitelist));

    this.parsedEmail = null;
    this.kiteCanRun = true;
    this.installPromise = null;
    this.authTimerID = null;
    this.whitelistTimerID = null;
    this.onFinish = onFinish || null;

    this.opts = {
      install: false,
      onDownload: () => {
        this.flow.installStep.progress.addMarker(20, 4000, 10);
      },
      onInstallStart: () => {
        this.flow.installStep.progress.addMarker(30, 1000, 10);
      },
      onMount: () => {
        this.flow.installStep.progress.addMarker(40, 500, 10);
      },
      onCopy: () => {
        this.flow.installStep.progress.addMarker(60, 500, 10);
      },
      onUnmount: () => {
        this.flow.installStep.progress.addMarker(80, 500, 10);
      },
      onRemove: () => {
        this.flow.installStep.progress.addMarker(90, 500, 10);
      },
    };

    this.parseUserEmail();
    Tracker.trackEvent('start flow');
  }

  get projectName() {
    if (!this.path) {
      return null;
    }
    var parts = this.path.split('/');
    return parts[parts.length - 1];
  }

  install() {
    Tracker.trackEvent('enabled kite');
    this.flow.showCreateAccount(this.parsedEmail);
    this.installPromise = StateController.downloadKiteRelease(this.opts);
    return this.installPromise.catch((err) => {
      Tracker.trackEvent('error downloading to memory', { error: err });
      Logger.error('error installing kite:', err);
    });
  }

  installAndRun(callback) {
    Tracker.trackEvent('saving to disk');
    return (this.installPromise || StateController.downloadKiteRelease(this.opts))
    .then(() => {
      StateController.installKite(this.opts).then(() => {
        Tracker.trackEvent('installed successfully');
        StateController.runKiteAndWait(30, Installer.INTERVAL).then(() => {
          Tracker.trackEvent('app started successfully');
          this.flow.installStep.progress.addMarker(95, 100, 10);
          if (typeof callback === 'function') {
            callback();
          }
        }, (err) => {
          Tracker.trackEvent('error running kite', { error: err });
          Logger.error('error running kite:', err);
          this.kiteCanRun = false;
          this.flow.installStep.progress.addMarker(95, 100, 10);
        });
      }, (err) => {
        Tracker.trackEvent('error saving kite', { error: err });
        Logger.error('error saving kite:', err);
      });
    });
  }

  createAccount() {
    Tracker.trackEvent('creating account');
    try {
      var data = this.flow.createAccountStep.data;
      return AccountManager.createAccount(data, (resp) => {
        Tracker.trackEvent('attempt to create account');
        switch (resp.statusCode) {
          case 200:
            utils.handleResponseData(resp, (raw) => {
              var info = JSON.parse(raw);
              Tracker.props.distinct_id = info.id;
              Tracker.trackEvent('account created');
            });
            if (typeof this.onAuthenticate === 'function') {
              this.onAuthenticate();
            }
            this.flow.showWhitelist({
              email: data.email,
              path: this.path,
            });
            break;
          case 409:
            Tracker.trackEvent('email exists showing login');
            this.flow.showLogin();
            break;
          default:
            Tracker.trackEvent('create account error', { code: resp.statusCode });
            this.flow.createAccountStep.showError('An error occurred');
            break;
        }
      });
    } catch (err) {
      Tracker.trackEvent('error creating account', { error: err.message });
      Logger.error('error creating account:', err);
      return Promise.reject();
    }
  }

  login() {<commit_msg>:bug: Fix install not started after account creation<commit_after>'use strict';

var child_process = require('child_process');
var fs = require('fs');
var os = require('os');
var path = require('path');

var AccountManager = require('./account-manager');
var AtomHelper = require('./atom-helper');
var StateController = require('./state-controller');
var utils = require('./utils');
const Logger = require('./logger');

var Tracker = require('../ext/telemetry/metrics').Tracker;

var Installer = class {
  static get INTERVAL() {
    return 2500;
  }

  constructor(paths) {
    paths = paths || [];
    this.flow = null;
    this.path = paths.length ? paths[0] : os.homedir();
  }

  instrument(fn) {
    this.flow.disable();
    fn.call(this)
    .then(() => this.flow.enable())
    .catch(() => this.flow.enable());
  }

  init(flow, onFinish) {
    this.flow = flow;
    this.flow.onInstall(() => this.instrument(this.install));
    this.flow.onCreateAccount(() => this.instrument(this.createAccount));
    this.flow.onLogin(() => this.instrument(this.login));
    this.flow.onResetPassword(() => this.instrument(this.resetPassword));
    this.flow.onLoginBack(() => this.flow.showCreateAccount());
    this.flow.onWhitelist(() => this.instrument(this.whitelist));
    this.flow.onSkipWhitelist(() => this.instrument(this.skipWhitelist));

    this.parsedEmail = null;
    this.kiteCanRun = true;
    this.installPromise = null;
    this.authTimerID = null;
    this.whitelistTimerID = null;
    this.onFinish = onFinish || null;

    this.opts = {
      install: false,
      onDownload: () => {
        this.flow.installStep.progress.addMarker(20, 4000, 10);
      },
      onInstallStart: () => {
        this.flow.installStep.progress.addMarker(30, 1000, 10);
      },
      onMount: () => {
        this.flow.installStep.progress.addMarker(40, 500, 10);
      },
      onCopy: () => {
        this.flow.installStep.progress.addMarker(60, 500, 10);
      },
      onUnmount: () => {
        this.flow.installStep.progress.addMarker(80, 500, 10);
      },
      onRemove: () => {
        this.flow.installStep.progress.addMarker(90, 500, 10);
      },
    };

    this.parseUserEmail();
    Tracker.trackEvent('start flow');
  }

  get projectName() {
    if (!this.path) {
      return null;
    }
    var parts = this.path.split('/');
    return parts[parts.length - 1];
  }

  install() {
    Tracker.trackEvent('enabled kite');
    this.flow.showCreateAccount(this.parsedEmail);
    this.installPromise = StateController.downloadKiteRelease(this.opts);
    return this.installPromise.catch((err) => {
      Tracker.trackEvent('error downloading to memory', { error: err });
      Logger.error('error installing kite:', err);
    });
  }

  installAndRun(callback) {
    Tracker.trackEvent('saving to disk');
    return (this.installPromise || StateController.downloadKiteRelease(this.opts))
    .then(() => {
      StateController.installKite(this.opts).then(() => {
        Tracker.trackEvent('installed successfully');
        StateController.runKiteAndWait(30, Installer.INTERVAL).then(() => {
          Tracker.trackEvent('app started successfully');
          this.flow.installStep.progress.addMarker(95, 100, 10);
          if (typeof callback === 'function') {
            callback();
          }
        }, (err) => {
          Tracker.trackEvent('error running kite', { error: err });
          Logger.error('error running kite:', err);
          this.kiteCanRun = false;
          this.flow.installStep.progress.addMarker(95, 100, 10);
        });
      }, (err) => {
        Tracker.trackEvent('error saving kite', { error: err });
        Logger.error('error saving kite:', err);
      });
    });
  }

  createAccount() {
    Tracker.trackEvent('creating account');
    try {
      var data = this.flow.createAccountStep.data;
      return AccountManager.createAccount(data, (resp) => {
        Tracker.trackEvent('attempt to create account');
        switch (resp.statusCode) {
          case 200:
            utils.handleResponseData(resp, (raw) => {
              var info = JSON.parse(raw);
              Tracker.props.distinct_id = info.id;
              Tracker.trackEvent('account created');
            });
            if (typeof this.onAuthenticate === 'function') {
              this.onAuthenticate();
            }
            this.installAndRun();
            this.flow.showWhitelist({
              email: data.email,
              path: this.path,
            });
            break;
          case 409:
            Tracker.trackEvent('email exists showing login');
            this.flow.showLogin();
            break;
          default:
            Tracker.trackEvent('create account error', { code: resp.statusCode });
            this.flow.createAccountStep.showError('An error occurred');
            break;
        }
      });
    } catch (err) {
      Tracker.trackEvent('error creating account', { error: err.message });
      Logger.error('error creating account:', err);
      return Promise.reject();
    }
  }

  login() {
{'fraction_non_alphanumeric': 0.15509879853043393, 'fraction_numerical': 0.014298480786416443, 'mean_word_length': 2.5118549511854953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2435289', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2898, 'n_words': 540}