| 1 | import { Subcommand, convertSubcommand, Initializer } from "./convert"; |
| 2 | import { makeArray, SpecLocationSource } from "./utils"; |
| 3 | |
| 4 | // eslint-disable-next-line @typescript-eslint/ban-types |
| 5 | type FigLoadSpecFn = Fig.LoadSpec extends infer U ? (U extends Function ? U : never) : never; |
| 6 | export type LoadSpec<ArgT = ArgMeta, OptionT = OptionMeta, SubcommandT = SubcommandMeta> = |
| 7 | | Fig.SpecLocation[] |
| 8 | | Subcommand<ArgT, OptionT, SubcommandT> |
| 9 | | (( |
| 10 | ...args: Parameters<FigLoadSpecFn> |
| 11 | ) => Promise<Fig.SpecLocation[] | Subcommand<ArgT, OptionT, SubcommandT>>); |
| 12 | |
| 13 | export type OptionMeta = Omit<Fig.Option, "args" | "name">; |
| 14 | export type ArgMeta = Omit<Fig.Arg, "template" | "generators" | "loadSpec"> & { |
| 15 | generators: Fig.Generator[]; |
| 16 | loadSpec?: LoadSpec<ArgMeta, OptionMeta, SubcommandMeta>; |
| 17 | }; |
| 18 | |
| 19 | type SubcommandMetaExcludes = |
| 20 | | "subcommands" |
| 21 | | "options" |
| 22 | | "loadSpec" |
| 23 | | "persistentOptions" |
| 24 | | "args" |
| 25 | | "name"; |
| 26 | export type SubcommandMeta = Omit<Fig.Subcommand, SubcommandMetaExcludes> & { |
| 27 | loadSpec?: LoadSpec<ArgMeta, OptionMeta, SubcommandMeta>; |
| 28 | }; |
| 29 | |
| 30 | export function convertLoadSpec<ArgT, OptionT, SubcommandT>( |
| 31 | loadSpec: Fig.LoadSpec, |
| 32 | initialize: Initializer<ArgT, OptionT, SubcommandT> |
| 33 | ): LoadSpec<ArgT, OptionT, SubcommandT> { |
| 34 | if (typeof loadSpec === "string") { |
| 35 | return [{ name: loadSpec, type: SpecLocationSource.GLOBAL }]; |
| 36 | } |
| 37 | |
| 38 | if (typeof loadSpec === "function") { |
| 39 | return (...args) => |
| 40 | loadSpec(...args).then((result) => { |
| 41 | if (Array.isArray(result)) { |
| 42 | return result; |
| 43 | } |
| 44 | if ("type" in result) { |
| 45 | return [result]; |
| 46 | } |
| 47 | // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 48 | return convertSubcommand(result, initialize); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 53 | return convertSubcommand(loadSpec, initialize); |
| 54 | } |
| 55 | |
| 56 | function initializeOptionMeta(option: Fig.Option): OptionMeta { |
| 57 | return option; |
| 58 | } |
| 59 | |
| 60 | // Default initialization functions: |
| 61 | function initializeArgMeta(arg: Fig.Arg): ArgMeta { |
| 62 | const { template, ...rest } = arg; |
| 63 | const generators = template ? [{ template }] : makeArray(arg.generators ?? []); |
| 64 | return { |
| 65 | ...rest, |
| 66 | loadSpec: arg.loadSpec |
| 67 | ? convertLoadSpec(arg.loadSpec, { |
| 68 | option: initializeOptionMeta, |
| 69 | // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 70 | subcommand: initializeSubcommandMeta, |
| 71 | arg: initializeArgMeta, |
| 72 | }) |
| 73 | : undefined, |
| 74 | generators: generators.map((generator) => { |
| 75 | let { trigger, getQueryTerm } = generator; |
| 76 | if (generator.template) { |
| 77 | const templates = makeArray(generator.template); |
| 78 | if (templates.includes("folders") || templates.includes("filepaths")) { |
| 79 | trigger = trigger ?? "/"; |
| 80 | getQueryTerm = getQueryTerm ?? "/"; |
| 81 | } |
| 82 | } |
| 83 | return { ...generator, trigger, getQueryTerm }; |
| 84 | }), |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | function initializeSubcommandMeta(subcommand: Fig.Subcommand): SubcommandMeta { |
| 89 | return { |
| 90 | ...subcommand, |
| 91 | loadSpec: subcommand.loadSpec |
| 92 | ? convertLoadSpec(subcommand.loadSpec, { |
| 93 | subcommand: initializeSubcommandMeta, |
| 94 | option: initializeOptionMeta, |
| 95 | arg: initializeArgMeta, |
| 96 | }) |
| 97 | : undefined, |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | export const initializeDefault: Initializer<ArgMeta, OptionMeta, SubcommandMeta> = { |
| 102 | subcommand: initializeSubcommandMeta, |
| 103 | option: initializeOptionMeta, |
| 104 | arg: initializeArgMeta, |
| 105 | }; |