<commit_before>var gutil = require('gulp-util');
var through = require('through2');
var semver = require('semver');

//test if a type is a valid semver type
var isValidType = function(type) {
    return semver.inc('0.0.1', type);
};

//test if a version is a valid semver version
var isValidVersion = function(version, type) {
    return semver.valid(version);
};

var setDefaultOptions = function(opts) {
    opts = opts || {};
    //set defaults
    opts.key = opts.key || 'version';
    opts.indent = opts.indent || 2;
    //default type bump is patch
    if (!opts.type || !isValidType(opts.type)) {
        opts.type = 'patch';
    }
    //if passed specific version - validate it
    if (opts.version && !isValidVersion(opts.version, opts.type)) {
        gutil.log('invalid version used as option', opts.version);
        opts.version = null;
    }
    return opts;
};

module.exports = function(opts) {
    //set task options
    opts = setDefaultOptions(opts);
    var key = opts.key;
    var version = opts.version;
    var indent = opts.indent;
    var type = opts.type;

    return through.obj(function(file, enc, cb) {
        //pass through empty file
        if (file.isNull()) {
            this.push(file);
            return cb();
        }

        if (file.isStream()) {
            this.emit('error', new gutil.PluginError('gulp-bump', 'Streaming not supported'));
            return cb();
        }

        var json;
        try {
            json = JSON.parse(file.contents.toString());
        } catch (e) {
            this.emit('error', new gutil.PluginError('gulp-bump', 'Problem parsing JSON file ' + file.path));
            return cb();
        }

        //just set a version to the key
        if (version) {
            if (!json[key]) {
                //log to user that key didn't exist before
                gutil.log('Creating key', gutil.colors.red(key), 'with version:', version);
            }
            //set version to key
            json[key] = version;
        } else if (semver.valid(json[key])) {
            //increment the key with type
            json[key] = semver.inc(json[key], type);
        } else {
            this.emit('error', new gutil.PluginError('gulp-bump', 'Detected invalid semver ' + key + ' in file ' + file.path));
            return cb();
        }

        //rebuild file contents
        file.contents = new Buffer(JSON.stringify(json, null, indent) + '\n');
        //log new version
        gutil.log('Bumped ' + gutil.colors.underline(key) + ' to: ' + gutil.colors.cyan(json[key]));
        this.push(file);
        return cb();
    });
};<commit_msg>Clean code style<commit_after>var gutil = require('gulp-util');
var through = require('through2');
var semver = require('semver');

var setDefaultOptions = function(opts) {
  opts = opts || {};
  opts.key = opts.key || 'version';
  opts.indent = opts.indent || 2;
  // default type bump is patch
  if (!opts.type || !semver.inc('0.0.1', opts.type)) {
    opts.type = 'patch';
  }
  // if passed specific version - validate it
  if (opts.version && !semver.valid(opts.version, opts.type)) {
    gutil.log('invalid version used as option', gutil.colors.red(opts.version));
    opts.version = null;
  }
  return opts;
};

module.exports = function(opts) {
  // set task options
  opts = setDefaultOptions(opts);
  var key = opts.key;
  var version = opts.version;
  var indent = opts.indent;
  var type = opts.type;

  var json;

  return through.obj(function(file, enc, cb) {
    if (file.isNull()) {
      this.push(file);
      return cb();
    }
    if (file.isStream()) {
      this.emit('error', new gutil.PluginError('gulp-bump', 'Streaming not supported'));
      return cb();
    }
    
    try {
      json = JSON.parse(file.contents.toString());
    } catch (e) {
      this.emit('error', new gutil.PluginError('gulp-bump', 'Problem parsing JSON file ' + file.path));
      return cb();
    }

    // just set a version to the key
    if (version) {
      if (!json[key]) {
        // log to user that key didn't exist before
        gutil.log('Creating key', gutil.colors.red(key), 'with version:', gutil.colors.cyan(version));
      }
      json[key] = version;
    }
    else if (semver.valid(json[key])) {
    // increment the key with type
      json[key] = semver.inc(json[key], type);
    }
    else {
      this.emit('error', new gutil.PluginError('gulp-bump', 'Detected invalid semver ' + key + ' in file ' + file.path));
      return cb();
    }

    file.contents = new Buffer(JSON.stringify(json, null, indent) + '\n');
    
    gutil.log('Bumped ' + gutil.colors.magenta(key) + ' to: ' + gutil.colors.cyan(json[key]));
    this.push(file);
    return cb();
  });
};
{'fraction_non_alphanumeric': 0.1532753868984524, 'fraction_numerical': 0.00211999152003392, 'mean_word_length': 2.343727852586818, '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': '1382989', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1464, 'n_words': 387}