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.

load-rules.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @fileoverview Module for loading rules from files and directories.
  3. * @author Michael Ficarra
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const fs = require("fs"),
  10. path = require("path");
  11. const rulesDirCache = {};
  12. //------------------------------------------------------------------------------
  13. // Public Interface
  14. //------------------------------------------------------------------------------
  15. /**
  16. * Load all rule modules from specified directory.
  17. * @param {string} relativeRulesDir Path to rules directory, may be relative.
  18. * @param {string} cwd Current working directory
  19. * @returns {Object} Loaded rule modules.
  20. */
  21. module.exports = function(relativeRulesDir, cwd) {
  22. const rulesDir = path.resolve(cwd, relativeRulesDir);
  23. // cache will help performance as IO operation are expensive
  24. if (rulesDirCache[rulesDir]) {
  25. return rulesDirCache[rulesDir];
  26. }
  27. const rules = Object.create(null);
  28. fs.readdirSync(rulesDir).forEach(file => {
  29. if (path.extname(file) !== ".js") {
  30. return;
  31. }
  32. rules[file.slice(0, -3)] = require(path.join(rulesDir, file));
  33. });
  34. rulesDirCache[rulesDir] = rules;
  35. return rules;
  36. };