<commit_before>#! /usr/bin/env node

var homeDir     = process.env.HOME || process.env.USERPROFILE,

    fs          = require('fs'),
    yesno       = require('yesno'),
    prompt      = require('prompt'),

    _           = require('./lib/utils'),
    reporter    = require('./lib/reporter'),
    File        = require('./lib/file'),

    Promise     = require('bluebird'),

    gitSwap     = new File(homeDir + '/.gitswap'),
    gitConfig   = new File(homeDir + '/.gitconfig'),

    args        = process.argv.slice(2),

    current,
    config,
    swap;

// console table
require('console.table');

// git config first
gitConfig.exists()

    //
    // read the  git config file
    //
    .then(function () {
        return gitConfig.read();
    }, function () {
        // error no file found
        console.error(reporter.get('noGitConfig', 'red'));
        exit();
    })

    //
    // read the users config
    //
    .then(function (gitConfigContents) {

        config = gitConfigContents;

        return gitConfig.getUser(gitConfigContents);
    })

    //
    // write the user check file exists
    //
    .then(function (credentials) {

        current = credentials;

        gitSwap.exists()

            //
            // git swap is present
            //
            .then(function () {
                return gitSwap.read();
            }, function () {

                return gitSwap.create({
                    orig: credentials
                })
                .then(exit, exit);
            })

            //
            // resolved
            //
            .then(allFilesExists);
    });


/**
 * exit
 */
function exit () {
    process.exit();
}


/**
 * allFilesExists
 *
 * @param contents
 */
function allFilesExists (contents) {

    var swapProfile,
        table = [];

    if (contents) {

        contents = JSON.parse(contents);
        swap = contents;

        if (!args.length) {
            console.info(reporter.get('noProfile', 'yellow'));
            console.log(reporter.wrap('Curernt Profile: ' + current.username + ' <' + current.email + '>', 'blue'));
            console.table(getAllProfiles());

            return askForNewProfile();
        }

        swapProfile = contents[args[0]];

        if (swapProfile) {
            gitConfig.updateSwap(swapProfile, config, '.gitconfig swapped to: ' + swapProfile.username + ' <' + swapProfile.email + '>');
        } else {
            console.log(reporter.get('noTag', 'red'));
            askForNewProfile();
        }
    }
}


/**
 * gets all the tags in a preety format
 *
 * @method  getAllProfiles
 * @returns {Array}   [array of profiles]
 */
function getAllProfiles () {

    var profileTable = [],
        prop;

    _.each(swap, function (value, profile) {

        profileTable.push({
            tag:      profile,
            username: value.username,
            email:    value.email
        });
    });

    return profileTable;
}


/**
 * askForNewProfile
 * asks the user if they wish to add a new profile
 */
function askForNewProfile () {

    yesno.ask(reporter.wrap('Do you want to add a new profile? (y/n)', 'white'), true, function (ok) {

        if (ok) {
            getNewProfile();
        } else {
            exit();
        }
    });
};


/**<commit_msg>Adding sorting to the profiles table<commit_after>#! /usr/bin/env node

var homeDir     = process.env.HOME || process.env.USERPROFILE,

    fs          = require('fs'),
    yesno       = require('yesno'),
    prompt      = require('prompt'),

    _           = require('./lib/utils'),
    reporter    = require('./lib/reporter'),
    File        = require('./lib/file'),

    Promise     = require('bluebird'),

    gitSwap     = new File(homeDir + '/.gitswap'),
    gitConfig   = new File(homeDir + '/.gitconfig'),

    args        = process.argv.slice(2),

    current,
    config,
    swap;

// console table
require('console.table');

// git config first
gitConfig.exists()

    //
    // read the  git config file
    //
    .then(function () {
        return gitConfig.read();
    }, function () {
        // error no file found
        console.error(reporter.get('noGitConfig', 'red'));
        exit();
    })

    //
    // read the users config
    //
    .then(function (gitConfigContents) {

        config = gitConfigContents;

        return gitConfig.getUser(gitConfigContents);
    })

    //
    // write the user check file exists
    //
    .then(function (credentials) {

        current = credentials;

        gitSwap.exists()

            //
            // git swap is present
            //
            .then(function () {
                return gitSwap.read();
            }, function () {

                return gitSwap.create({
                    orig: credentials
                })
                .then(exit, exit);
            })

            //
            // resolved
            //
            .then(allFilesExists);
    });


/**
 * exit
 */
function exit () {
    process.exit();
}


/**
 * allFilesExists
 *
 * @param contents
 */
function allFilesExists (contents) {

    var swapProfile,
        table = [];

    if (contents) {

        contents = JSON.parse(contents);
        swap = contents;

        if (!args.length) {
            console.info(reporter.get('noProfile', 'yellow'));
            console.log(reporter.wrap('Curernt Profile: ' + current.username + ' <' + current.email + '>', 'blue'));
            console.table(getAllProfiles());

            return askForNewProfile();
        }

        swapProfile = contents[args[0]];

        if (swapProfile) {
            gitConfig.updateSwap(swapProfile, config, '.gitconfig swapped to: ' + swapProfile.username + ' <' + swapProfile.email + '>');
        } else {
            console.log(reporter.get('noTag', 'red'));
            askForNewProfile();
        }
    }
}


/**
 * gets all the tags in a preety format
 *
 * @method  getAllProfiles
 * @returns {Array}   [array of profiles]
 */
function getAllProfiles () {

    var profileTable = [],
        prop;

    _.each(swap, function (value, profile) {

        profileTable.push({
            tag:      profile,
            username: value.username,
            email:    value.email
        });
    });

    return profileTable.sort(function (a, b) {
        return a.tag.toLowerCase() > b.tag.toLowerCase();
    });
}


/**
 * askForNewProfile
 * asks the user if they wish to add a new profile
 */
function askForNewProfile () {

    yesno.ask(reporter.wrap('Do you want to add a new profile? (y/n)', 'white'), true, function (ok) {

        if (ok) {
            getNewProfile();
        } else {
            exit();
        }
    });
};


/**
{'fraction_non_alphanumeric': 0.14245184410930267, 'fraction_numerical': 0.0005972823652381663, 'mean_word_length': 1.6526732673267326, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '5535788', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1881, 'n_words': 418}