Skip to content

Instantly share code, notes, and snippets.

@suissa
Created October 4, 2017 20:28
Show Gist options
  • Save suissa/d54ed931c293eb85c14380b80af5efd2 to your computer and use it in GitHub Desktop.
Save suissa/d54ed931c293eb85c14380b80af5efd2 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