config.utils.js 2.2 KB

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