feat: add settings config UI

This commit is contained in:
Federico Jaramillo Martinez
2026-06-03 20:20:54 +02:00
parent e0ae5ef9b2
commit 4495a26ffd
21 changed files with 863 additions and 19 deletions
+33 -7
View File
@@ -1,12 +1,9 @@
import { existsSync, readFileSync } from "node:fs";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join, resolve } from "node:path";
import type { PiWebConfigValues } from "./shared/apiTypes.js";
export interface PiWebConfig {
host?: string;
port?: number;
allowedHosts?: string[] | true;
}
export type PiWebConfig = PiWebConfigValues;
export interface LoadedPiWebConfig {
path: string;
@@ -14,7 +11,7 @@ export interface LoadedPiWebConfig {
config: PiWebConfig;
}
interface LoadOptions {
export interface LoadOptions {
env?: NodeJS.ProcessEnv;
cwd?: string;
}
@@ -69,6 +66,35 @@ export function effectivePiWebConfig(options: LoadOptions = {}): LoadedPiWebConf
};
}
export function savePiWebConfig(config: PiWebConfig, options: LoadOptions = {}): LoadedPiWebConfig {
const env = options.env ?? process.env;
const path = piWebConfigPath(env, options.cwd ?? process.cwd());
const normalized = parsePiWebConfig(piWebConfigRecord(config), path);
const existing = readExistingConfigObject(path);
delete existing["host"];
delete existing["port"];
delete existing["allowedHosts"];
const merged = { ...existing, ...piWebConfigRecord(normalized) };
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, `${JSON.stringify(merged, null, 2)}\n`, "utf8");
return { path, exists: true, config: normalized };
}
function readExistingConfigObject(path: string): Record<string, unknown> {
if (!existsSync(path)) return {};
const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
if (!isRecord(parsed)) throw new Error(`PI WEB config must be a JSON object: ${path}`);
return parsed;
}
function piWebConfigRecord(config: PiWebConfig): Record<string, unknown> {
return {
...(config.host !== undefined ? { host: config.host } : {}),
...(config.port !== undefined ? { port: config.port } : {}),
...(config.allowedHosts !== undefined ? { allowedHosts: config.allowedHosts } : {}),
};
}
function parsePiWebConfig(value: Record<string, unknown>, path: string): PiWebConfig {
return {
...(value["host"] !== undefined ? { host: parseString(value["host"], "host", path) } : {}),