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.

config-cache.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @fileoverview Responsible for caching config files
  3. * @author Sylvan Mably
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Get a string hash for a config vector
  11. * @param {Array<Object>} vector config vector to hash
  12. * @returns {string} hash of the vector values
  13. * @private
  14. */
  15. function hash(vector) {
  16. return JSON.stringify(vector);
  17. }
  18. //------------------------------------------------------------------------------
  19. // API
  20. //------------------------------------------------------------------------------
  21. /**
  22. * Configuration caching class
  23. */
  24. module.exports = class ConfigCache {
  25. constructor() {
  26. this.configFullNameCache = new Map();
  27. this.localHierarchyCache = new Map();
  28. this.mergedVectorCache = new Map();
  29. this.mergedCache = new Map();
  30. }
  31. /**
  32. * Gets a config object from the cache for the specified config file path.
  33. * @param {string} configFullName the name of the configuration as used in the eslint config(e.g. 'plugin:node/recommended'),
  34. * or the absolute path to a config file. This should uniquely identify a config.
  35. * @returns {Object|null} config object, if found in the cache, otherwise null
  36. * @private
  37. */
  38. getConfig(configFullName) {
  39. return this.configFullNameCache.get(configFullName);
  40. }
  41. /**
  42. * Sets a config object in the cache for the specified config file path.
  43. * @param {string} configFullName the name of the configuration as used in the eslint config(e.g. 'plugin:node/recommended'),
  44. * or the absolute path to a config file. This should uniquely identify a config.
  45. * @param {Object} config the config object to add to the cache
  46. * @returns {void}
  47. * @private
  48. */
  49. setConfig(configFullName, config) {
  50. this.configFullNameCache.set(configFullName, config);
  51. }
  52. /**
  53. * Gets a list of hierarchy-local config objects that apply to the specified directory.
  54. * @param {string} directory the path to the directory
  55. * @returns {Object[]|null} a list of config objects, if found in the cache, otherwise null
  56. * @private
  57. */
  58. getHierarchyLocalConfigs(directory) {
  59. return this.localHierarchyCache.get(directory);
  60. }
  61. /**
  62. * For each of the supplied parent directories, sets the list of config objects for that directory to the
  63. * appropriate subset of the supplied parent config objects.
  64. * @param {string[]} parentDirectories a list of parent directories to add to the config cache
  65. * @param {Object[]} parentConfigs a list of config objects that apply to the lowest directory in parentDirectories
  66. * @returns {void}
  67. * @private
  68. */
  69. setHierarchyLocalConfigs(parentDirectories, parentConfigs) {
  70. parentDirectories.forEach((localConfigDirectory, i) => {
  71. const directoryParentConfigs = parentConfigs.slice(0, parentConfigs.length - i);
  72. this.localHierarchyCache.set(localConfigDirectory, directoryParentConfigs);
  73. });
  74. }
  75. /**
  76. * Gets a merged config object corresponding to the supplied vector.
  77. * @param {Array<Object>} vector the vector to find a merged config for
  78. * @returns {Object|null} a merged config object, if found in the cache, otherwise null
  79. * @private
  80. */
  81. getMergedVectorConfig(vector) {
  82. return this.mergedVectorCache.get(hash(vector));
  83. }
  84. /**
  85. * Sets a merged config object in the cache for the supplied vector.
  86. * @param {Array<Object>} vector the vector to save a merged config for
  87. * @param {Object} config the merged config object to add to the cache
  88. * @returns {void}
  89. * @private
  90. */
  91. setMergedVectorConfig(vector, config) {
  92. this.mergedVectorCache.set(hash(vector), config);
  93. }
  94. /**
  95. * Gets a merged config object corresponding to the supplied vector, including configuration options from outside
  96. * the vector.
  97. * @param {Array<Object>} vector the vector to find a merged config for
  98. * @returns {Object|null} a merged config object, if found in the cache, otherwise null
  99. * @private
  100. */
  101. getMergedConfig(vector) {
  102. return this.mergedCache.get(hash(vector));
  103. }
  104. /**
  105. * Sets a merged config object in the cache for the supplied vector, including configuration options from outside
  106. * the vector.
  107. * @param {Array<Object>} vector the vector to save a merged config for
  108. * @param {Object} config the merged config object to add to the cache
  109. * @returns {void}
  110. * @private
  111. */
  112. setMergedConfig(vector, config) {
  113. this.mergedCache.set(hash(vector), config);
  114. }
  115. };