refactor(cli): normalize route boundaries

This commit is contained in:
Peter Steinberger
2026-04-06 15:37:34 +01:00
parent e4fa414ed0
commit ff7fe37d17
7 changed files with 95 additions and 38 deletions

View File

@@ -5,12 +5,24 @@ export type StructuredCommandPathMatchRule = {
export 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 };
}
export function matchesCommandPath(
commandPath: string[],
pattern: readonly string[],
@@ -23,10 +35,10 @@ export function matchesCommandPath(
}
export function matchesCommandPathRule(commandPath: string[], rule: CommandPathMatchRule): boolean {
if (!isStructuredCommandPathMatchRule(rule)) {
return matchesCommandPath(commandPath, rule);
}
return matchesCommandPath(commandPath, rule.pattern, { exact: rule.exact });
const normalizedRule = normalizeCommandPathMatchRule(rule);
return matchesCommandPath(commandPath, normalizedRule.pattern, {
exact: normalizedRule.exact,
});
}
export function matchesAnyCommandPath(