|
| 1 | +import type { Operation } from "../deps.ts"; |
| 2 | + |
| 3 | +import { |
| 4 | + parse as parseFlags, |
| 5 | + ParseOptions, |
| 6 | +} from "https://deno.land/std@0.159.0/flags/mod.ts"; |
| 7 | + |
| 8 | +export function* dispatch( |
| 9 | + args: string[], |
| 10 | + routes: Routes, |
| 11 | + matched: string[] = [], |
| 12 | +): Operation<void> { |
| 13 | + let shellwords = parseFlags(args)._; |
| 14 | + match: |
| 15 | + for (let [key, value] of Object.entries(routes)) { |
| 16 | + let pattern: PatternElement[] = key.split(/\s/).map((name) => { |
| 17 | + if (name.startsWith(":")) { |
| 18 | + return { type: "dynamic", required: true, name: name.slice(1) }; |
| 19 | + } else { |
| 20 | + return { type: "static", value: name }; |
| 21 | + } |
| 22 | + }); |
| 23 | + let segments: Record<string, string> = {}; |
| 24 | + let rest = args.slice(); |
| 25 | + let copy = shellwords.slice(); |
| 26 | + for (let element of pattern) { |
| 27 | + let top = copy.shift(); |
| 28 | + |
| 29 | + if (element.type === "dynamic") { |
| 30 | + if (top) { |
| 31 | + segments[element.name] = String(top); |
| 32 | + rest.splice(rest.indexOf(String(top), 1)); |
| 33 | + matched = matched.concat(element.name); |
| 34 | + } |
| 35 | + } else if (element.value !== top) { |
| 36 | + continue match; |
| 37 | + } else { |
| 38 | + rest.splice(rest.indexOf(element.value), 1); |
| 39 | + matched = matched.concat(element.value); |
| 40 | + } |
| 41 | + } |
| 42 | + let [handler, subroutes] = Array.isArray(value) ? value : [value]; |
| 43 | + |
| 44 | + let helpful = helpify(handler); |
| 45 | + |
| 46 | + let parseOptions = routeOptions(helpful); |
| 47 | + |
| 48 | + let flags = parseFlags(rest, parseOptions); |
| 49 | + |
| 50 | + let children = subroutes |
| 51 | + ? dispatch(flags._.map(String), subroutes, matched) |
| 52 | + : { *[Symbol.iterator]() {} }; |
| 53 | + |
| 54 | + let props: RouteProps = { |
| 55 | + flags, |
| 56 | + route: helpful, |
| 57 | + children, |
| 58 | + segments, |
| 59 | + }; |
| 60 | + |
| 61 | + return yield* helpful.handle(props); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function routeOptions(route: Route): ParseOptions { |
| 66 | + return route.options.reduce((options, spec) => { |
| 67 | + options[spec.type].push(spec.name); |
| 68 | + if (spec.alias) { |
| 69 | + options.alias[spec.name] = spec.alias; |
| 70 | + } |
| 71 | + return options; |
| 72 | + }, { |
| 73 | + boolean: [] as string[], |
| 74 | + number: [] as string[], |
| 75 | + string: [] as string[], |
| 76 | + alias: {} as Record<string, string>, |
| 77 | + stopEarly: true, |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +function helpify(route: Route): Route { |
| 82 | + let options = route.options.slice(); |
| 83 | + options.splice(0, 0, { |
| 84 | + type: "boolean", |
| 85 | + name: "help", |
| 86 | + alias: "h", |
| 87 | + description: "Print help information", |
| 88 | + }); |
| 89 | + return { |
| 90 | + ...route, |
| 91 | + options, |
| 92 | + *handle(props) { |
| 93 | + if (props.flags.help) { |
| 94 | + printHelp(props.route); |
| 95 | + } else { |
| 96 | + yield* route.handle(props); |
| 97 | + } |
| 98 | + }, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +export type PatternElement = { |
| 103 | + type: "dynamic"; |
| 104 | + name: string; |
| 105 | + required: boolean; |
| 106 | +} | { |
| 107 | + type: "static"; |
| 108 | + value: string; |
| 109 | +}; |
| 110 | + |
| 111 | +export interface RouteProps<TFlags = unknown> { |
| 112 | + flags: TFlags; |
| 113 | + route: Route; |
| 114 | + segments: Record<string, string>; |
| 115 | + children: Operation<void>; |
| 116 | +} |
| 117 | + |
| 118 | +export interface Route { |
| 119 | + options: { |
| 120 | + type: "boolean" | "string" | "number"; |
| 121 | + name: string; |
| 122 | + alias?: string; |
| 123 | + description: string; |
| 124 | + }[]; |
| 125 | + help: { |
| 126 | + HEAD: string; |
| 127 | + USAGE: string; |
| 128 | + }; |
| 129 | + handle(props: RouteProps): Operation<void>; |
| 130 | +} |
| 131 | + |
| 132 | +export type Routes = Record<string, Route | [Route, Routes]>; |
| 133 | + |
| 134 | +function printHelp(route: Route): void { |
| 135 | + let optionsTable = route.options.map((opt) => { |
| 136 | + let line = new Array(80).fill(" ", 0, 79); |
| 137 | + if (opt.alias) { |
| 138 | + line.splice(4, 3, ...`-${opt.alias},`); |
| 139 | + } |
| 140 | + line.splice(8, opt.name.length + 2, ...`--${opt.name}`); |
| 141 | + line.splice(20, opt.description.length, ...opt.description); |
| 142 | + return line.join(""); |
| 143 | + }).join("\n"); |
| 144 | + console.log(`${route.help.HEAD}\n`); |
| 145 | + console.log(`USAGE:\n ${route.help.USAGE}\n`); |
| 146 | + console.log(`OPTIONS:\n${optionsTable}`); |
| 147 | +} |
0 commit comments