1+ /* eslint global-require: 0 */
2+
13'use strict'
24
35const kebabCase = require ( 'lodash.kebabcase' )
4- const fs = require ( 'fs' )
56const path = require ( 'path' )
67const debug = require ( 'debug' ) ( 'loopback:component:model-extender' )
78const appRoot = require ( 'app-root-path' )
9+ const requireAll = require ( 'require-all' )
810
911module . exports = ( app , options ) => {
1012 options = options || { }
@@ -23,27 +25,24 @@ module.exports = (app, options) => {
2325
2426 // Set up logging class.
2527 let log = options . log || console
28+
2629 if ( typeof log === 'string' ) {
2730 log = require ( log )
2831 }
2932
3033 // Apply models customisations.
31- function customizeModel ( sourcePath ) {
32- fs . readdirSync ( sourcePath ) . forEach ( sourceFile => {
33- const filePath = path . join ( sourcePath , sourceFile )
34-
35- if ( path . extname ( sourceFile ) === '.js' ) {
36- debug ( 'Loading customization script %s' , filePath )
37- const code = require ( filePath )
38-
34+ function customizeModel ( dirname ) {
35+ requireAll ( {
36+ dirname,
37+ resolve : code => {
38+ debug ( 'Loading customization script %s' , code )
3939 if ( typeof code === 'function' ) {
4040 debug ( 'Customizing model %s' , Model . modelName )
41- code ( Model )
42- }
43- else {
44- debug ( 'Skipping model file %s - `module.exports` is not a function' , sourceFile )
41+ return code ( Model )
4542 }
46- }
43+ debug ( 'Skipping model file %s - `module.exports` is not a function' , code )
44+ return code
45+ } ,
4746 } )
4847 }
4948
@@ -60,7 +59,7 @@ module.exports = (app, options) => {
6059 // anything other than 'file not found' should be a hard fail.
6160 if ( err . code !== 'ENOENT' ) {
6261 log . error ( `Failure loading component path: ${ requirePath } ` , err . stack )
63- process . exit ( 1 )
62+ throw err
6463 }
6564 }
6665 }
0 commit comments