<commit_before>var fs = require('fs'),
    rsa = require('rsa'),
    request = require('request'),
    os = require('os');

log = require('npmlog');
log.heading = 'figaro';

var eol = os.platform
    ? ('win32' == os.platform() ? '\r\n' : '\n')
    : '\n';

var defaultContents = [
    '{',
    '}'].join(eol);

var defaultFilename = 'figaro.json';

exports.setup = function(filename, contents, skipGitIgnore) {
    filename = filename || defaultFilename;
    contents = contents || defaultContents;

    fs.lstat(filename, function(err, stats) {
       if (err) {
           log.info('creating ' + filename);
           fs.writeFile(filename, contents);
       } else {
           log.warn(filename + ' already exists, we will not overwrite');
       }
    });

    if (!skipGitIgnore) {
        fs.lstat('.gitignore', function(err, stats) {
            var gitIgnoreContents = eol + filename + eol;
            if (err) {
                log.info('creating .gitignore and adding ' + filename + ' to it');
                fs.writeFile('.gitignore', gitIgnoreContents);
            } else {
                if (fs.readFileSync('.gitignore', 'utf8').indexOf(gitIgnoreContents) == -1) {
                    log.info('adding ' + filename + ' to .gitignore');
                    fs.open('.gitignore', 'a', null, function(err, fd) {
                        fs.write(fd, gitIgnoreContents, null, 'utf8', function() {
                            fs.close(fd);
                        })
                    });
                } else {
                    log.info('.gitignore already ignores ' + filename);
                }
            }
        });
    }
};

function encrypt(publicKey, plaintext) {
    publicKey = publicKey.replace('-----BEGIN RSA PUBLIC KEY-----', '-----BEGIN PUBLIC KEY-----');
    publicKey = publicKey.replace('-----END RSA PUBLIC KEY-----', '-----END PUBLIC KEY-----');

    var keypair = rsa.createRsaKeypair({publicKey: publicKey});

    return keypair.encrypt(plaintext, 'utf8', 'base64');
}


var travisURL = exports.travisURL = 'http://travis-ci.org/';

function travisPublicKey(slug, callback) {
    if (!slug) {
        try {
            var pkg = JSON.parse(fs.readFileSync('./package.json'));
            if (pkg.repository && pkg.repository.url) {
                slug = pkg.repository.url.match(/git@github\.com:(.*)\.git$/)[1];
            } else {
                log.error('in order to perform travis encryption I need to know your slug: either you provide it explicitly or you provide your git repository URL in your module package.json');
            }
        } catch (err) {
            log.error('in order to perform travis encryption I need to know your slug: either you provide it explicitly or you execute figaro where your module package.json resides');
        }
    }

    if (slug) {
        var url = travisURL + slug + '.json';
        log.http('GET', url);
        request({'uri': url}, function (err, response, body) {
            if (err) {
                callback(err);
            } else {
                try {
                    var publicKey = JSON.parse(body).public_key;
                } catch (e) {
                    callback(new Error('could not obtain travis public key for this module'));
                }
                callback(null, publicKey);
            }
        });
    } else {
        callback(new Error('could not obtain travis slug for this module'));
    }
}

exports.travisPublicKey = travisPublicKey;

exports.travisEncrypt = function(filename, slug, values) {
    filename = filename || defaultFilename;

    try {
        var figaroContents = fs.readFileSync(filename);
    } catch (ignore) {
        if (!values) {
            log.warn('no values to encrypt');
            return;
        }
    }

    var toEncrypt = [];
    for(var i in values) {
        toEncrypt.push(i + '=' + values[i]);
    }

    var figaroValues = JSON.parse(figaroContents);
    for(var i in figaroValues) {
        toEncrypt.push(i + '=' + figaroValues[i]);
    }

    travisPublicKey(slug, function(err, publicKey) {
        if (err) {
            log.error(err);
        } else {
            try {
                log.info('add the following to your .travis.yml file:');
                console.log('env: {secure: ' + encrypt(publicKey, toEncrypt.join(' ')) + '}');
                log.info('finished');
            } catch (e) {
                log.error(e);
            }
        }
    });
}<commit_msg>fixing output<commit_after>var fs = require('fs'),
    rsa = require('rsa'),
    request = require('request'),
    os = require('os');

log = require('npmlog');
log.heading = 'figaro';

var eol = os.platform
    ? ('win32' == os.platform() ? '\r\n' : '\n')
    : '\n';

var defaultContents = [
    '{',
    '}'].join(eol);

var defaultFilename = 'figaro.json';

exports.setup = function(filename, contents, skipGitIgnore) {
    filename = filename || defaultFilename;
    contents = contents || defaultContents;

    fs.lstat(filename, function(err, stats) {
       if (err) {
           log.info('creating ' + filename);
           fs.writeFile(filename, contents);
       } else {
           log.warn(filename + ' already exists, we will not overwrite');
       }
    });

    if (!skipGitIgnore) {
        fs.lstat('.gitignore', function(err, stats) {
            var gitIgnoreContents = eol + filename + eol;
            if (err) {
                log.info('creating .gitignore and adding ' + filename + ' to it');
                fs.writeFile('.gitignore', gitIgnoreContents);
            } else {
                if (fs.readFileSync('.gitignore', 'utf8').indexOf(gitIgnoreContents) == -1) {
                    log.info('adding ' + filename + ' to .gitignore');
                    fs.open('.gitignore', 'a', null, function(err, fd) {
                        fs.write(fd, gitIgnoreContents, null, 'utf8', function() {
                            fs.close(fd);
                        })
                    });
                } else {
                    log.info('.gitignore already ignores ' + filename);
                }
            }
        });
    }
};

function encrypt(publicKey, plaintext) {
    publicKey = publicKey.replace('-----BEGIN RSA PUBLIC KEY-----', '-----BEGIN PUBLIC KEY-----');
    publicKey = publicKey.replace('-----END RSA PUBLIC KEY-----', '-----END PUBLIC KEY-----');

    var keypair = rsa.createRsaKeypair({publicKey: publicKey});

    return keypair.encrypt(plaintext, 'utf8', 'base64');
}


var travisURL = exports.travisURL = 'http://travis-ci.org/';

function travisPublicKey(slug, callback) {
    if (!slug) {
        try {
            var pkg = JSON.parse(fs.readFileSync('./package.json'));
            if (pkg.repository && pkg.repository.url) {
                slug = pkg.repository.url.match(/git@github\.com:(.*)\.git$/)[1];
            } else {
                log.error('in order to perform travis encryption I need to know your slug: either you provide it explicitly or you provide your git repository URL in your module package.json');
            }
        } catch (err) {
            log.error('in order to perform travis encryption I need to know your slug: either you provide it explicitly or you execute figaro where your module package.json resides');
        }
    }

    if (slug) {
        var url = travisURL + slug + '.json';
        log.http('GET', url);
        request({'uri': url}, function (err, response, body) {
            if (err) {
                callback(err);
            } else {
                try {
                    var publicKey = JSON.parse(body).public_key;
                } catch (e) {
                    callback(new Error('could not obtain travis public key for this module'));
                }
                callback(null, publicKey);
            }
        });
    } else {
        callback(new Error('could not obtain travis slug for this module'));
    }
}

exports.travisPublicKey = travisPublicKey;

exports.travisEncrypt = function(filename, slug, values) {
    filename = filename || defaultFilename;

    try {
        var figaroContents = fs.readFileSync(filename);
    } catch (ignore) {
        if (!values) {
            log.warn('no values to encrypt');
            return;
        }
    }

    var toEncrypt = [];
    for(var i in values) {
        toEncrypt.push(i + '=' + values[i]);
    }

    var figaroValues = JSON.parse(figaroContents);
    for(var i in figaroValues) {
        toEncrypt.push(i + '=' + figaroValues[i]);
    }

    travisPublicKey(slug, function(err, publicKey) {
        if (err) {
            log.error(err);
        } else {
            try {
                log.info('add the following to your .travis.yml file:');
                console.log('env:\n - {secure: "' + encrypt(publicKey, toEncrypt.join(' ')) + '"}');
                log.info('finished');
            } catch (e) {
                log.error(e);
            }
        }
    });
}
{'fraction_non_alphanumeric': 0.14499553172475424, 'fraction_numerical': 0.0020107238605898124, 'mean_word_length': 1.7951920074929753, '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': '3237121', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2501, 'n_words': 623}