src/index.js
function readModels (dir, relative) {
  const fs = require('fs')
  const path = require('path')
  // eslint-disable-next-line no-eval
  const evalRequire = eval('require')
  const list = []
  function readRecursively (dir, root, list) {
    fs.readdirSync(dir).forEach(file => {
      const filePath = path.join(dir, file)
      if (fs.statSync(filePath).isDirectory()) {
        // walk through
        readRecursively(filePath, root, list)
      } else {
        // only '*.js' file will be added as model
        if (file.match(/^(\.\/)?([\w]+\/)*([\w]+\.js)$/)) {
          // bundler like webpack will complain about using dynamic require
          // See https://github.com/webpack/webpack/issues/196
          // use `eval` trick to avoid this
          let model = evalRequire(filePath)
          // handling es6 module
          if (model.__esModule) {
            model = model.default
          }
          // get relative path of the root path
          const pathNS = path.join(path.relative(root, dir), file)
          list.push(checkNS(model) ? model : {namespace: getNamespace(pathNS, relative), ...model})
        }
      }
    })
  }
  const p = path.resolve(__dirname, dir)
  readRecursively(p, p, list)
  return list
}
function readModels (dir, relative) {
  const fs = require('fs')
  const path = require('path')
  // eslint-disable-next-line no-eval
  const evalRequire = eval('require')
  const list = []
  function readRecursively (dir, root, list) {
    fs.readdirSync(dir).forEach(file => {
      const filePath = path.join(dir, file)
      if (fs.statSync(filePath).isDirectory()) {
        // walk through
        readRecursively(filePath, root, list)
      } else {
        // only '*.js' file will be added as model
        if (file.match(/^(\.\/)?([\w]+\/)*([\w]+\.js)$/)) {
          // bundler like webpack will complain about using dynamic require
          // See https://github.com/webpack/webpack/issues/196
          // use `eval` trick to avoid this
          let model = evalRequire(filePath)
          // handling es6 module
          if (model.__esModule) {
            model = model.default
          }
          // get relative path of the root path
          const pathNS = path.join(path.relative(root, dir), file)
          list.push(checkNS(model) ? model : {namespace: getNamespace(pathNS, relative), ...model})
        }
      }
    })
  }
  const p = path.resolve(__dirname, dir)
  readRecursively(p, p, list)
  return list
}
function readModels ( dir , relative ) { const fs = require ( 'fs' ) const path = require ( 'path' ) // eslint-disable-next-line no-eval const evalRequire = eval ( 'require' ) const list = [ ] function readRecursively ( dir , root , list ) { fs . readdirSync ( dir ) . forEach ( file => { const filePath = path . join ( dir , file ) if ( fs . statSync ( filePath ) . isDirectory ( ) ) { // walk through readRecursively ( filePath , root , list ) } else { // only '*.js' file will be added as model if ( file . match ( / ^(\.\/)?([\w]+\/)*([\w]+\.js)$ / ) ) { // bundler like webpack will complain about using dynamic require // See https://github.com/webpack/webpack/issues/196 // use `eval` trick to avoid this let model = evalRequire ( filePath ) // handling es6 module if ( model . __esModule ) { model = model . default } // get relative path of the root path const pathNS = path . join ( path . relative ( root , dir ) , file ) list . push ( checkNS ( model ) ? model : { namespace : getNamespace ( pathNS , relative ) , ... model } ) } } } ) } const p = path . resolve ( __dirname , dir ) readRecursively ( p , p , list ) return list }
Read all models recursively in a path. This just works in Node project

@param {String} dir
@param {String} root
@param {Array} list
Read all models recursively in a path . This just works in Node project