Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.markdown 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # require-directory
  2. Recursively iterates over specified directory, `require()`'ing each file, and returning a nested hash structure containing those modules.
  3. **[Follow me (@troygoode) on Twitter!](https://twitter.com/intent/user?screen_name=troygoode)**
  4. [![NPM](https://nodei.co/npm/require-directory.png?downloads=true&stars=true)](https://nodei.co/npm/require-directory/)
  5. [![build status](https://secure.travis-ci.org/troygoode/node-require-directory.png)](http://travis-ci.org/troygoode/node-require-directory)
  6. ## How To Use
  7. ### Installation (via [npm](https://npmjs.org/package/require-directory))
  8. ```bash
  9. $ npm install require-directory
  10. ```
  11. ### Usage
  12. A common pattern in node.js is to include an index file which creates a hash of the files in its current directory. Given a directory structure like so:
  13. * app.js
  14. * routes/
  15. * index.js
  16. * home.js
  17. * auth/
  18. * login.js
  19. * logout.js
  20. * register.js
  21. `routes/index.js` uses `require-directory` to build the hash (rather than doing so manually) like so:
  22. ```javascript
  23. var requireDirectory = require('require-directory');
  24. module.exports = requireDirectory(module);
  25. ```
  26. `app.js` references `routes/index.js` like any other module, but it now has a hash/tree of the exports from the `./routes/` directory:
  27. ```javascript
  28. var routes = require('./routes');
  29. // snip
  30. app.get('/', routes.home);
  31. app.get('/register', routes.auth.register);
  32. app.get('/login', routes.auth.login);
  33. app.get('/logout', routes.auth.logout);
  34. ```
  35. The `routes` variable above is the equivalent of this:
  36. ```javascript
  37. var routes = {
  38. home: require('routes/home.js'),
  39. auth: {
  40. login: require('routes/auth/login.js'),
  41. logout: require('routes/auth/logout.js'),
  42. register: require('routes/auth/register.js')
  43. }
  44. };
  45. ```
  46. *Note that `routes.index` will be `undefined` as you would hope.*
  47. ### Specifying Another Directory
  48. You can specify which directory you want to build a tree of (if it isn't the current directory for whatever reason) by passing it as the second parameter. Not specifying the path (`requireDirectory(module)`) is the equivelant of `requireDirectory(module, __dirname)`:
  49. ```javascript
  50. var requireDirectory = require('require-directory');
  51. module.exports = requireDirectory(module, './some/subdirectory');
  52. ```
  53. For example, in the [example in the Usage section](#usage) we could have avoided creating `routes/index.js` and instead changed the first lines of `app.js` to:
  54. ```javascript
  55. var requireDirectory = require('require-directory');
  56. var routes = requireDirectory(module, './routes');
  57. ```
  58. ## Options
  59. You can pass an options hash to `require-directory` as the 2nd parameter (or 3rd if you're passing the path to another directory as the 2nd parameter already). Here are the available options:
  60. ### Whitelisting
  61. Whitelisting (either via RegExp or function) allows you to specify that only certain files be loaded.
  62. ```javascript
  63. var requireDirectory = require('require-directory'),
  64. whitelist = /onlyinclude.js$/,
  65. hash = requireDirectory(module, {include: whitelist});
  66. ```
  67. ```javascript
  68. var requireDirectory = require('require-directory'),
  69. check = function(path){
  70. if(/onlyinclude.js$/.test(path)){
  71. return true; // don't include
  72. }else{
  73. return false; // go ahead and include
  74. }
  75. },
  76. hash = requireDirectory(module, {include: check});
  77. ```
  78. ### Blacklisting
  79. Blacklisting (either via RegExp or function) allows you to specify that all but certain files should be loaded.
  80. ```javascript
  81. var requireDirectory = require('require-directory'),
  82. blacklist = /dontinclude\.js$/,
  83. hash = requireDirectory(module, {exclude: blacklist});
  84. ```
  85. ```javascript
  86. var requireDirectory = require('require-directory'),
  87. check = function(path){
  88. if(/dontinclude\.js$/.test(path)){
  89. return false; // don't include
  90. }else{
  91. return true; // go ahead and include
  92. }
  93. },
  94. hash = requireDirectory(module, {exclude: check});
  95. ```
  96. ### Visiting Objects As They're Loaded
  97. `require-directory` takes a function as the `visit` option that will be called for each module that is added to module.exports.
  98. ```javascript
  99. var requireDirectory = require('require-directory'),
  100. visitor = function(obj) {
  101. console.log(obj); // will be called for every module that is loaded
  102. },
  103. hash = requireDirectory(module, {visit: visitor});
  104. ```
  105. The visitor can also transform the objects by returning a value:
  106. ```javascript
  107. var requireDirectory = require('require-directory'),
  108. visitor = function(obj) {
  109. return obj(new Date());
  110. },
  111. hash = requireDirectory(module, {visit: visitor});
  112. ```
  113. ### Renaming Keys
  114. ```javascript
  115. var requireDirectory = require('require-directory'),
  116. renamer = function(name) {
  117. return name.toUpperCase();
  118. },
  119. hash = requireDirectory(module, {rename: renamer});
  120. ```
  121. ### No Recursion
  122. ```javascript
  123. var requireDirectory = require('require-directory'),
  124. hash = requireDirectory(module, {recurse: false});
  125. ```
  126. ## Run Unit Tests
  127. ```bash
  128. $ npm run lint
  129. $ npm test
  130. ```
  131. ## License
  132. [MIT License](http://www.opensource.org/licenses/mit-license.php)
  133. ## Author
  134. [Troy Goode](https://github.com/TroyGoode) ([troygoode@gmail.com](mailto:troygoode@gmail.com))