Skip to content

Instantly share code, notes, and snippets.

@CesarBalzer
Forked from suissa/controller.js
Created May 31, 2019 00:06
Show Gist options
  • Save CesarBalzer/f390fad7c444f1c441edf4bc4beb8c89 to your computer and use it in GitHub Desktop.
Save CesarBalzer/f390fad7c444f1c441edf4bc4beb8c89 to your computer and use it in GitHub Desktop.
const Model = require( './model' )
const createController = '../../helpers/createController'
module.exports = require( createController )( Model )( __dirname )
const fs = require( 'fs' )
const path = require( 'path' )
const ACTIONS_PATH = '/actions/'
const forbiden = [ '.', '_' ]
const listFilesIn = require( './listFilesIn' )
const filterHiddenFiles = require( './filterHiddenFiles' )( forbiden )
const removeJSExtension = ( action ) => action.replace('.js', '')
const toObject = ( obj, action ) => Object.assign( obj, action )
const createAction = ( Model, localPath ) => ( action ) => ( {
[ action ]: require( localPath + ACTIONS_PATH + action )( Model )
} )
const createController = ( Model ) => ( localPath ) => //console.log('localPath', localPath)
listFilesIn( localPath + ACTIONS_PATH )
.filter( filterHiddenFiles )
.map( removeJSExtension )
.map( createAction( Model, localPath ) )
.reduce( toObject, {} )
module.exports = ( Model ) => ( localPath ) => createController( Model )( localPath )
module.exports = ( forbiden ) => ( file ) =>
!forbiden.some( char => file.startsWith( char ) )
? file
: false
module.exports = ( path ) => require( 'fs' ).readdirSync( path )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment