config.utils.js 2.3 KB

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