<commit_before>"use strict"

var
    _             = require('lodash'),          // Module to ease the pain!
    Promise       = require('es6-promise').Promise,
    app           = require('app'),
    ipc           = require('ipc'),             // Module to have interprocess communication
    request       = require('superagent'),      // Module used to create/recieve HTTP requests
    mainWindow    = null,                       // Keep a global reference to avoid it being garbage collected
    BrowserWindow = require('browser-window');  // Module to create browser windows


// If all windows are closed ...
app.on('window-all-closed', function () {
  // Quit the application if we are not on an Apple machine (darwin)
  if (process.platform != 'darwin') {
    app.quit();
  }
});

// When the application is ready (when Electron is ready to create browser windows)
app.on('ready', function () {
  mainWindow = new BrowserWindow({      // Create the browser window
    width: 800,
    height: 600,
    show: false     // Hide the window initially
  });

  // Load the first html file
  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  // Make the window visible when HTML is done loading
  // This is non-essential but it is a good practice to hide non-loaded pages
  mainWindow.webContents.on('did-stop-loading', function () {
    mainWindow.show();  // Show window
  });

  ipc.on('auth-request', function(event, arg) {
    authenticateAsync()
      .then(function (result) {
        getTokenAsync(result)
          .then(function (res) {
            getFilesAsync(res)
              .then(function (result) {
                _.each(result, function (item) {
                  console.log(item.name);
                });
              })
          })
      })
      .catch(function (err) {
        console.error(err);
      });

  });

  mainWindow.on('closed', function () {
    mainWindow = null;  // Dereference window
  });
});

////////////////////////////////
/* FUNCTIONS WITH CALL BACKS  */
////////////////////////////////

/* Gets authentication code */
function authenticate(callback) {
  // Use let instead of var for block scoped variables, this is why we had to add "use strict" at the top of the file
  let client_id = "2060";
  let callback_uri = "https://localhost/callback";
  // Create url using ES6 template strings (yey!)
  // let url = `https://put.io`;  // uncommnet and comment line below to logout for testing only
  let url = `https://api.put.io/v2/oauth2/authenticate?client_id=${client_id}&response_type=code&redirect_uri=${callback_uri}`;
  let code = null;

  // Create authentication window, pointing to Put.io API auth page
  let authWindow = new BrowserWindow({
    width: 500,
    height: 800,
    show: false,      // hide window initially
    // frame: false,
    'node-integration': false
  });

  // Start loading Put.io's page
  authWindow.loadUrl(url);

  // Present window to the user once it has been fully loaded
  authWindow.webContents.on('did-stop-loading', function () {
    authWindow.show();
  });

  // Detect form completion event
  authWindow.webContents.on('did-get-redirect-request', function (event, oldUrl, newUrl, isMainFrame) {
    if (newUrl.startsWith(callback_uri)) {
      // Strip code from the uri (Expected format: https://localhost/callback?code=COOKIES-HERE)
      code = newUrl.split(/[=]/)[1];
      authWindow.close(); // Close the auth window (This will trigger the close event, so we will clean up the references properly)
    }
  });

  // Close window
  authWindow.on('close', function() {
    authWindow = null;

    if (code) {
      callback(null, code);
    } else {
      callback(new Error("Could not Authenticate application"));
    }

  });
}


/* Gets access token */
function getToken(auth_code, callback) {<commit_msg>Change crude nested Promises to production line promises<commit_after>"use strict"

var
    _             = require('lodash'),          // Module to ease the pain!
    Promise       = require('es6-promise').Promise,
    app           = require('app'),
    ipc           = require('ipc'),             // Module to have interprocess communication
    request       = require('superagent'),      // Module used to create/recieve HTTP requests
    mainWindow    = null,                       // Keep a global reference to avoid it being garbage collected
    BrowserWindow = require('browser-window');  // Module to create browser windows


// If all windows are closed ...
app.on('window-all-closed', function () {
  // Quit the application if we are not on an Apple machine (darwin)
  if (process.platform != 'darwin') {
    app.quit();
  }
});

// When the application is ready (when Electron is ready to create browser windows)
app.on('ready', function () {
  mainWindow = new BrowserWindow({      // Create the browser window
    width: 800,
    height: 600,
    show: false     // Hide the window initially
  });

  // Load the first html file
  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  // Make the window visible when HTML is done loading
  // This is non-essential but it is a good practice to hide non-loaded pages
  mainWindow.webContents.on('did-stop-loading', function () {
    mainWindow.show();  // Show window
  });

  ipc.on('auth-request', function (event, arg) {
    authenticateAsync().then(function (auth_code) {
      return getTokenAsync(auth_code);
    }).then(function (token) {
      return getFilesAsync(token)
    }).then(function (files) {
      _.each(files, function (file) {
        console.log(file.name);
      });
    }).catch(function (err) {
      console.error(err);
    });
  });

  mainWindow.on('closed', function () {
    mainWindow = null;  // Dereference window
  });
});

////////////////////////////////
/* FUNCTIONS WITH CALL BACKS  */
////////////////////////////////

/* Gets authentication code */
function authenticate(callback) {
  // Use let instead of var for block scoped variables, this is why we had to add "use strict" at the top of the file
  let client_id = "2060";
  let callback_uri = "https://localhost/callback";
  // Create url using ES6 template strings (yey!)
  // let url = `https://put.io`;  // uncommnet and comment line below to logout for testing only
  let url = `https://api.put.io/v2/oauth2/authenticate?client_id=${client_id}&response_type=code&redirect_uri=${callback_uri}`;
  let code = null;

  // Create authentication window, pointing to Put.io API auth page
  let authWindow = new BrowserWindow({
    width: 500,
    height: 800,
    show: false,      // hide window initially
    // frame: false,
    'node-integration': false
  });

  // Start loading Put.io's page
  authWindow.loadUrl(url);

  // Present window to the user once it has been fully loaded
  authWindow.webContents.on('did-stop-loading', function () {
    authWindow.show();
  });

  // Detect form completion event
  authWindow.webContents.on('did-get-redirect-request', function (event, oldUrl, newUrl, isMainFrame) {
    if (newUrl.startsWith(callback_uri)) {
      // Strip code from the uri (Expected format: https://localhost/callback?code=COOKIES-HERE)
      code = newUrl.split(/[=]/)[1];
      authWindow.close(); // Close the auth window (This will trigger the close event, so we will clean up the references properly)
    }
  });

  // Close window
  authWindow.on('close', function () {
    authWindow = null;

    if (code) {
      callback(null, code);
    } else {
      callback(new Error("Could not Authenticate application"));
    }

  });
}


/* Gets access token */
function getToken(auth_code, callback) {
{'fraction_non_alphanumeric': 0.14113167913876853, 'fraction_numerical': 0.005513981882630957, 'mean_word_length': 2.5252198056455346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, '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': '5666555', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2102, 'n_words': 679}