<commit_before>/*
 * grunt-dev-update
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

var async = require('async'),
    inquirer = require('inquirer'),
    _ = require('lodash'),
    findup = require('findup-sync');

module.exports = function (grunt) {

    var exports = {
        options: {},
        devDeps: [],
        prodDeps: [],
        results: {}
    };

    //default spawn options
    var spawnOptions = {
        cmd: 'npm',
        grunt: false,
        opts: {}
    };

    exports.stats = {
        outdated: 0,
        upToDate: 0
    };

    /**
     * Get the dev dependencies packages to update from package.json
     */
    var getPackageNames = function (packages) {
        //how is package.json located
        if (exports.options.packageJson) {
            grunt.verbose.writelns('Using custom option for package.json: ' + exports.options.packageJson);
        } else {
            exports.options.packageJson = findup('package.json', {
                cwd: process.cwd()
            });
        }

        try {
            //load package json
            var pkg = require(exports.options.packageJson);
        } catch (e) {
            //couldn't get packages... critical error
            grunt.verbose.writelns('Error ', e);
            grunt.fail.fatal('Could not read from package.json', exports.options.packageJson);
        }

        var _packages = _.map(packages, function (dep) {
            dep.deps = pkg[dep.type];
            grunt.log.writeln('Found ' + _.keys(dep.deps).length + ' ' + dep.type.blue + ' to check for latest version');
            return _.map(dep.deps, function (item, key) {
                return {
                    name: key,
                    installType: dep.installType,
                    type: dep.type
                };
            });
        });

        return _.flatten(_packages);
    };

    /**
     * Get the spawn arguments for the action
     * @param {String} dependency
     * @param {String} phase
     * @param {String} saveType should be either --save or --save-dev
     */
    var getSpawnArguments = function (dependency, phase, saveType) {
        switch (phase) {
        case 'outdated':
            return ['outdated', '--json', '--depth=0'];
            //arguments to spawn to get local package version
        case 'update':
            return ['update', dependency];
        case 'install':
            //this will force the version to install to override locks in package.json
            return ['install', dependency + '@latest', saveType];
            //no action detected
        default:
            return [];
        }
    };

    var getOutdatedPkgs = function (packages, done) {
        var pkgNames = _.pluck(packages, 'name');
        spawnOptions.args = getSpawnArguments(null, 'outdated').concat(pkgNames);
        grunt.util.spawn(spawnOptions, function (error, result) {
            if (error) {
                grunt.verbose.writelns(error);
                grunt.fail.fatal('Task failed due to ', error);
                return done(error);
            }
            var jsonResults;
            try {
                jsonResults = JSON.parse(result.stdout);
            } catch (e) {
                grunt.fail.fatal('Task failed with JSON.parse due to ', e);
            }
            return done(null, jsonResults);
        });
    };

    var processByUpdateType = function (pkg, specs, done) {
        /** Update phase **/
        grunt.log.subhead('Package:%s, Package type: %s, Current version: %s, Wanted: %s, Latest: %s',
            pkg.name, specs.current, specs.wanted, specs.latest);

        //only report outdated, do nothing
        if (exports.options.updateType === 'report') {
            return done();
        }
        var updateType = exports.options.semver ? 'update' : 'install';
        var spawnArgs = getSpawnArguments(pkg.name, updateType, pkg.installType);

        //prompt user if package should be updated
        if (exports.options.updateType === 'prompt') {
            //prompt to update
            var msg = 'update using [npm ' + spawnArgs.join(' ') + ']';
            return inquirer.prompt({
                name: 'confirm',
                message: msg,
                default: false,
                type: 'confirm'<commit_msg>fixed update phase string output; made it a bit more readable imo<commit_after>/*
 * grunt-dev-update
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

var async = require('async'),
    inquirer = require('inquirer'),
    _ = require('lodash'),
    findup = require('findup-sync');

module.exports = function (grunt) {

    var exports = {
        options: {},
        devDeps: [],
        prodDeps: [],
        results: {}
    };

    //default spawn options
    var spawnOptions = {
        cmd: 'npm',
        grunt: false,
        opts: {}
    };

    exports.stats = {
        outdated: 0,
        upToDate: 0
    };

    /**
     * Get the dev dependencies packages to update from package.json
     */
    var getPackageNames = function (packages) {
        //how is package.json located
        if (exports.options.packageJson) {
            grunt.verbose.writelns('Using custom option for package.json: ' + exports.options.packageJson);
        } else {
            exports.options.packageJson = findup('package.json', {
                cwd: process.cwd()
            });
        }

        try {
            //load package json
            var pkg = require(exports.options.packageJson);
        } catch (e) {
            //couldn't get packages... critical error
            grunt.verbose.writelns('Error ', e);
            grunt.fail.fatal('Could not read from package.json', exports.options.packageJson);
        }

        var _packages = _.map(packages, function (dep) {
            dep.deps = pkg[dep.type];
            grunt.log.writeln('Found ' + _.keys(dep.deps).length + ' ' + dep.type.blue + ' to check for latest version');
            return _.map(dep.deps, function (item, key) {
                return {
                    name: key,
                    installType: dep.installType,
                    type: dep.type
                };
            });
        });

        return _.flatten(_packages);
    };

    /**
     * Get the spawn arguments for the action
     * @param {String} dependency
     * @param {String} phase
     * @param {String} saveType should be either --save or --save-dev
     */
    var getSpawnArguments = function (dependency, phase, saveType) {
        switch (phase) {
        case 'outdated':
            return ['outdated', '--json', '--depth=0'];
            //arguments to spawn to get local package version
        case 'update':
            return ['update', dependency];
        case 'install':
            //this will force the version to install to override locks in package.json
            return ['install', dependency + '@latest', saveType];
            //no action detected
        default:
            return [];
        }
    };

    var getOutdatedPkgs = function (packages, done) {
        var pkgNames = _.pluck(packages, 'name');
        spawnOptions.args = getSpawnArguments(null, 'outdated').concat(pkgNames);
        grunt.util.spawn(spawnOptions, function (error, result) {
            if (error) {
                grunt.verbose.writelns(error);
                grunt.fail.fatal('Task failed due to ', error);
                return done(error);
            }
            var jsonResults;
            try {
                jsonResults = JSON.parse(result.stdout);
            } catch (e) {
                grunt.fail.fatal('Task failed with JSON.parse due to ', e);
            }
            return done(null, jsonResults);
        });
    };

    var processByUpdateType = function (pkg, specs, done) {
        /** Update phase **/
        grunt.log.subhead('Package: %s\n  Package type: %s\n  Current version: %s\n  Wanted: %s\n  Latest: %s',
            pkg.name, pkg.type, specs.current, specs.wanted, specs.latest);

        //only report outdated, do nothing
        if (exports.options.updateType === 'report') {
            return done();
        }
        var updateType = exports.options.semver ? 'update' : 'install';
        var spawnArgs = getSpawnArguments(pkg.name, updateType, pkg.installType);

        //prompt user if package should be updated
        if (exports.options.updateType === 'prompt') {
            //prompt to update
            var msg = 'update using [npm ' + spawnArgs.join(' ') + ']';
            return inquirer.prompt({
                name: 'confirm',
                message: msg,
                default: false,
                type: 'confirm'
{'fraction_non_alphanumeric': 0.12306101344364012, 'fraction_numerical': 0.0016086406986096748, 'mean_word_length': 1.7106820305200872, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4297967', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2318, 'n_words': 636}