2

I dont know much about node.Is there anything I can do to iterate the objects without having to require it everytime

folderOrder = ['routes', 'models','fixtures', 'views', 'controllers']

require 'routes/posts_route'
require 'routes/post_route'
require 'models/post'

Is there anyway all the new files I create in folders routes and models will be automatically required without having to require each one of them. My case for this example is https://github.com/abhayathapa/ember-blogger/blob/master/app/initialize.coffee

Any help will be greatly appreciated. I have tried the answers in node.js require all files in a folder? but I get the same --- Cannot find module "fs"

Community
  • 1
  • 1
Abhaya
  • 2,086
  • 16
  • 21

3 Answers3

3

You could try something like this (if you were using node.js):

folderOrder = ['routes', 'models','fixtures', 'views', 'controllers']

fs = require 'fs'
folderOrder.forEach (folder) ->
  dir = __dirname + '/' + folder + '/'
  files = fs.readdirSync dir
  files.forEach (file) ->
    require dir + file


UPDATE

After examining the example you linked, it seems that this has nothing to do with node. It's browser code, that uses brunch for managing build. However, in the initialize.coffee, there is already a commented code snippet which does exactly that, and it seems to work if you uncomment it, and comment out the 'manual' requires. I don't know why it is not used in the first place, though.

So, initialize.coffee should contain this:

# ===== Config =====
window.App = require 'config/app'
require 'config/router'
require 'config/store'

# Load all modules in order automagically. 
# Ember likes things to work this way so everything is in the App.* namespace.
folderOrder = [
  'routes', 'models','fixtures', 'views', 'controllers', 'helpers', 'templates'
]

folderOrder.forEach (folder) ->
  # Go through the prefixes in order and require them
  window.require.list().filter((module) ->
    new RegExp("^#{folder}/").test(module)
  ).forEach((module) -> require(module))

After editing the file, run brunch w -s from the root folder and it should be working.

mutil
  • 3,205
  • 1
  • 27
  • 34
  • I get Uncaught Error: Cannot find module "fs". Do I need to create index.js inside my app folder.This is where initialize.coffee resides. – Abhaya Dec 07 '13 at 15:31
  • Thanks for the comment but it still doesnt work for me.Can you just pull my code and make it run. If I individually require all files, it works perfectly. But, if I comment out the code as you said or try what you have written above, it doesnt work. I have no clue what I'm supposed to do here.Am I missing sth on package.json ? Do I need express ?? – Abhaya Dec 07 '13 at 16:55
  • So, how do you run it? and what error do you get if you uncomment lines 7-16 of [initialize.coffee](https://github.com/abhayathapa/ember-blogger/blob/master/app/initialize.coffee) and comment everything below that? – mutil Dec 07 '13 at 17:01
  • Could you please pull the repo and make it work, it would mean massive favour. If i use ur code,I get cant find module 'fs'. I get this error. ```Uncaught TypeError: Object function (name) { var path = expand(name, '.'); if (has(cache, path)) return cache[path]; if (has(modules, path)) return initModule(path, modules[path]); var dirIndex = expand(path, './index'); if (has(cache, dirIndex)) return cache[dirIndex]; if (has(modules, dirIndex)) return initModule(dirIndex, modules[dirIndex]);``` throw new Error('Cannot find module "' + name + '"'); } has no method – Abhaya Dec 07 '13 at 17:19
  • **DO NOT** add the code I posted at first. Make a fresh git clone, uncomment the lines that I mentioned in my previous comment, run `brunch w -s` and it's working. – mutil Dec 07 '13 at 17:36
  • Yes, its working because there is already require for all the folders, routers, models,etc that is line 24 to 46. If I remove line 24-46 and uncomment 6-16, it doesnt work. I added line 24-46 just because my line 6-16 didnt work. Did you get it ? – Abhaya Dec 07 '13 at 17:52
1

Yep I'm using something like :

fs = require 'fs'
requiredFiles = {}

for file in fs.readdirSync __dirname when file isnt 'index.coffee'
    requiredFiles[file.replace /\.coffee$/, ""] = require "./#{file}"

# requiredFiles is populated with all files in the current path, except index.coffee
drinchev
  • 19,201
  • 4
  • 67
  • 93
  • I get Uncaught Error: Cannot find module "fs". Do I need to create index.js inside my app folder.This is where initialize.coffee resides? Is there sth Im missing. – Abhaya Dec 07 '13 at 15:38
0

I would do something similar to @mutil, but would instead put that logic in each given folder.

Given an array of paths, an index.coffee in each folder:

fs = require 'fs'

module.exports = (path)->
  fs.readdir "./#{path}", (err, files) ->
    if err?
      return err
    else
      return (require "./#{file}" for file in files when file isnt 'index.coffee' and file isnt '.DS_Store')

with

for folder in folderOrder
  require("./#{folder}")(folder) for folder in folderOrder

in initialize.coffee, but you may run into some async issues depending.

max
  • 5,979
  • 2
  • 21
  • 16
  • I still get same error ' Uncaught Error: Cannot find module "fs" '. And its quite cumbersome to add index.coffee for all these folders.Dont u think ? Is it something I'm missing big time :( – Abhaya Dec 07 '13 at 16:23
  • That error is odd, because fs is a core node module; not sure what would allow you to run a node app that wouldn't be able to import that module. I also think this method is nice because it lets you have a mass import statement but still let you granularly control how each folder is imported (skip/process files, etc). – max Dec 07 '13 at 20:12