<commit_before>"use strict";
const fs = require('fs');
const account = require('./account');
const path = require('path');

const filepath = './baqend/schema/';

module.exports = function(args) {
  return account.login({app: args.app, host: args.host}).then((db) => {
    if (!fs.existsSync(filepath)){
      createDir(filepath)
    }
    switch(args.command) {
      case 'upload':
        uploadSchema(db, args).then(() => {
          console.log("---------------------------------------")
          console.log("The schema was successfully " + (args.force && "replaced" || "updated") )
        }, (err) => {
          console.log("An error occured")
        })
        break;
      case 'download':
        downloadSchema(db, args).then(() => {
          console.log("---------------------------------------")
          console.log("Your schema was successfully downloaded")
        }, (err) => {
          console.log("schema.json could not be saved. Did you create a baqend directory already?")
        })
        break;
    }
  })
}

function uploadSchema(db, args = {}) {
  let allSchemas = []
  let filepath = 'baqend/schema/'
  return readDirectory(filepath).then((fileNames) => {
    return Promise.all(
      fileNames.map((fileName) => {
        return readFile(path.join(filepath, fileName)).then((file) => {
          return JSON.parse(file)
        })
      })
    ).then((schemas) => {
      schemas.forEach((schema) => {
        console.log("Uploading " + schema.class.replace('/db/', '') + " Schema")
      })
      if (args.force) {
        return db.send(new db.message.ReplaceAllSchemas(schemas))
      } else {
        return db.send(new db.message.UpdateAllSchemas(schemas))
      }
    })
  })
}
module.exports.uploadSchema = uploadSchema;

function downloadSchema(db, args = {}) {
  return db.send(new db.message.GetAllSchemas()).then((res) => {
    return Promise.all(
      res.entity.map((schema) => {
        let classname = schema.class.replace('/db/', '')
        let filename = 'baqend/schema/' + classname + ".json"

        if (!classname.match(/logs\./) && classname !== 'Object') {
          return writeFile(filename, JSON.stringify(schema, null, 2)).then(() => {
            console.log('Downloaded ' + classname + ' Schema')
          })
        }
      })
    )
  });<commit_msg>removed default parameters to ensure node 4 compatibility<commit_after>"use strict";
const fs = require('fs');
const account = require('./account');
const path = require('path');

const filepath = './baqend/schema/';

module.exports = function(args) {
  return account.login({app: args.app, host: args.host}).then((db) => {
    if (!fs.existsSync(filepath)){
      createDir(filepath)
    }
    switch(args.command) {
      case 'upload':
        uploadSchema(db, args).then(() => {
          console.log("---------------------------------------")
          console.log("The schema was successfully " + (args.force && "replaced" || "updated") )
        }, (err) => {
          console.log("An error occured")
        })
        break;
      case 'download':
        downloadSchema(db, args).then(() => {
          console.log("---------------------------------------")
          console.log("Your schema was successfully downloaded")
        }, (err) => {
          console.log("schema.json could not be saved. Did you create a baqend directory already?")
        })
        break;
    }
  })
}

function uploadSchema(db, args) {
  args = args || {};
  let allSchemas = [];
  let filepath = 'baqend/schema/';
  return readDirectory(filepath).then((fileNames) => {
    return Promise.all(
      fileNames.map((fileName) => {
        return readFile(path.join(filepath, fileName)).then((file) => {
          return JSON.parse(file)
        })
      })
    ).then((schemas) => {
      schemas.forEach((schema) => {
        console.log("Uploading " + schema.class.replace('/db/', '') + " Schema")
      })
      if (args.force) {
        return db.send(new db.message.ReplaceAllSchemas(schemas))
      } else {
        return db.send(new db.message.UpdateAllSchemas(schemas))
      }
    })
  })
}
module.exports.uploadSchema = uploadSchema;

function downloadSchema(db) {
  return db.send(new db.message.GetAllSchemas()).then((res) => {
    return Promise.all(
      res.entity.map((schema) => {
        let classname = schema.class.replace('/db/', '')
        let filename = 'baqend/schema/' + classname + ".json"

        if (!classname.match(/logs\./) && classname !== 'Object') {
          return writeFile(filename, JSON.stringify(schema, null, 2)).then(() => {
            console.log('Downloaded ' + classname + ' Schema')
          })
        }
      })
    )
  });
{'fraction_non_alphanumeric': 0.19940285775218597, 'fraction_numerical': 0.0006397952655150352, 'mean_word_length': 2.186141304347826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2585341', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1356, 'n_words': 266}