<commit_before>/*
 * grunt-version-check
 * https://github.com/stevewillard/grunt-version-check
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

var bower = require("bower"),
    npm = require("npm"),
    _ = require("lodash"),
    semver = require("semver"),
    async = require("async");

require("colors");

/*
   Takes either a bower.json or package.json and returns some metadata about each object
   
   Input: (bower.json)
   { name: "my-bower-component", dependencies: {"foo" : "1.2.3"}, devDependencies: {"bar" : "4.5.6"} }

   Output:
    [{
        "name": "foo",
        "type": "bower",
        "version": "1.2.3"
    },{
        "name": "bar",
        "type": "bower",
        "version": "4.5.6"
    }]
 */
function componentFileToMetadatas(type, json) {
    var results = [],
        allDeps = _.merge(json.dependencies || {}, json.devDependencies || {});

    _.each(Object.keys(allDeps), function(dependency) {
        results.push({
            "name": dependency,
            "type": type,
            "version": allDeps[dependency]
        });
    });

    return results;
}

function readFile(grunt, file) {
    return grunt.file.exists(file) ? grunt.file.readJSON(file) : {};
}

function bowerCallback(dependency) {
    return function(callback) {
        bower.commands
            .info(dependency.name, "")
            .on("end", function(results) {
                var latest = results.versions[0];

                callback(null, _.merge({
                    latest : latest,
                    upToDate : semver.satisfies(latest, version)
                }, dependency));
            });
    };
}

function npmCallback(dependency) {
    return function(callback) {
        npm.commands.info([dependency.name, "version"], function(err, data) {
            // Data is structured as: { '1.2.1': { version: '1.2.1' } }
            // So get the first key of the object
            var latest = data[Object.keys(data)[0]].version;

            callback(null, _.merge({
                latest : latest,
                upToDate : semver.satisfies(latest, version)
            }, dependency));
        });
    };
}

module.exports = function(grunt) {

    grunt.registerTask("versioncheck", "Checks if your NPM or Bower dependencies are out of date.", function() {
        var done = this.async(),
            dependencyCalls = [];

        var options = this.options({
            skip : [],
            hideUpToDate: false,
            packageLocation: "package.json",
            bowerLocation: "bower.json"
        });
        
        var allDependencies = componentFileToMetadatas("bower", readFile(grunt, options.bowerLocation))
                      .concat(componentFileToMetadatas("npm",   readFile(grunt, options.packageLocation)));

        // Skip dependencies<commit_msg>Fixed issue introduced by code refactoring.<commit_after>/*
 * grunt-version-check
 * https://github.com/stevewillard/grunt-version-check
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

var bower = require("bower"),
    npm = require("npm"),
    _ = require("lodash"),
    semver = require("semver"),
    async = require("async");

require("colors");

/*
   Takes either a bower.json or package.json and returns some metadata about each object
   
   Input: (bower.json)
   { name: "my-bower-component", dependencies: {"foo" : "1.2.3"}, devDependencies: {"bar" : "4.5.6"} }

   Output:
    [{
        "name": "foo",
        "type": "bower",
        "version": "1.2.3"
    },{
        "name": "bar",
        "type": "bower",
        "version": "4.5.6"
    }]
 */
function componentFileToMetadatas(type, json) {
    var results = [],
        allDeps = _.merge(json.dependencies || {}, json.devDependencies || {});

    _.each(Object.keys(allDeps), function(dependency) {
        results.push({
            "name": dependency,
            "type": type,
            "version": allDeps[dependency]
        });
    });

    return results;
}

function readFile(grunt, file) {
    return grunt.file.exists(file) ? grunt.file.readJSON(file) : {};
}

function bowerCallback(dependency) {
    return function(callback) {
        bower.commands
            .info(dependency.name, "")
            .on("end", function(results) {
                var latest = results.versions[0];

                callback(null, _.merge({
                    latest : latest,
                    upToDate : semver.satisfies(latest, dependency.version)
                }, dependency));
            });
    };
}

function npmCallback(dependency) {
    return function(callback) {
        npm.commands.info([dependency.name, "version"], function(err, data) {
            // Data is structured as: { '1.2.1': { version: '1.2.1' } }
            // So get the first key of the object
            var latest = data[Object.keys(data)[0]].version;

            callback(null, _.merge({
                latest : latest,
                upToDate : semver.satisfies(latest, dependency.version)
            }, dependency));
        });
    };
}

module.exports = function(grunt) {

    grunt.registerTask("versioncheck", "Checks if your NPM or Bower dependencies are out of date.", function() {
        var done = this.async(),
            dependencyCalls = [];

        var options = this.options({
            skip : [],
            hideUpToDate: false,
            packageLocation: "package.json",
            bowerLocation: "bower.json"
        });
        
        var allDependencies = componentFileToMetadatas("bower", readFile(grunt, options.bowerLocation))
                      .concat(componentFileToMetadatas("npm",   readFile(grunt, options.packageLocation)));

        // Skip dependencies
{'fraction_non_alphanumeric': 0.14648232411620582, 'fraction_numerical': 0.00840042002100105, 'mean_word_length': 2.1246582832148717, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2947822', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1635, 'n_words': 356}