<commit_before>'use strict'

const fs        = require('fs')
  ,   path      = require('path')
  ,   _         = require('lodash')
  ,   objHash   = require('object-hash')
  ,   case      = require('snake-case')
  ,   Promise   = require('bluebird')
  ,   Waterline = require('waterline')
  ,   peer      = require('codependency').register(module)
  ,   readdir   = Promise.promisify(fs.readdir)

const defaultConfig = {
    dir: null,
    target: global,
    adapters: {},
    connections: {}
}

const adapters = [
    'postgresql',
    'mysql',
    'mongo',
    'memory',
    'disk',
    'sqlserver',
    'redis',
    'riak',
    'irc',
    'twitter',
    'jsdom',
    'neo4j',
    'orientdb',
    'arangodb',
    'cassandra',
    'graphql',
    'solr'
]
.forEach((name) => {
    let adapter = peer('sails-' + name, { optional: true })
    if (adapter != null)
        defaultConfig.adapters[name] = adapter
})

const ormCache = {}

const buildConfig = function(_config) {
    if (_.isString(_config))
        _config = { dir: _config }

    if (!_config.dir) {
        throw TypeError('First argument must be the path to the models directory or an object with a dir attribute')

    let config = _.assign({}, defaultConfig, _config)

    if (config.connection) {
        if (config.connections.default)
            throw Error('connections.default and connection should not be both defined')
        config.connections.default = config.connection
        config.connection = undefined
    }

    if (_.isEmpty(config.connections)) {
        config.connections.default = { adapter: 'memory' }
    }

    _.values(config.connections).each((c) => {
        if (!c.adapter)
            throw TypeError('connection must contain an adapter field')
        if (!defaultConfig.adapters[c.adapter])
            throw Error('Default sails-memory adapter could\'t be loaded. Run "npm i sails-' + c.adapter + '"')
    })

    // config.orm is what will be passed to Waterline#initialize
    // so keep it clean
    config.orm = _.omit(config, [ 'dir', 'target', 'connection' ])

    return config
}

const WaterlineLighter = function(_config, cb) {

    let hash = objHash(_config)
    let cached = ormCache[hash]

    if (cached)
        return Promise.resolve(cached)

    let config = null;

    try {
        config = buildConfig(_config)
    } catch(err) {
        return Promise.reject(err)
    }

    let orm = new Waterline()
    let ormInitialize = Promise.promisify(orm.initialize, { context: orm })
    let dir = path.join(path.dirname(module.parent.filename), config.dir)

    return readdir(dir)
    .then((files) => {
        let p = files
        .filter((f) => (/\.js$/.test(f)))
        .map((f) => {
            let file = path.join(dir, f)
            let name = path.basename(file, '.js')
            let model = require(file)
            if (!model.connection)
                model.connection = 'default'
            if (!model.identity)
                model.identity = case(name)
            return model
        })
        .map((m) => (Waterline.Collection.extend(m)))
        .map((m) => (orm.loadCollection(m)))

        return Promise.all(p)
    })
    .then(() => (ormInitialize(config.orm)))
    .then((models) => {
        let result = _.assign({ orm, config }, models)

        if (config.target !== false) // Inject in the target if available
            _.assign(config.target, _.pick(result, [ 'models', 'connections' ]))

        ormCache[hash] = result

        if (_.isFunction(cb))
            return cb(null, result)
        else
            return result
    })
    .catch((error) => {
        if (_.isFunction(cb))
            return cb(error)<commit_msg>Fix compile error... Of course case is a reserved word... -.-''<commit_after>'use strict'

const fs        = require('fs')
  ,   path      = require('path')
  ,   _         = require('lodash')
  ,   objHash   = require('object-hash')
  ,   sCase     = require('snake-case')
  ,   Promise   = require('bluebird')
  ,   Waterline = require('waterline')
  ,   peer      = require('codependency').register(module)
  ,   readdir   = Promise.promisify(fs.readdir)

const defaultConfig = {
    dir: null,
    target: global,
    adapters: {},
    connections: {}
}

const adapters = [
    'postgresql',
    'mysql',
    'mongo',
    'memory',
    'disk',
    'sqlserver',
    'redis',
    'riak',
    'irc',
    'twitter',
    'jsdom',
    'neo4j',
    'orientdb',
    'arangodb',
    'cassandra',
    'graphql',
    'solr'
]
.forEach((name) => {
    let adapter = peer('sails-' + name, { optional: true })
    if (adapter != null)
        defaultConfig.adapters[name] = adapter
})

const ormCache = {}

const buildConfig = function(_config) {
    if (_.isString(_config))
        _config = { dir: _config }

    if (!_config.dir) {
        throw TypeError('First argument must be the path to the models directory or an object with a dir attribute')

    let config = _.assign({}, defaultConfig, _config)

    if (config.connection) {
        if (config.connections.default)
            throw Error('connections.default and connection should not be both defined')
        config.connections.default = config.connection
        config.connection = undefined
    }

    if (_.isEmpty(config.connections)) {
        config.connections.default = { adapter: 'memory' }
    }

    _.values(config.connections).each((c) => {
        if (!c.adapter)
            throw TypeError('connection must contain an adapter field')
        if (!defaultConfig.adapters[c.adapter])
            throw Error('Default sails-memory adapter could\'t be loaded. Run "npm i sails-' + c.adapter + '"')
    })

    // config.orm is what will be passed to Waterline#initialize
    // so keep it clean
    config.orm = _.omit(config, [ 'dir', 'target', 'connection' ])

    return config
}

const WaterlineLighter = function(_config, cb) {

    let hash = objHash(_config)
    let cached = ormCache[hash]

    if (cached)
        return Promise.resolve(cached)

    let config = null;

    try {
        config = buildConfig(_config)
    } catch(err) {
        return Promise.reject(err)
    }

    let orm = new Waterline()
    let ormInitialize = Promise.promisify(orm.initialize, { context: orm })
    let dir = path.join(path.dirname(module.parent.filename), config.dir)

    return readdir(dir)
    .then((files) => {
        let p = files
        .filter((f) => (/\.js$/.test(f)))
        .map((f) => {
            let file = path.join(dir, f)
            let name = path.basename(file, '.js')
            let model = require(file)
            if (!model.connection)
                model.connection = 'default'
            if (!model.identity)
                model.identity = sCase(name)
            return model
        })
        .map((m) => (Waterline.Collection.extend(m)))
        .map((m) => (orm.loadCollection(m)))

        return Promise.all(p)
    })
    .then(() => (ormInitialize(config.orm)))
    .then((models) => {
        let result = _.assign({ orm, config }, models)

        if (config.target !== false) // Inject in the target if available
            _.assign(config.target, _.pick(result, [ 'models', 'connections' ]))

        ormCache[hash] = result

        if (_.isFunction(cb))
            return cb(null, result)
        else
            return result
    })
    .catch((error) => {
        if (_.isFunction(cb))
            return cb(error)
{'fraction_non_alphanumeric': 0.1423185673892554, 'fraction_numerical': 0.00026928773394371884, 'mean_word_length': 2.203104786545925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1802539', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2213, 'n_words': 545}