<commit_before>// npm modules
import moment from 'moment';
import inquirer from 'inquirer';
import PushBullet from 'pushbullet';
import ora from 'ora';
// our modules
import {Api, Config} from '../utils';

const config = new Config();
let api;

async function getUserInfoFromApi() {
  const startDateSpinner = ora('Getting startDate...').start();
  const completedReviews = await api.call({
    task: 'completed',
    body: {
      start_date: moment('2014-01-01').format('YYYY-MM-DDTHH:mm:ss.SSS'),
      end_date: moment().format('YYYY-MM-DDTHH:mm:ss.SSS'),
    },
  });
  const startDate = completedReviews.body
    .map(review => moment(review.assigned_at)) // returns date of review
    .map(date => date.valueOf()) // returns date in Unix Time (milliseconds from 1970)
    .reduce((acc, val) => { // returns the smallest number
      if (acc < val) {
        return acc;
      }
      return val;
    });
  config.startDate = moment(startDate).format('YYYY-MM-DD');
  startDateSpinner.succeed(`Startdate is ${config.startDate}`);

  const certSpinner = ora('Getting certifications...').start();
  const certifications = await api.call({
    task: 'certifications',
  });
  config.certs = certifications.body
    .filter(cert => cert.status === 'certified')
    .reduce((acc, cert) => {
      /* eslint-disable no-param-reassign */
      acc[cert.project.id] = {
        name: cert.project.name,
        price: cert.project.price,
      };
      return acc;
    }, {});
  certSpinner.succeed(`Your certifications:\n${JSON.stringify(config.certs, null, 2)}`);
  const configSpinner = ora('Saving configs...').start();
  config.save();
  configSpinner.succeed('Configs successfully save.');
  process.exit(0);
}

const accessToken = () => {
  inquirer.prompt([{
    type: 'input',
    name: 'pushbulletToken',
    message: 'Input your PushBullet access token:',
    validate(pushbulletToken) {
      const pusher = new PushBullet(pushbulletToken);
      return new Promise((resolve, reject) => {
        pusher.devices((err) => {
          if (err) reject(err);
          resolve(true);
        });
      });
    },
  }]).then((pushbulletToken) => {
    Object.assign(config, pushbulletToken);
    getUserInfoFromApi();
  });
};

const pushbullet = () => {
  inquirer.prompt([{
    type: 'confirm',
    name: 'pushbullet',
    message: 'Do you wish to use PushBullet?',
    default: false,
  }]).then((confirm) => {
    if (confirm.pushbullet) {
      accessToken();
    } else {
      getUserInfoFromApi();
    }
  });
};

const languages = () => {
  inquirer.prompt([{
    type: 'checkbox',
    message: 'Select Language(s) that you are certified for:\n',
    name: 'languages',
    choices: ['en-us', 'pt-br', 'zh-cn'],
    validate: (langs) => {
      if (langs.length < 1) {
        return 'You must choose at least one language.';
      }
      return true;
    },
  }]).then((langs) => {
    Object.assign(config, langs);
    pushbullet();
  });
};

const tokenInput = () => {
  inquirer.prompt([{
    type: 'input',
    name: 'token',
    message: 'Input your token:',
    validate(token) {
      api = new Api(token);
      return api.call({task: 'count'}).then((res) => {
        /* eslint-disable eqeqeq */
        if (res.statusCode == '200') {
          return true;
        }
        return 'The token was invalid, try again.';
      });
    },
  }]).then((token) => {
    Object.assign(config, token);
    config.tokenAge = moment().add(28, 'd');
    languages();
  });
};

export const setupCmd = () => {
  tokenInput();
};
<commit_msg>Shows certifications in a table.<commit_after>// npm modules
import moment from 'moment';
import inquirer from 'inquirer';
import PushBullet from 'pushbullet';
import Table from 'cli-table2';
import ora from 'ora';
// our modules
import {Api, Config} from '../utils';

const config = new Config();
let api;

async function getUserInfoFromApi() {
  const startDateSpinner = ora('Getting startDate...').start();
  const completedReviews = await api.call({
    task: 'completed',
    body: {
      start_date: moment('2014-01-01').format('YYYY-MM-DDTHH:mm:ss.SSS'),
      end_date: moment().format('YYYY-MM-DDTHH:mm:ss.SSS'),
    },
  });
  const startDate = completedReviews.body
    .map(review => moment(review.assigned_at)) // returns date of review
    .map(date => date.valueOf()) // returns date in Unix Time (milliseconds from 1970)
    .reduce((acc, val) => { // returns the smallest number
      if (acc < val) {
        return acc;
      }
      return val;
    });
  config.startDate = moment(startDate).format('YYYY-MM-DD');
  startDateSpinner.succeed(`Startdate is ${config.startDate}`);

  const certSpinner = ora('Getting certifications...').start();
  const certifications = await api.call({
    task: 'certifications',
  });
  config.certs = certifications.body
    .filter(cert => cert.status === 'certified')
    .reduce((acc, cert) => {
      /* eslint-disable no-param-reassign */
      acc[cert.project.id] = {
        name: cert.project.name,
        price: cert.project.price,
      };
      return acc;
    }, {});
  certSpinner.succeed('Your certifications:');

  // Create a new table for certifications
  const certsDetails = new Table({
    head: [
      {hAlign: 'center', content: 'id'},
      {hAlign: 'left', content: 'project name'},
      {hAlign: 'center', content: 'price'}],
    colWidths: [5, 40, 7],
  });

  certifications.body
    .filter(cert => cert.status === 'certified')
    .forEach((cert) => {
      certsDetails.push([
        {hAlign: 'center', content: cert.project.id},
        {hAlign: 'left', content: cert.project.name},
        {hAlign: 'center', content: cert.project.price},
      ]);
    });
  console.log(`${certsDetails.toString()}\n`);

  const configSpinner = ora('Saving configs...').start();
  config.save();
  configSpinner.succeed('Configs successfully save.');
  process.exit(0);
}

const accessToken = () => {
  inquirer.prompt([{
    type: 'input',
    name: 'pushbulletToken',
    message: 'Input your PushBullet access token:',
    validate(pushbulletToken) {
      const pusher = new PushBullet(pushbulletToken);
      return new Promise((resolve, reject) => {
        pusher.devices((err) => {
          if (err) reject(err);
          resolve(true);
        });
      });
    },
  }]).then((pushbulletToken) => {
    Object.assign(config, pushbulletToken);
    getUserInfoFromApi();
  });
};

const pushbullet = () => {
  inquirer.prompt([{
    type: 'confirm',
    name: 'pushbullet',
    message: 'Do you wish to use PushBullet?',
    default: false,
  }]).then((confirm) => {
    if (confirm.pushbullet) {
      accessToken();
    } else {
      getUserInfoFromApi();
    }
  });
};

const languages = () => {
  inquirer.prompt([{
    type: 'checkbox',
    message: 'Select Language(s) that you are certified for:\n',
    name: 'languages',
    choices: ['en-us', 'pt-br', 'zh-cn'],
    validate: (langs) => {
      if (langs.length < 1) {
        return 'You must choose at least one language.';
      }
      return true;
    },
  }]).then((langs) => {
    Object.assign(config, langs);
    pushbullet();
  });
};

const tokenInput = () => {
  inquirer.prompt([{
    type: 'input',
    name: 'token',
    message: 'Input your token:',
    validate(token) {
      api = new Api(token);
      return api.call({task: 'count'}).then((res) => {
        /* eslint-disable eqeqeq */
        if (res.statusCode == '200') {
          return true;
        }
        return 'The token was invalid, try again.';
      });
    },
  }]).then((token) => {
    Object.assign(config, token);
    config.tokenAge = moment().add(28, 'd');
    languages();
  });
};

export const setupCmd = () => {
  tokenInput();
};

{'fraction_non_alphanumeric': 0.17369374030005175, 'fraction_numerical': 0.005690636316606311, 'mean_word_length': 2.835813492063492, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, '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': '4700707', 'n_tokens_mistral': 2537, 'n_tokens_neox': 2479, 'n_words': 573}