Files
openclaw/src/cli/command-path-matches.ts
Peter Steinberger 1e7510ae10 docs: continue inline comment pass (#88849)
Adds broad inline comments and JSDoc for CLI, cron, outbound/channel, plugin SDK, ACP, shared helpers, net policy, and related utility contracts. Proof: git diff --check on latest exact head plus focused cron tests passed; CI had no failing checks observed before merge attempt.
2026-05-31 22:32:28 -04:00

53 lines
1.7 KiB
TypeScript

type StructuredCommandPathMatchRule = {
pattern: readonly string[];
exact?: boolean;
};
type CommandPathMatchRule = readonly string[] | StructuredCommandPathMatchRule;
type NormalizedCommandPathMatchRule = {
pattern: readonly string[];
exact: boolean;
};
function isStructuredCommandPathMatchRule(
rule: CommandPathMatchRule,
): rule is StructuredCommandPathMatchRule {
return !Array.isArray(rule);
}
function normalizeCommandPathMatchRule(rule: CommandPathMatchRule): NormalizedCommandPathMatchRule {
if (!isStructuredCommandPathMatchRule(rule)) {
return { pattern: rule, exact: false };
}
return { pattern: rule.pattern, exact: rule.exact ?? false };
}
/** Matches a command path prefix, or the full path when `exact` is requested. */
export function matchesCommandPath(
commandPath: string[],
pattern: readonly string[],
params?: { exact?: boolean },
): boolean {
if (pattern.some((segment, index) => commandPath[index] !== segment)) {
return false;
}
return !params?.exact || commandPath.length === pattern.length;
}
/** Applies the shared command-path rule shape used by startup and help policies. */
export function matchesCommandPathRule(commandPath: string[], rule: CommandPathMatchRule): boolean {
const normalizedRule = normalizeCommandPathMatchRule(rule);
return matchesCommandPath(commandPath, normalizedRule.pattern, {
exact: normalizedRule.exact,
});
}
/** Returns whether any configured command-path rule matches the parsed command path. */
export function matchesAnyCommandPath(
commandPath: string[],
rules: readonly CommandPathMatchRule[],
): boolean {
return rules.some((rule) => matchesCommandPathRule(commandPath, rule));
}