config.utils.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import deepmerge from 'deepmerge'
  2. import store from '@/store'
  3. import { experiments as experimentsDEFAULT } from '@/../experimentConfig.default'
  4. import { experiments } from '@/../experimentConfig'
  5. // Merge a default config with a specific scene config
  6. const buildConfig = ({ defaultConfig = {}, scenesConfig = {} }, sceneName) =>
  7. deepmerge(defaultConfig, scenesConfig[sceneName] || {})
  8. // Merge multiple configs (used for multiple mixins)
  9. const buildMultiConfig = (confArr, sceneName) =>
  10. deepmerge.all(confArr.map(aConfig => buildConfig(aConfig, sceneName)))
  11. /**
  12. * Find the configuration.
  13. * Will use the default configuration if not found in the real one.
  14. * @param {String} experimentName The selected experiment
  15. * @returns {Object} Configuration object
  16. */
  17. const getConfigObject = experimentName => {
  18. if (experiments[experimentName])
  19. return experiments
  20. else if (experimentsDEFAULT[experimentName])
  21. return experimentsDEFAULT
  22. throw new Error(`Could not find the experiment "${experimentName}" in the config file nor the default config file.`)
  23. }
  24. /**
  25. * Build a configuration file by merging the default config with the asked scene.
  26. * The asked scene config will overwrite the default config.
  27. * It merges the mixin config with the experiment config.
  28. * Experiment config overwrites all.
  29. *
  30. * @param {Object} experimentName The selected experiment
  31. * @param {Object} sceneName The selected scene
  32. * @returns {Object} The config for the selected experiment with the selected scene
  33. */
  34. export const getExperimentConfig = (experimentName, sceneName) => {
  35. const config = getConfigObject(experimentName)
  36. // Build parent mixin config
  37. const mixinConfig = buildMultiConfig(config[experimentName].mixins, sceneName)
  38. // Build global config
  39. const globalConfig = buildConfig(config[experimentName], sceneName)
  40. // Merge configs
  41. return deepmerge(mixinConfig, globalConfig)
  42. }
  43. /**
  44. * Read config to get the list of available scenes for a given experiment.
  45. * If no whitelist is supplied, it will take all the available scenes.
  46. * If a blacklist is supplied, it will remove its scenes from the list of scenes.
  47. *
  48. * @param {Object} experimentName The selected experiment
  49. * @returns {String[]} The list of available scenes for this experiment
  50. */
  51. export const getExperimentSceneList = experimentName => {
  52. const config = getConfigObject(experimentName)
  53. let configuredScenesList = []
  54. const confObj = config[experimentName].availableScenes
  55. const scenesList = store.state.scenesList
  56. // Apply whitelist
  57. if (confObj.whitelist) configuredScenesList = scenesList.filter(x => confObj.whitelist.includes(x))
  58. else configuredScenesList = scenesList
  59. // Apply blacklist
  60. if (confObj.blacklist) configuredScenesList = configuredScenesList.filter(x => !confObj.blacklist.includes(x))
  61. return configuredScenesList
  62. }