config: device: Add support for device-list configs

This commit is contained in:
Danny Lin 2021-12-16 20:24:13 -08:00
parent d8cff5ccee
commit 64dfb3f0d0

View file

@ -52,6 +52,14 @@ export interface DeviceConfig {
} }
} }
interface DeviceListConfig {
type: ConfigType.DeviceList
devices: string[] // config paths
// Not part of the final config
// includes: string[]
}
// Untyped because this isn't a full config // Untyped because this isn't a full config
export const EMPTY_FILTERS = { export const EMPTY_FILTERS = {
mode: FilterMode.Exclude, mode: FilterMode.Exclude,
@ -101,7 +109,7 @@ function mergeConfigs(base: any, overlay: any) {
if (_.isArray(a)) { if (_.isArray(a)) {
return a.concat(b) return a.concat(b)
} }
}) as DeviceConfig })
} }
async function loadOverlaysRecursive(overlays: any[], rootDir: string, root: any) { async function loadOverlaysRecursive(overlays: any[], rootDir: string, root: any) {
@ -120,7 +128,7 @@ async function loadOverlaysRecursive(overlays: any[], rootDir: string, root: any
// No dedicated parse function as this requires loading includes and overlaying // No dedicated parse function as this requires loading includes and overlaying
// them in the correct order // them in the correct order
export async function loadDevicesConfig(configPath: string) { async function loadAndMergeConfig(configPath: string) {
// TODO: type definition for structuredClone // TODO: type definition for structuredClone
let base = structuredClone(DEFAULT_CONFIG_BASE) // deep copy to avoid mutating base let base = structuredClone(DEFAULT_CONFIG_BASE) // deep copy to avoid mutating base
@ -138,5 +146,27 @@ export async function loadDevicesConfig(configPath: string) {
// Finally, cast it to the parsed config type // Finally, cast it to the parsed config type
delete merged.includes delete merged.includes
return [merged as DeviceConfig] return merged
}
export async function loadDevicesConfig(configPath: string) {
let merged = await loadAndMergeConfig(configPath)
let type = merged.type
delete merged.type
if (type == ConfigType.Device) {
return [merged as DeviceConfig]
} else if (type == ConfigType.DeviceList) {
// Load all the device configs
let list = merged as DeviceListConfig
let devices: DeviceConfig[] = []
for (let devicePath of list.devices) {
devicePath = path.resolve(path.dirname(configPath), devicePath)
devices.push(await loadAndMergeConfig(devicePath))
}
return devices
} else {
throw new Error(`Unknown config type ${type}`)
}
} }