mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-08-15 07:17:30 +00:00
Reject empty config path segments
This commit is contained in:
parent
8f4691584c
commit
c6cc21409b
2 changed files with 20 additions and 5 deletions
9
packages/cli/src/config.test.ts
Normal file
9
packages/cli/src/config.test.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { getConfigValue, setConfigValue } from "./config.js";
|
||||
|
||||
describe("CLI config", () => {
|
||||
it("rejects empty config path segments", () => {
|
||||
expect(() => getConfigValue("waiting..arcade", {})).toThrow(/empty segments/);
|
||||
expect(() => setConfigValue("waiting..arcade", "true", {})).toThrow(/empty segments/);
|
||||
});
|
||||
});
|
||||
|
|
@ -40,14 +40,12 @@ export function writeConfig(config: JsonObject) {
|
|||
}
|
||||
|
||||
export function getConfigValue(path: string, config = readConfig()) {
|
||||
return path.split(".").reduce<unknown>((current, key) => (isObject(current) ? current[key] : undefined), config);
|
||||
const parts = splitConfigPath(path);
|
||||
return parts.reduce<unknown>((current, key) => (isObject(current) ? current[key] : undefined), config);
|
||||
}
|
||||
|
||||
export function setConfigValue(path: string, rawValue: string, config = readConfig()) {
|
||||
const parts = path.split(".").filter(Boolean);
|
||||
if (parts.length === 0) {
|
||||
throw new Error("Config path cannot be empty.");
|
||||
}
|
||||
const parts = splitConfigPath(path);
|
||||
let current: JsonObject = config;
|
||||
for (const part of parts.slice(0, -1)) {
|
||||
if (!isObject(current[part])) {
|
||||
|
|
@ -59,6 +57,14 @@ export function setConfigValue(path: string, rawValue: string, config = readConf
|
|||
return config;
|
||||
}
|
||||
|
||||
function splitConfigPath(path: string) {
|
||||
const parts = path.split(".");
|
||||
if (parts.length === 0 || parts.some((part) => part.length === 0)) {
|
||||
throw new Error("Config path cannot contain empty segments.");
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
export function parseConfigValue(value: string): unknown {
|
||||
if (value === "true") return true;
|
||||
if (value === "false") return false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue