Ohm-Management - Projektarbeit B-ME
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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. Defaults to `lib/rules`.
  18. * @param {string} cwd Current working directory
  19. * @returns {Object} Loaded rule modules by rule ids (file names).
  20. */
  21. module.exports = function(relativeRulesDir, cwd) {
  22. const rulesDir = relativeRulesDir
  23. ? path.resolve(cwd, relativeRulesDir)
  24. : path.join(__dirname, "rules");
  25. // cache will help performance as IO operation are expensive
  26. if (rulesDirCache[rulesDir]) {
  27. return rulesDirCache[rulesDir];
  28. }
  29. const rules = Object.create(null);
  30. fs.readdirSync(rulesDir).forEach(file => {
  31. if (path.extname(file) !== ".js") {
  32. return;
  33. }
  34. rules[file.slice(0, -3)] = path.join(rulesDir, file);
  35. });
  36. rulesDirCache[rulesDir] = rules;
  37. return rules;
  38. };