1
0
Fork 0

wip: Work on design init

This commit is contained in:
Joost De Cock 2022-09-24 12:44:41 +02:00 committed by joostdecock
parent f0e233513b
commit 485492e452
7 changed files with 307 additions and 280 deletions

View file

@ -603,165 +603,6 @@ export function __addNonEnumProp(obj, name, value) {
return obj
}
/**
* Resolves/Adds a part's configured measurements to the global config
*
* @private
* @param {Part} part - The part of which to resolve the config
* @param {onject} config - The global config
* @param {Store} store - The store, used for logging
* @return {object} config - The mutated global config
*/
const __addPartMeasurements = (part, config, store, list = false) => {
if (!list) list = config.measurements ? [...config.measurements] : []
if (part.measurements) {
for (const m of part.measurements) {
if (list.indexOf(m) === -1) {
list.push(m)
store.log.debug(`🟠 __${m}__ measurement is required in \`${part.name}\``)
}
}
}
if (part.from) __addPartMeasurements(part.from, config, store, list)
if (part.after) {
if (Array.isArray(part.after)) {
for (const dep of part.after) __addPartMeasurements(dep, config, store, list)
} else __addPartMeasurements(part.after, config, store, list)
}
// Weed out duplicates
config.measurements = [...new Set(list)]
return config
}
/**
* Resolves/Adds a part's configured optional measurements to the global config
*
* @private
* @param {Part} part - The part of which to resolve the config
* @param {onject} config - The global config
* @param {Store} store - The store, used for logging
* @return {object} config - The mutated global config
*/
const __addPartOptionalMeasurements = (part, config, store, list = false) => {
if (!list) list = config.optionalMeasurements ? [...config.optionalMeasurements] : []
if (part.optionalMeasurements) {
for (const m of part.optionalMeasurements) {
// Don't add it's a required measurement for another part
if (config.measurements.indexOf(m) === -1) {
if (list.indexOf(m) === -1) {
list.push(m)
store.log.debug(`🟡 __${m}__ measurement is optional in \`${part.name}\``)
}
}
}
}
if (part.from) __addPartOptionalMeasurements(part.from, config, store, list)
if (part.after) {
if (Array.isArray(part.after)) {
for (const dep of part.after) __addPartOptionalMeasurements(dep, config, store, list)
} else __addPartOptionalMeasurements(part.after, config, store, list)
}
// Weed out duplicates
config.optionalMeasurements = [...new Set(list)]
return config
}
/**
* Resolves/Adds a part's configured options to the global config
*
* @private
* @param {Part} part - The part of which to resolve the config
* @param {onject} config - The global config
* @param {Store} store - The store, used for logging
* @return {object} config - The mutated global config
*/
const __addPartOptions = (part, config, store) => {
if (part.options) {
for (const optionName in part.options) {
if (!config.optionDistance[optionName]) {
config.optionDistance[optionName] = part.distance
config.options[optionName] = part.options[optionName]
store.log.debug(`🔵 __${optionName}__ option loaded from \`${part.name}\``)
} else if (config.optionDistance[optionName] > part.distance) {
config.options[optionName] = part.options[optionName]
store.log.debug(`🟣 __${optionName}__ option overwritten by \`${part.name}\``)
}
}
}
if (part.from) __addPartOptions(part.from, config, store)
if (part.after) {
if (Array.isArray(part.after)) {
for (const dep of part.after) __addPartOptions(dep, config, store)
} else __addPartOptions(part.after, config, store)
}
return config
}
/**
* Resolves/Adds a part's configured plugins to the global config
*
* @private
* @param {Part} part - The part of which to resolve the config
* @param {onject} config - The global config
* @param {Store} store - The store, used for logging
* @return {object} config - The mutated global config
*/
export const __addPartPlugins = (part, config, store) => {
const plugins = {}
if (!part.plugins) return config
for (const plugin of config.plugins) plugins[plugin.name] = plugin
if (!Array.isArray(part.plugins)) part.plugins = [part.plugins]
for (let plugin of part.plugins) {
// Handle [plugin, data] scenario
if (Array.isArray(plugin)) {
const pluginObj = { ...plugin[0], data: plugin[1] }
plugin = pluginObj
}
store.log.debug(`🔌 __${plugin.name}__ plugin in \`${part.name}\``)
// Do not overwrite an existing plugin with a conditional plugin unless it is also conditional
if (plugin.plugin && plugin.condition) {
if (plugins[plugin.plugin.name]?.condition) plugins[plugin.plugin.name] = plugin
else store.log.info(
`Plugin \`${plugin.plugin.name}\` was requested conditionally, but is already loaded explicitly. Not loading.`
)
} else {
plugins[plugin.name] = plugin
}
}
return {
...config,
plugins: [...new Set(Object.values(plugins))],
}
}
/**
* Resolves/Adds a part's configuration to the global config
*
* @private
* @param {Part} part - The part of which to resolve the config
* @param {onject} config - The global config
* @param {Store} store - The store, used for logging
* @return {object} config - The mutated global config
*/
export const __addPartConfig = (part, config, store) => {
if (part.resolved) return config
// Add parts, using set to keep them unique in the array
part.resolved = true
config.parts = [...new Set(config.parts).add(part)]
config = __addPartOptions(part, config, store)
config = __addPartMeasurements(part, config, store)
config = __addPartOptionalMeasurements(part, config, store)
config = __addPartPlugins(part, config, store)
return config
}
/**
* Checks whether the paramater passed to it is a valid coordinate (x and y attribute)
*