<commit_before>var util = require('util');
var prompt = require('prompt');
var request = require('request');
var argv = require('optimist')
    .alias('username', 'u')
    .alias('password', 'p')
    .alias('repo', 'r')
    .alias('status', 's')
    .default('status', 'open')
    .argv;
var couleurs = require('couleurs')();
var Table = require('le-table');
var GitUrlParse = require('giturlparse');

// Table defaults
Table.defaults.marks = {
    nw: '┌'
  , n:  '─'
  , ne: '┐'
  , e:  '│'
  , se: '┘'
  , s:  '─'
  , sw: '└'
  , w:  '│'
  , b:  ' '
  , mt: '┬'
  , ml: '├'
  , mr: '┤'
  , mb: '┴'
  , mm: '┼'
};

/**************************************************************************/
/* CONFIGURATION                                                          */
/**************************************************************************/

var CONFIG = {
    promptSchema: {
        properties: {
            username: {
                pattern: /^[a-zA-Z\s\-]+$/,
                message: 'Name must be only letters, spaces, or dashes'
            },
            password: {
                description: 'Password:',
                hidden: true
            }
        }
    }
};

/**************************************************************************/

var status = argv['status'];
var stati = ['closed', 'open'];
if (stati.indexOf(status) === -1) {
    console.error('Invalid status choose one of: ' + stati.join(', '));
    process.exit(4);
}

prompt.override = argv;
prompt.message = '';
prompt.delimiter = '';

// if a user is present, it must be a strong
var user = argv['username'];
if (user !== undefined && typeof user !== 'string' || user === '') {
    console.error('The username (-u) must be a non-empty string');
    process.exit(1);
}

// extract and validate the source (github/bitbucket), owner, and name from the repo URL
var son = extractSONFromFromUrl(argv['repo']);
if (['github', 'bitbucket'].indexOf(son.source) === -1) {
    console.error('Repository source not supported: ' + son.source);
    process.exit(11);
}

// if a user is provided, we also need a password for the basic authentication
if (user && !argv['password']) {

    prompt.get(CONFIG.promptSchema, function(err, result) {

        if (err) {
            console.error(err);
            process.exit(2);
        }

        // fetch and print the issues
        getIssues(son, user, result.password, issuesCallback);
    });
} else {
    // fetch and print the issues
    getIssues(son, user, argv['password'], issuesCallback);
}

function issuesCallback(err, issues) {

    if (err) {
        console.error(err);
        process.exit(3);
    }

    if (!issues || issues.length === 0) {
        console.log('No issues in this repository');
        process.exit(0);
    }

    var table = new Table();
    table.addRow([
        couleurs.bold('#'),
        couleurs.bold('Title'),
        couleurs.bold('Status')
    ]);

    issues.sort(function (a, b) {
        return a.number > b.number;
    });

    for (var i in issues) {
        var cI = issues[i];
        table.addRow([cI.number, cI.title, cI.state.toUpperCase()]);
    }

    console.log(table.toString());
}

/**************************************************************************/
/* IMPLEMENTATION FUNCTIONS
/**************************************************************************/

function getIssues(son, user, pass, callback) {

    var provider;

    try {
        provider = require('./providers/' + son.source);
    } catch (err) {}

    if (!provider) {
        return callback('Provider not suppoerted: ' + son.source);
    }

    var repoIssueUrl = provider.getIssueApiUrl(son, { status: status });
    if (!repoIssueUrl) {
        return callback('Missing issue API url format for source: ' + son.source);
    }

    var options = {
        url: repoIssueUrl,
        json: true,
        headers: {
            'user-agent': 'git-issues NPM Module'
        }
    };

    // if a user is provided we
    if (user) {
        options.auth = {
            user: user,
            pass: <PASSWORD>
        };
    }

    // fetch the issues from the web
    request.get(options, function(err, response, issueRespose) {

        if (err) {
            return callback(err);
        }

        if (response.statusCode != 200) {
            return callback(issueRespose.error || 'Error: ' + JSON.stringify(issueRespose));
        }

        // we convert the issues in a common format
        // (I took the GitHub format as example)
        callback(null, provider.convertIssues(issueRespose));
    });
}

function extractSONFromFromUrl(url) {


    var parsed = GitUrlParse(url);
    if (!parsed.source || !parsed.owner || !parsed.name) {
        console.error('Repository URL not supported: ' + parsed._);
        process.exit(11);
    }

    parsed.source = parsed.source.toLowerCase().replace(/\.com$/g, "");
    return parsed;
}

/**************************************************************************/<commit_msg>Rewriting the lib file as module<commit_after>// Dependencies
var Prompt = require('prompt')
  , Request = require('request')
  , GitUrlParse = require('giturlparse')
  , IsThere = require("is-there")
  , Gry = require("gry")
  ;

function GitIssues() {

}

GitIssues.prototype.fromPath = function (path, callback) {
    if (!IsThere(path)) {
        return callback(new Error("The provided path doesn't exist."));
    }
    var repo = new Gry(path);
    repo.exec("config --get remote.origin.url", function (err, stdout) {
        console.log(stdout);
    });
};

module.exports = new GitIssues();

//var status = argv['status'];
//var stati = ['closed', 'open'];
//if (stati.indexOf(status) === -1) {
//    console.error('Invalid status choose one of: ' + stati.join(', '));
//    process.exit(4);
//}
//
//// if a user is present, it must be a strong
//var user = argv['username'];
//if (user !== undefined && typeof user !== 'string' || user === '') {
//    console.error('The username (-u) must be a non-empty string');
//    process.exit(1);
//}
//
//// extract and validate the source (github/bitbucket), owner, and name from the repo URL
//var son = extractSONFromFromUrl(argv['repo']);
//if (['github', 'bitbucket'].indexOf(son.source) === -1) {
//    console.error('Repository source not supported: ' + son.source);
//    process.exit(11);
//}
//
//// if a user is provided, we also need a password for the basic authentication
//if (user && !argv['password']) {
//
//    prompt.get(CONFIG.promptSchema, function(err, result) {
//
//        if (err) {
//            console.error(err);
//            process.exit(2);
//        }
//
//        // fetch and print the issues
//        getIssues(son, user, result.password, issuesCallback);
//    });
//} else {
//    // fetch and print the issues
//    getIssues(son, user, argv['password'], issuesCallback);
//}
//
//function issuesCallback(err, issues) {
//
//    if (err) {
//        console.error(err);
//        process.exit(3);
//    }
//
//    if (!issues || issues.length === 0) {
//        console.log('No issues in this repository');
//        process.exit(0);
//    }
//
//    var table = new Table();
//    table.addRow([
//        couleurs.bold('#'),
//        couleurs.bold('Title'),
//        couleurs.bold('Status')
//    ]);
//
//    issues.sort(function (a, b) {
//        return a.number > b.number;
//    });
//
//    for (var i in issues) {
//        var cI = issues[i];
//        table.addRow([cI.number, cI.title, cI.state.toUpperCase()]);
//    }
//
//    console.log(table.toString());
//}
//
///**************************************************************************/
///* IMPLEMENTATION FUNCTIONS
///**************************************************************************/
//
//function getIssues(son, user, pass, callback) {
//
//    var provider;
//
//    try {
//        provider = require('./providers/' + son.source);
//    } catch (err) {}
//
//    if (!provider) {
//        return callback('Provider not suppoerted: ' + son.source);
//    }
//
//    var repoIssueUrl = provider.getIssueApiUrl(son, { status: status });
//    if (!repoIssueUrl) {
//        return callback('Missing issue API url format for source: ' + son.source);
//    }
//
//    var options = {
//        url: repoIssueUrl,
//        json: true,
//        headers: {
//            'user-agent': 'git-issues NPM Module'
//        }
//    };
//
//    // if a user is provided we
//    if (user) {
//        options.auth = {
//            user: user,
//            pass: <PASSWORD>
//        };
//    }
//
//    // fetch the issues from the web
//    request.get(options, function(err, response, issueRespose) {
//
//        if (err) {
//            return callback(err);
//        }
//
//        if (response.statusCode != 200) {
//            return callback(issueRespose.error || 'Error: ' + JSON.stringify(issueRespose));
//        }
//
//        // we convert the issues in a common format
//        // (I took the GitHub format as example)
//        callback(null, provider.convertIssues(issueRespose));
//    });
//}
//
//function extractSONFromFromUrl(url) {
//
//
//    var parsed = GitUrlParse(url);
//    if (!parsed.source || !parsed.owner || !parsed.name) {
//        console.error('Repository URL not supported: ' + parsed._);
//        process.exit(11);
//    }
//
//    parsed.source = parsed.source.toLowerCase().replace(/\.com$/g, "");
//    return parsed;
//}
{'fraction_non_alphanumeric': 0.23413497192499205, 'fraction_numerical': 0.003178302786312109, 'mean_word_length': 2.2926403906522497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1397792', 'n_tokens_mistral': 2878, 'n_tokens_neox': 2810, 'n_words': 689}