<commit_before>'use strict'

const fs = require('fs')
const yaml = require('js-yaml')
const request = require('request')
const parser = require('./response-contract-results')
const url = require('./jackal-url')

const exitOnMissingContract = (contractsPath, skipMissingContractFlag) => {
  return skipMissingContractFlag && !fs.existsSync(contractsPath)
}

module.exports = (jackalUrl, contractsPath, options, done) => {
  const jackal = url(jackalUrl, '/api/contracts')

  if(exitOnMissingContract(contractsPath, options.skipMissingContract)){
    /* eslint-disable no-console  */
    console.log(`Skipping no contracts, file not found: ${contractsPath}`)
    /* eslint-enble no-console  */
    return done()
  }

  if(!fs.existsSync(contractsPath)){
    return done(`Missing contract file ${contractsPath}`)
  }

  const fileBuffer = fs.readFileSync(contractsPath)

  let bodyBuffer
  if (contractsPath.endsWith('json')) {
    bodyBuffer = fileBuffer
  } else {
    const contracts = yaml.safeLoad(fileBuffer.toString())
    const json = JSON.stringify(contracts)
    bodyBuffer = Buffer.from(json)
  }

  const req = {
    url: jackal,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: bodyBuffer
  }

  request(req, parser(done))
}<commit_msg>Updated send to return error and response when contracts file not found<commit_after>'use strict'

const fs = require('fs')
const yaml = require('js-yaml')
const request = require('request')
const parser = require('./response-contract-results')
const url = require('./jackal-url')

const exitOnMissingContract = (contractsPath, skipMissingContractFlag) => {
  return skipMissingContractFlag && !fs.existsSync(contractsPath)
}

module.exports = (jackalUrl, contractsPath, options, done) => {
  const jackal = url(jackalUrl, '/api/contracts')

  if(exitOnMissingContract(contractsPath, options.skipMissingContract)){
    return done(null, `Skipping no contracts, file not found: ${contractsPath}`)
  }

  if (!fs.existsSync(contractsPath)) {
    return done(`Missing contract file ${contractsPath}`)
  }

  const fileBuffer = fs.readFileSync(contractsPath)

  let bodyBuffer
  if (contractsPath.endsWith('json')) {
    bodyBuffer = fileBuffer
  } else {
    const contracts = yaml.safeLoad(fileBuffer.toString())
    const json = JSON.stringify(contracts)
    bodyBuffer = Buffer.from(json)
  }

  const req = {
    url: jackal,
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: bodyBuffer
  }

  request(req, parser(done))
}
{'fraction_non_alphanumeric': 0.13318112633181126, 'fraction_numerical': 0.0, 'mean_word_length': 4.226640159045726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6835840', 'n_tokens_mistral': 803, 'n_tokens_neox': 787, 'n_words': 182}