|
| 1 | +--- |
| 2 | +import { z } from "astro:schema"; |
| 3 | +import { PackageManagers } from "starlight-package-managers"; |
| 4 | +import { commands, getCommand } from "~/util/wrangler"; |
| 5 | +import WranglerArg from "./WranglerArg.astro"; |
| 6 | +import Details from "./Details.astro"; |
| 7 | +
|
| 8 | +function validateArg(value: any, expected: string): boolean { |
| 9 | + if (Array.isArray(expected)) { |
| 10 | + for (const choice of expected) { |
| 11 | + if (value === choice) { |
| 12 | + return true; |
| 13 | + } |
| 14 | + } |
| 15 | +
|
| 16 | + return false; |
| 17 | + } |
| 18 | +
|
| 19 | + return typeof value === expected; |
| 20 | +} |
| 21 | +
|
| 22 | +type Props = z.input<typeof props>; |
| 23 | +
|
| 24 | +const props = z.object({ |
| 25 | + command: z.string(), |
| 26 | + positionals: z.array(z.string()).optional(), |
| 27 | + flags: z.record(z.string(), z.any()).optional(), |
| 28 | + showArgs: z.boolean().default(false), |
| 29 | +}); |
| 30 | +
|
| 31 | +const { command, positionals, flags, showArgs } = props.parse(Astro.props); |
| 32 | +
|
| 33 | +const definition = getCommand(command); |
| 34 | +
|
| 35 | +const { globalFlags } = commands; |
| 36 | +
|
| 37 | +let args = []; |
| 38 | +
|
| 39 | +if (flags) { |
| 40 | + for (const [key, value] of Object.entries(flags)) { |
| 41 | + const flagDef = definition.args?.[key]; |
| 42 | +
|
| 43 | + if (!flagDef) { |
| 44 | + throw new Error( |
| 45 | + `[WranglerCLI] Received "${key}" for "${command}" but no such arg exists`, |
| 46 | + ); |
| 47 | + } |
| 48 | +
|
| 49 | + const type = flagDef.type ?? flagDef.choices; |
| 50 | + const valid = validateArg(value, type); |
| 51 | +
|
| 52 | + if (!valid) { |
| 53 | + throw new Error( |
| 54 | + `[WranglerCLI] Expected "${type}" for "${key}" but got "${typeof value}"`, |
| 55 | + ); |
| 56 | + } |
| 57 | +
|
| 58 | + args.push(...[`--${key}`, value]); |
| 59 | + } |
| 60 | +} |
| 61 | +
|
| 62 | +if (positionals) { |
| 63 | + const positionalsDef = definition.positionalArgs ?? []; |
| 64 | +
|
| 65 | + if (positionalsDef.length === 0) { |
| 66 | + throw new Error( |
| 67 | + `[WranglerCLI] Expected 0 positional arguments for "${command}" but received ${positionals.length}`, |
| 68 | + ); |
| 69 | + } |
| 70 | +
|
| 71 | + args.push(...positionals); |
| 72 | +} |
| 73 | +--- |
| 74 | + |
| 75 | +<PackageManagers |
| 76 | + pkg="wrangler" |
| 77 | + type="exec" |
| 78 | + args={`${command} ${args.join(" ")}`} |
| 79 | +/> |
| 80 | + |
| 81 | +{ |
| 82 | + showArgs && definition.args && ( |
| 83 | + <Details header="Arguments"> |
| 84 | + <p> |
| 85 | + <strong>Command flags</strong> |
| 86 | + </p> |
| 87 | + <ul> |
| 88 | + {Object.entries(definition.args) |
| 89 | + .filter(([_, value]) => !value.hidden) |
| 90 | + .map(([key, value]) => { |
| 91 | + return <WranglerArg key={key} definition={value} />; |
| 92 | + })} |
| 93 | + </ul> |
| 94 | + |
| 95 | + <p> |
| 96 | + <strong>Global flags</strong> |
| 97 | + </p> |
| 98 | + <ul> |
| 99 | + {Object.entries(globalFlags).map(([key, value]) => { |
| 100 | + return <WranglerArg key={key} definition={value} />; |
| 101 | + })} |
| 102 | + </ul> |
| 103 | + </Details> |
| 104 | + ) |
| 105 | +} |
0 commit comments