<commit_before>var
  fs = require('fs'),
  RequestObject = require('./core/models/requestObject.js'),
  InternalError = require('./core/errors/internalError'),
  async = require('async'),
  q = require('q'),
  rc = require('rc'),
  env = {
    fixtures: 'FIXTURES',
    mappings: 'DEFAULT_MAPPING'
  };

module.exports = function (kuzzle) {
  this.kuzzle = kuzzle;
  this.data = {};
  this.params = rc('kuzzle');

  this.defaultRoleDefinition = this.params.roleWithoutAdmin;

  if (kuzzle.isServer) {
    kuzzle.pluginsManager.trigger('log:info', '== Starting DB preparation...');
    return createInternalStructure.call(this)
      .then(() => {
        return readFile.call(this, 'mappings');
      })
      .then(() => {
        return readFile.call(this, 'fixtures');
      })
      .then(() => {
        return createIndexes.call(this);
      })
      .then(() => {
        return importMapping.call(this);
      })
      .then(() => {
        return importFixtures.call(this);
      })
      .then(() => {
        kuzzle.pluginsManager.trigger('log:info', '== DB preparation done.');
      });
  }

  return q();
};

function createInternalStructure () {
  var requestObject = new RequestObject({controller: 'admin', action: 'createIndex', index: this.kuzzle.config.internalIndex});

  if (this.kuzzle.indexCache.indexes[this.kuzzle.config.internalIndex]) {
    return q();
  }

  this.kuzzle.pluginsManager.trigger('log:info', '== Creating Kuzzle internal index...');

  this.kuzzle.pluginsManager.trigger('data:createIndex', requestObject);

  return this.kuzzle.workerListener.add(requestObject)
    .then(() => {
      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex);

      this.kuzzle.pluginsManager.trigger('log:info', '== Creating profiles collection...');

      requestObject = new RequestObject({
        controller: 'admin',
        action: 'updateMapping',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',
        body: {
          properties: {
            roles: {
              index: 'not_analyzed',
              type: 'string'
            }
          }
        }
      });

      this.kuzzle.pluginsManager.trigger('data:updateMapping', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex, 'profiles');
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating users collection...');

      requestObject = new RequestObject({
        controller: 'admin',
        action: 'updateMapping',
        index: this.kuzzle.config.internalIndex,
        collection: 'users',
        body: {
          properties: {
            profile: {
              index: 'not_analyzed',
              type: 'string'
            },
            password: {
              index: 'no',
              type: 'string'
            }
          }
        }
      });

      this.kuzzle.pluginsManager.trigger('data:updateMapping', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex, 'users');
    })
    .then(() => {
      var requestObject;

      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default role for anonymous...');

      this.defaultRoleDefinition._id = 'anonymous';

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'roles',
        body: this.defaultRoleDefinition
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      var requestObject;

      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default role for default...');

      this.defaultRoleDefinition._id = 'default';

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'roles',
        body: this.defaultRoleDefinition
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      var requestObject;

      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex, 'roles');
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default role for admin...');

      this.defaultRoleDefinition._id = 'admin';

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'roles',
        body: this.defaultRoleDefinition
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })

    .then(() => {
      var requestObject;

      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default profile for default...');

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',
        body: {_id: 'default', roles: [ 'default' ]}
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      var requestObject;

      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default profile for anonymous...');

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',
        body: {_id: 'anonymous', roles: [ 'anonymous' ]}
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      var requestObject;
      
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default profile for admin...');

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',<commit_msg>cleanup & linting<commit_after>var
  fs = require('fs'),
  RequestObject = require('./core/models/requestObject.js'),
  InternalError = require('./core/errors/internalError'),
  async = require('async'),
  q = require('q'),
  rc = require('rc'),
  env = {
    fixtures: 'FIXTURES',
    mappings: 'DEFAULT_MAPPING'
  };

module.exports = function (kuzzle) {
  this.kuzzle = kuzzle;
  this.data = {};
  this.params = rc('kuzzle');

  this.defaultRoleDefinition = this.params.roleWithoutAdmin;

  if (kuzzle.isServer) {
    kuzzle.pluginsManager.trigger('log:info', '== Starting DB preparation...');
    return createInternalStructure.call(this)
      .then(() => {
        return readFile.call(this, 'mappings');
      })
      .then(() => {
        return readFile.call(this, 'fixtures');
      })
      .then(() => {
        return createIndexes.call(this);
      })
      .then(() => {
        return importMapping.call(this);
      })
      .then(() => {
        return importFixtures.call(this);
      })
      .then(() => {
        kuzzle.pluginsManager.trigger('log:info', '== DB preparation done.');
      });
  }

  return q();
};

function createInternalStructure () {
  var requestObject = new RequestObject({controller: 'admin', action: 'createIndex', index: this.kuzzle.config.internalIndex});

  if (this.kuzzle.indexCache.indexes[this.kuzzle.config.internalIndex]) {
    return q();
  }

  this.kuzzle.pluginsManager.trigger('log:info', '== Creating Kuzzle internal index...');

  this.kuzzle.pluginsManager.trigger('data:createIndex', requestObject);

  return this.kuzzle.workerListener.add(requestObject)
    .then(() => {
      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex);

      this.kuzzle.pluginsManager.trigger('log:info', '== Creating profiles collection...');

      requestObject = new RequestObject({
        controller: 'admin',
        action: 'updateMapping',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',
        body: {
          properties: {
            roles: {
              index: 'not_analyzed',
              type: 'string'
            }
          }
        }
      });

      this.kuzzle.pluginsManager.trigger('data:updateMapping', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex, 'profiles');
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating users collection...');

      requestObject = new RequestObject({
        controller: 'admin',
        action: 'updateMapping',
        index: this.kuzzle.config.internalIndex,
        collection: 'users',
        body: {
          properties: {
            profile: {
              index: 'not_analyzed',
              type: 'string'
            },
            password: {
              index: 'no',
              type: 'string'
            }
          }
        }
      });

      this.kuzzle.pluginsManager.trigger('data:updateMapping', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex, 'users');
    })
    .then(() => {
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default role for anonymous...');

      this.defaultRoleDefinition._id = 'anonymous';

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'roles',
        body: this.defaultRoleDefinition
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default role for default...');

      this.defaultRoleDefinition._id = 'default';

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'roles',
        body: this.defaultRoleDefinition
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      this.kuzzle.indexCache.add(this.kuzzle.config.internalIndex, 'roles');
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default role for admin...');

      this.defaultRoleDefinition._id = 'admin';

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'roles',
        body: this.defaultRoleDefinition
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })

    .then(() => {
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default profile for default...');

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',
        body: {_id: 'default', roles: [ 'default' ]}
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default profile for anonymous...');

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',
        body: {_id: 'anonymous', roles: [ 'anonymous' ]}
      });

      this.kuzzle.pluginsManager.trigger('data:createOrReplace', requestObject);
      return this.kuzzle.workerListener.add(requestObject);
    })
    .then(() => {
      var requestObject;
      
      this.kuzzle.pluginsManager.trigger('log:info', '== Creating default profile for admin...');

      requestObject = new RequestObject({
        controller: 'write',
        action: 'createOrReplace',
        index: this.kuzzle.config.internalIndex,
        collection: 'profiles',
{'fraction_non_alphanumeric': 0.14514608859566447, 'fraction_numerical': 0.0, 'mean_word_length': 2.669452449567723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '4078898', 'n_tokens_mistral': 3694, 'n_tokens_neox': 3512, 'n_words': 616}