<commit_before>path = require 'path'
async = require 'async'
restapi = require './restapi'
manifest = require './manifest'
_ = require 'underscore'
semver = require 'semver'

versionExists = (manifest, gadgets) ->
  otherGadgetVersions = _.select gadgets, (gadget) ->
    manifest.name == gadget.name
  return _.any otherGadgetVersions, (gadget) ->
    return semver.gte gadget.version, manifest.version

# TODO function and supporting functions are a temporary measure
# until rest-api#1693 is resolved
module.exports =

  checkProject: (dir, options, callback) ->
    async.concat ['approved', 'sandbox'], (catalog, cb) ->
      restapi.getGadgetCatalog catalog, options, cb
    , (err, gadgets) ->

      manifest.readManifest dir, (err, manifestInfo) ->
        if err then return callback err

        if versionExists manifestInfo, gadgets
          manifest.lookupManifest dir, (manifestPath) ->
            errorMessage = "Version 'v#{manifestInfo.version}' or greater " +
            "already exists. Bump the version in '#{path.basename manifestPath}' " +
            "before uploading."
            return callback(new Error(errorMessage))

        else
          return callback()<commit_msg>Break validator into smaller fns<commit_after>path = require 'path'
async = require 'async'
restapi = require './restapi'
manifest = require './manifest'
_ = require 'underscore'
semver = require 'semver'

versionExists = (manifestInfo, gadgets) ->
  otherGadgetVersions = _.select gadgets, (gadget) ->
    manifestInfo.name == gadget.name
  return _.any otherGadgetVersions, (gadget) ->
    return semver.gte gadget.version, manifestInfo.version

getAllGadgetVersions = (options, callback) ->
  async.concat ['approved', 'sandbox'], (catalog, cb) ->
    restapi.getGadgetCatalog catalog, options, cb
  , callback

# TODO function and supporting functions are a temporary measure
# until rest-api#1693 is resolved
assertValidVersion = (dir, options, callback) ->
  getAllGadgetVersions options, (err, gadgets) ->
    manifest.readManifest dir, (err, manifestInfo) ->
      if err then return callback err

      if versionExists manifestInfo, gadgets
        manifest.lookupManifest dir, (manifestPath) ->
          errorMessage = "Version 'v#{manifestInfo.version}' or greater already exists." +
          "\n       Bump the version in '#{path.basename manifestPath}' before uploading."
          return callback(new Error(errorMessage))

      else
        return callback()

module.exports =
  checkProject: (dir, options, callback) ->
    assertValidVersion dir, options, callback
{'fraction_non_alphanumeric': 0.10664605873261206, 'fraction_numerical': 0.0030911901081916537, 'mean_word_length': 3.4947916666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '1265055', 'n_tokens_mistral': 735, 'n_tokens_neox': 717, 'n_words': 220}