mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 19:01:47 +03:00
refactor(shared): dedupe config path eval
This commit is contained in:
+8
-59
@@ -1,7 +1,11 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import type { OpenClawConfig, HookConfig } from "../config/config.js";
|
||||
import type { HookEligibilityContext, HookEntry } from "./types.js";
|
||||
import {
|
||||
hasBinary,
|
||||
isConfigPathTruthyWithDefaults,
|
||||
resolveConfigPath,
|
||||
resolveRuntimePlatform,
|
||||
} from "../shared/config-eval.js";
|
||||
import { resolveHookKey } from "./frontmatter.js";
|
||||
|
||||
const DEFAULT_CONFIG_VALUES: Record<string, boolean> = {
|
||||
@@ -10,40 +14,10 @@ const DEFAULT_CONFIG_VALUES: Record<string, boolean> = {
|
||||
"workspace.dir": true,
|
||||
};
|
||||
|
||||
function isTruthy(value: unknown): boolean {
|
||||
if (value === undefined || value === null) {
|
||||
return false;
|
||||
}
|
||||
if (typeof value === "boolean") {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "number") {
|
||||
return value !== 0;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
return value.trim().length > 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function resolveConfigPath(config: OpenClawConfig | undefined, pathStr: string) {
|
||||
const parts = pathStr.split(".").filter(Boolean);
|
||||
let current: unknown = config;
|
||||
for (const part of parts) {
|
||||
if (typeof current !== "object" || current === null) {
|
||||
return undefined;
|
||||
}
|
||||
current = (current as Record<string, unknown>)[part];
|
||||
}
|
||||
return current;
|
||||
}
|
||||
export { hasBinary, resolveConfigPath, resolveRuntimePlatform };
|
||||
|
||||
export function isConfigPathTruthy(config: OpenClawConfig | undefined, pathStr: string): boolean {
|
||||
const value = resolveConfigPath(config, pathStr);
|
||||
if (value === undefined && pathStr in DEFAULT_CONFIG_VALUES) {
|
||||
return DEFAULT_CONFIG_VALUES[pathStr];
|
||||
}
|
||||
return isTruthy(value);
|
||||
return isConfigPathTruthyWithDefaults(config, pathStr, DEFAULT_CONFIG_VALUES);
|
||||
}
|
||||
|
||||
export function resolveHookConfig(
|
||||
@@ -61,31 +35,6 @@ export function resolveHookConfig(
|
||||
return entry;
|
||||
}
|
||||
|
||||
export function resolveRuntimePlatform(): string {
|
||||
return process.platform;
|
||||
}
|
||||
|
||||
export function hasBinary(bin: string): boolean {
|
||||
const pathEnv = process.env.PATH ?? "";
|
||||
const parts = pathEnv.split(path.delimiter).filter(Boolean);
|
||||
const extensions =
|
||||
process.platform === "win32"
|
||||
? ["", ...(process.env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean)]
|
||||
: [""];
|
||||
for (const part of parts) {
|
||||
for (const ext of extensions) {
|
||||
const candidate = path.join(part, bin + ext);
|
||||
try {
|
||||
fs.accessSync(candidate, fs.constants.X_OK);
|
||||
return true;
|
||||
} catch {
|
||||
// keep scanning
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function shouldIncludeHook(params: {
|
||||
entry: HookEntry;
|
||||
config?: OpenClawConfig;
|
||||
|
||||
Reference in New Issue
Block a user