|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { Settings, SettingScope, LoadedSettings } from '../config/settings.js'; |
|
|
import { |
|
|
SETTINGS_SCHEMA, |
|
|
SettingDefinition, |
|
|
SettingsSchema, |
|
|
} from '../config/settingsSchema.js'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function flattenSchema( |
|
|
schema: SettingsSchema, |
|
|
prefix = '', |
|
|
): Record<string, SettingDefinition & { key: string }> { |
|
|
let result: Record<string, SettingDefinition & { key: string }> = {}; |
|
|
for (const key in schema) { |
|
|
const newKey = prefix ? `${prefix}.${key}` : key; |
|
|
const definition = schema[key]; |
|
|
result[newKey] = { ...definition, key: newKey }; |
|
|
if (definition.properties) { |
|
|
result = { ...result, ...flattenSchema(definition.properties, newKey) }; |
|
|
} |
|
|
} |
|
|
return result; |
|
|
} |
|
|
|
|
|
const FLATTENED_SCHEMA = flattenSchema(SETTINGS_SCHEMA); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSettingsByCategory(): Record< |
|
|
string, |
|
|
Array<SettingDefinition & { key: string }> |
|
|
> { |
|
|
const categories: Record< |
|
|
string, |
|
|
Array<SettingDefinition & { key: string }> |
|
|
> = {}; |
|
|
|
|
|
Object.values(FLATTENED_SCHEMA).forEach((definition) => { |
|
|
const category = definition.category; |
|
|
if (!categories[category]) { |
|
|
categories[category] = []; |
|
|
} |
|
|
categories[category].push(definition); |
|
|
}); |
|
|
|
|
|
return categories; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSettingDefinition( |
|
|
key: string, |
|
|
): (SettingDefinition & { key: string }) | undefined { |
|
|
return FLATTENED_SCHEMA[key]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function requiresRestart(key: string): boolean { |
|
|
return FLATTENED_SCHEMA[key]?.requiresRestart ?? false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDefaultValue(key: string): SettingDefinition['default'] { |
|
|
return FLATTENED_SCHEMA[key]?.default; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getRestartRequiredSettings(): string[] { |
|
|
return Object.values(FLATTENED_SCHEMA) |
|
|
.filter((definition) => definition.requiresRestart) |
|
|
.map((definition) => definition.key); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getNestedValue( |
|
|
obj: Record<string, unknown>, |
|
|
path: string[], |
|
|
): unknown { |
|
|
const [first, ...rest] = path; |
|
|
if (!first || !(first in obj)) { |
|
|
return undefined; |
|
|
} |
|
|
const value = obj[first]; |
|
|
if (rest.length === 0) { |
|
|
return value; |
|
|
} |
|
|
if (value && typeof value === 'object' && value !== null) { |
|
|
return getNestedValue(value as Record<string, unknown>, rest); |
|
|
} |
|
|
return undefined; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getEffectiveValue( |
|
|
key: string, |
|
|
settings: Settings, |
|
|
mergedSettings: Settings, |
|
|
): SettingDefinition['default'] { |
|
|
const definition = getSettingDefinition(key); |
|
|
if (!definition) { |
|
|
return undefined; |
|
|
} |
|
|
|
|
|
const path = key.split('.'); |
|
|
|
|
|
|
|
|
let value = getNestedValue(settings as Record<string, unknown>, path); |
|
|
if (value !== undefined) { |
|
|
return value as SettingDefinition['default']; |
|
|
} |
|
|
|
|
|
|
|
|
value = getNestedValue(mergedSettings as Record<string, unknown>, path); |
|
|
if (value !== undefined) { |
|
|
return value as SettingDefinition['default']; |
|
|
} |
|
|
|
|
|
|
|
|
return definition.default; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getAllSettingKeys(): string[] { |
|
|
return Object.keys(FLATTENED_SCHEMA); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSettingsByType( |
|
|
type: SettingDefinition['type'], |
|
|
): Array<SettingDefinition & { key: string }> { |
|
|
return Object.values(FLATTENED_SCHEMA).filter( |
|
|
(definition) => definition.type === type, |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSettingsRequiringRestart(): Array< |
|
|
SettingDefinition & { |
|
|
key: string; |
|
|
} |
|
|
> { |
|
|
return Object.values(FLATTENED_SCHEMA).filter( |
|
|
(definition) => definition.requiresRestart, |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isValidSettingKey(key: string): boolean { |
|
|
return key in FLATTENED_SCHEMA; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSettingCategory(key: string): string | undefined { |
|
|
return FLATTENED_SCHEMA[key]?.category; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function shouldShowInDialog(key: string): boolean { |
|
|
return FLATTENED_SCHEMA[key]?.showInDialog ?? true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDialogSettingsByCategory(): Record< |
|
|
string, |
|
|
Array<SettingDefinition & { key: string }> |
|
|
> { |
|
|
const categories: Record< |
|
|
string, |
|
|
Array<SettingDefinition & { key: string }> |
|
|
> = {}; |
|
|
|
|
|
Object.values(FLATTENED_SCHEMA) |
|
|
.filter((definition) => definition.showInDialog !== false) |
|
|
.forEach((definition) => { |
|
|
const category = definition.category; |
|
|
if (!categories[category]) { |
|
|
categories[category] = []; |
|
|
} |
|
|
categories[category].push(definition); |
|
|
}); |
|
|
|
|
|
return categories; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDialogSettingsByType( |
|
|
type: SettingDefinition['type'], |
|
|
): Array<SettingDefinition & { key: string }> { |
|
|
return Object.values(FLATTENED_SCHEMA).filter( |
|
|
(definition) => |
|
|
definition.type === type && definition.showInDialog !== false, |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDialogSettingKeys(): string[] { |
|
|
return Object.values(FLATTENED_SCHEMA) |
|
|
.filter((definition) => definition.showInDialog !== false) |
|
|
.map((definition) => definition.key); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSettingValue( |
|
|
key: string, |
|
|
settings: Settings, |
|
|
mergedSettings: Settings, |
|
|
): boolean { |
|
|
const definition = getSettingDefinition(key); |
|
|
if (!definition) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
const value = getEffectiveValue(key, settings, mergedSettings); |
|
|
|
|
|
if (typeof value === 'boolean') { |
|
|
return value; |
|
|
} |
|
|
|
|
|
const defaultValue = definition.default; |
|
|
if (typeof defaultValue === 'boolean') { |
|
|
return defaultValue; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isSettingModified(key: string, value: boolean): boolean { |
|
|
const defaultValue = getDefaultValue(key); |
|
|
|
|
|
if (typeof defaultValue === 'boolean') { |
|
|
return value !== defaultValue; |
|
|
} |
|
|
|
|
|
return value === true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function settingExistsInScope( |
|
|
key: string, |
|
|
scopeSettings: Settings, |
|
|
): boolean { |
|
|
const path = key.split('.'); |
|
|
const value = getNestedValue(scopeSettings as Record<string, unknown>, path); |
|
|
return value !== undefined; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setNestedValue( |
|
|
obj: Record<string, unknown>, |
|
|
path: string[], |
|
|
value: unknown, |
|
|
): Record<string, unknown> { |
|
|
const [first, ...rest] = path; |
|
|
if (!first) { |
|
|
return obj; |
|
|
} |
|
|
|
|
|
if (rest.length === 0) { |
|
|
obj[first] = value; |
|
|
return obj; |
|
|
} |
|
|
|
|
|
if (!obj[first] || typeof obj[first] !== 'object') { |
|
|
obj[first] = {}; |
|
|
} |
|
|
|
|
|
setNestedValue(obj[first] as Record<string, unknown>, rest, value); |
|
|
return obj; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setPendingSettingValue( |
|
|
key: string, |
|
|
value: boolean, |
|
|
pendingSettings: Settings, |
|
|
): Settings { |
|
|
const path = key.split('.'); |
|
|
const newSettings = JSON.parse(JSON.stringify(pendingSettings)); |
|
|
setNestedValue(newSettings, path, value); |
|
|
return newSettings; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function setPendingSettingValueAny( |
|
|
key: string, |
|
|
value: unknown, |
|
|
pendingSettings: Settings, |
|
|
): Settings { |
|
|
const path = key.split('.'); |
|
|
const newSettings = structuredClone(pendingSettings); |
|
|
setNestedValue(newSettings, path, value); |
|
|
return newSettings; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function hasRestartRequiredSettings( |
|
|
modifiedSettings: Set<string>, |
|
|
): boolean { |
|
|
return Array.from(modifiedSettings).some((key) => requiresRestart(key)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getRestartRequiredFromModified( |
|
|
modifiedSettings: Set<string>, |
|
|
): string[] { |
|
|
return Array.from(modifiedSettings).filter((key) => requiresRestart(key)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function saveModifiedSettings( |
|
|
modifiedSettings: Set<string>, |
|
|
pendingSettings: Settings, |
|
|
loadedSettings: LoadedSettings, |
|
|
scope: SettingScope, |
|
|
): void { |
|
|
modifiedSettings.forEach((settingKey) => { |
|
|
const path = settingKey.split('.'); |
|
|
const value = getNestedValue( |
|
|
pendingSettings as Record<string, unknown>, |
|
|
path, |
|
|
); |
|
|
|
|
|
if (value === undefined) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const existsInOriginalFile = settingExistsInScope( |
|
|
settingKey, |
|
|
loadedSettings.forScope(scope).settings, |
|
|
); |
|
|
|
|
|
const isDefaultValue = value === getDefaultValue(settingKey); |
|
|
|
|
|
if (existsInOriginalFile || !isDefaultValue) { |
|
|
|
|
|
|
|
|
const [parentKey] = path; |
|
|
if (parentKey) { |
|
|
const newParentValue = setPendingSettingValueAny( |
|
|
settingKey, |
|
|
value, |
|
|
loadedSettings.forScope(scope).settings, |
|
|
)[parentKey as keyof Settings]; |
|
|
|
|
|
loadedSettings.setValue( |
|
|
scope, |
|
|
parentKey as keyof Settings, |
|
|
newParentValue, |
|
|
); |
|
|
} |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDisplayValue( |
|
|
key: string, |
|
|
settings: Settings, |
|
|
_mergedSettings: Settings, |
|
|
modifiedSettings: Set<string>, |
|
|
pendingSettings?: Settings, |
|
|
): string { |
|
|
|
|
|
let value: boolean; |
|
|
if (pendingSettings && settingExistsInScope(key, pendingSettings)) { |
|
|
|
|
|
value = getSettingValue(key, pendingSettings, {}); |
|
|
} else if (settingExistsInScope(key, settings)) { |
|
|
|
|
|
value = getSettingValue(key, settings, {}); |
|
|
} else { |
|
|
|
|
|
const defaultValue = getDefaultValue(key); |
|
|
value = typeof defaultValue === 'boolean' ? defaultValue : false; |
|
|
} |
|
|
|
|
|
const valueString = String(value); |
|
|
|
|
|
|
|
|
const defaultValue = getDefaultValue(key); |
|
|
const isChangedFromDefault = |
|
|
typeof defaultValue === 'boolean' ? value !== defaultValue : value === true; |
|
|
const isInModifiedSettings = modifiedSettings.has(key); |
|
|
|
|
|
|
|
|
if (settingExistsInScope(key, settings) || isInModifiedSettings) { |
|
|
return `${valueString}*`; |
|
|
} |
|
|
if (isChangedFromDefault || isInModifiedSettings) { |
|
|
return `${valueString}*`; |
|
|
} |
|
|
|
|
|
return valueString; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isDefaultValue(key: string, settings: Settings): boolean { |
|
|
return !settingExistsInScope(key, settings); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isValueInherited( |
|
|
key: string, |
|
|
settings: Settings, |
|
|
_mergedSettings: Settings, |
|
|
): boolean { |
|
|
return !settingExistsInScope(key, settings); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getEffectiveDisplayValue( |
|
|
key: string, |
|
|
settings: Settings, |
|
|
mergedSettings: Settings, |
|
|
): boolean { |
|
|
return getSettingValue(key, settings, mergedSettings); |
|
|
} |
|
|
|