<commit_before>/**
 * Module dependencies
 */

var cwd = process.cwd()
var fs = require('fs')
var qs = require('qs')
var url = require('url')
var path = require('path')
var inquirer = require('inquirer')
var request = require('request-promise')
var AnvilConnect = require('anvil-connect-nodejs/promises')
var IDToken = require('anvil-connect-nodejs/lib/IDToken')
var AccessToken = require('anvil-connect-nodejs/lib/AccessToken')


/**
 * Load client configuration
 */

var home = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']
try {
  var config = require(path.join(home, '.nvl', 'config.json'))
} catch (e) {
  console.log(e)
  process.exit(1)
}

/**
 * Login command
 */

function registerLogin (cli, options, done) {
  cli.command('login')
    .handler(function (data, flags, done) {
      var anvil = new AnvilConnect(config)

      /**
       * Login
       */

      function login (email, password) {
        var self = this

        // construct the endpoint
        // this one isn't included in openid-configuration
        var uri = this.issuer + '/signin'

        // authorization parameters
        var params = this.authorizationParams({
          provider: 'password',
          email: email,
          password: password
        })

        return new Promise(function (resolve, reject) {
          request({
            url: uri,
            method: 'POST',
            form: params,
            headers: { 'referer': uri }
          })
          // this is so ugly, but redirects get treated as errors by HTTP clients.
          // So we need to handle the expected result using `catch`. Ugh.
          .catch(function (err) {
            if (err.statusCode === 302) {
              var u = url.parse(err.response.headers.location)
              var code = qs.parse(u.query).code

              self.token({ code: code })
              .then(function (data) {
                resolve(data)
              })
              .catch(function (err) {
                reject(err)
              })
            } else {
              reject(err)
            }
          })
        })
      }

      AnvilConnect.prototype.login = login


      inquirer.prompt([
        {
          type: 'input',
          name: 'email',
          message: 'Enter your email'
        },
        {
          type: 'password',
          name: 'password',
          message: 'Enter your password'
        }
      ], function (answers) {

        anvil
          // get the provider configuration
          .discover()

          // get the provider's public keys
          .then(function (configuration) {
            return anvil.jwks()
          })

          // login
          .then(function (jwks) {
            return anvil.login(answers.email, answers.password)
          })

          // store the tokens
          .then(function (tokens) {
            var filepath = path.join(home, '.nvl', 'tokens.json')
            fs.writeFile(filepath, JSON.stringify(tokens, null, 2), function (err) {
              console.log(tokens)
              done()
            })
          })

          .catch(function (err) {
            console.log(err, err.stack)
            done()
          })
      })
    })
  done()
}

/**
 * Export
 */

module.exports = registerLogin<commit_msg>style(login): fix formatting<commit_after>/**
 * Module dependencies
 */

var fs = require('fs')
var qs = require('qs')
var url = require('url')
var path = require('path')
var inquirer = require('inquirer')
var request = require('request-promise')
var AnvilConnect = require('anvil-connect-nodejs/promises')

/**
 * Load client configuration
 */

var home = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']
try {
  var config = require(path.join(home, '.nvl', 'config.json'))
} catch (e) {
  console.log(e)
  process.exit(1)
}

/**
 * Login command
 */

function registerLogin (cli, options, done) {
  cli.command('login')
    .handler(function (data, flags, done) {
      var anvil = new AnvilConnect(config)

      // add login method to anvil client
      function login (email, password) {
        var self = this

        // construct the endpoint
        // this one isn't included in openid-configuration
        var uri = this.issuer + '/signin'

        // authorization parameters
        var params = this.authorizationParams({
          provider: 'password',
          email: email,
          password: password
        })

        return new Promise(function (resolve, reject) {
          request({
            url: uri,
            method: 'POST',
            form: params,
            headers: { 'referer': uri }
          })
          // this is so ugly, but redirects get treated as errors by HTTP clients.
          // So we need to handle the expected result using `catch`. Ugh.
          .catch(function (err) {
            if (err.statusCode === 302) {
              var u = url.parse(err.response.headers.location)
              var code = qs.parse(u.query).code

              self.token({ code: code })
              .then(function (data) {
                resolve(data)
              })
              .catch(function (err) {
                reject(err)
              })
            } else {
              reject(err)
            }
          })
        })
      }

      AnvilConnect.prototype.login = login

      // prompt for email and password
      inquirer.prompt([
        {
          type: 'input',
          name: 'email',
          message: 'Enter your email'
        },
        {
          type: 'password',
          name: 'password',
          message: 'Enter your password'
        }
      ], function (answers) {

        anvil
          // get the provider configuration
          .discover()

          // get the provider's public keys
          .then(function (configuration) {
            return anvil.jwks()
          })

          // login
          .then(function (jwks) {
            return anvil.login(answers.email, answers.password)
          })

          // store the tokens
          .then(function (tokens) {
            var filepath = path.join(home, '.nvl', 'tokens.json')
            fs.writeFile(filepath, JSON.stringify(tokens, null, 2), function (err) {
              if (err) {
                console.log(err)
                process.exit(1)
              }

              console.log(tokens)
              done()
            })
          })

          .catch(function (err) {
            console.log(err, err.stack)
            done()
          })
      })
    })
  done()
}

/**
 * Export
 */

module.exports = registerLogin
{'fraction_non_alphanumeric': 0.12437509468262384, 'fraction_numerical': 0.0022723829722769277, 'mean_word_length': 1.579914028917546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7181831', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1791, 'n_words': 466}