[Electron] Show server launch args in server config panel (#1669)

* Move revertChanges

* Show launch args

* Explicit ServerConfigValue type

* nit

* nit

* Add tests
This commit is contained in:
Chenlei Hu
2024-11-24 15:14:05 -08:00
committed by GitHub
parent c61ed4da37
commit e01c8f06c7
6 changed files with 238 additions and 99 deletions

View File

@@ -1,4 +1,4 @@
import { ServerConfig } from '@/constants/serverConfig'
import { ServerConfig, ServerConfigValue } from '@/constants/serverConfig'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
@@ -14,18 +14,26 @@ export type ServerConfigWithValue<T> = ServerConfig<T> & {
}
export const useServerConfigStore = defineStore('serverConfig', () => {
const serverConfigById = ref<Record<string, ServerConfigWithValue<any>>>({})
const serverConfigById = ref<
Record<string, ServerConfigWithValue<ServerConfigValue>>
>({})
const serverConfigs = computed(() => {
return Object.values(serverConfigById.value)
})
const modifiedConfigs = computed<ServerConfigWithValue<any>[]>(() => {
return serverConfigs.value.filter((config) => {
return config.initialValue !== config.value
})
})
const modifiedConfigs = computed<ServerConfigWithValue<ServerConfigValue>[]>(
() => {
return serverConfigs.value.filter((config) => {
return config.initialValue !== config.value
})
}
)
const revertChanges = () => {
for (const config of modifiedConfigs.value) {
config.value = config.initialValue
}
}
const serverConfigsByCategory = computed<
Record<string, ServerConfigWithValue<any>[]>
Record<string, ServerConfigWithValue<ServerConfigValue>[]>
>(() => {
return serverConfigs.value.reduce(
(acc, config) => {
@@ -34,15 +42,17 @@ export const useServerConfigStore = defineStore('serverConfig', () => {
acc[category].push(config)
return acc
},
{} as Record<string, ServerConfigWithValue<any>[]>
{} as Record<string, ServerConfigWithValue<ServerConfigValue>[]>
)
})
const serverConfigValues = computed<Record<string, any>>(() => {
const serverConfigValues = computed<Record<string, ServerConfigValue>>(() => {
return Object.fromEntries(
serverConfigs.value.map((config) => {
return [
config.id,
config.value === config.defaultValue || !config.value
config.value === config.defaultValue ||
config.value === null ||
config.value === undefined
? undefined
: config.value
]
@@ -50,10 +60,18 @@ export const useServerConfigStore = defineStore('serverConfig', () => {
)
})
const launchArgs = computed<Record<string, string>>(() => {
return Object.assign(
const args: Record<
string,
Omit<ServerConfigValue, 'undefined' | 'null'>
> = Object.assign(
{},
...serverConfigs.value.map((config) => {
if (config.value === config.defaultValue || !config.value) {
// Filter out configs that have the default value or undefined | null value
if (
config.value === config.defaultValue ||
config.value === null ||
config.value === undefined
) {
return {}
}
return config.getValue
@@ -61,11 +79,29 @@ export const useServerConfigStore = defineStore('serverConfig', () => {
: { [config.id]: config.value }
})
)
// Convert true to empty string
// Convert number to string
return Object.fromEntries(
Object.entries(args).map(([key, value]) => {
if (value === true) {
return [key, '']
}
return [key, value.toString()]
})
) as Record<string, string>
})
const commandLineArgs = computed<string>(() => {
return Object.entries(launchArgs.value)
.map(([key, value]) => [`--${key}`, value])
.flat()
.filter((arg: string) => arg !== '')
.join(' ')
})
function loadServerConfig(
configs: ServerConfig<any>[],
values: Record<string, any>
configs: ServerConfig<ServerConfigValue>[],
values: Record<string, ServerConfigValue>
) {
for (const config of configs) {
const value = values[config.id] ?? config.defaultValue
@@ -84,6 +120,8 @@ export const useServerConfigStore = defineStore('serverConfig', () => {
serverConfigsByCategory,
serverConfigValues,
launchArgs,
commandLineArgs,
revertChanges,
loadServerConfig
}
})