| 1 | import { makeArray } from "./utils"; |
| 2 | |
| 3 | export type SpecMixin = |
| 4 | | Fig.Subcommand |
| 5 | | ((currentSpec: Fig.Subcommand, context: Fig.ShellContext) => Fig.Subcommand); |
| 6 | |
| 7 | type NamedObject = { name: Fig.SingleOrArray<string> }; |
| 8 | |
| 9 | const concatArrays = <T>(a: T[] | undefined, b: T[] | undefined): T[] | undefined => |
| 10 | a && b ? [...a, ...b] : a || b; |
| 11 | |
| 12 | const mergeNames = <T = string>(a: T | T[], b: T | T[]): T | T[] => [ |
| 13 | ...new Set(concatArrays(makeArray(a), makeArray(b))), |
| 14 | ]; |
| 15 | |
| 16 | const mergeArrays = <T>(a: T[] | undefined, b: T[] | undefined): T[] | undefined => |
| 17 | a && b ? [...new Set(concatArrays(makeArray(a), makeArray(b)))] : a || b; |
| 18 | |
| 19 | const mergeArgs = (arg: Fig.Arg, partial: Fig.Arg): Fig.Arg => ({ |
| 20 | ...arg, |
| 21 | ...partial, |
| 22 | suggestions: concatArrays<Fig.Suggestion | string>(arg.suggestions, partial.suggestions), |
| 23 | generators: |
| 24 | arg.generators && partial.generators |
| 25 | ? concatArrays(makeArray(arg.generators), makeArray(partial.generators)) |
| 26 | : arg.generators || partial.generators, |
| 27 | template: |
| 28 | arg.template && partial.template |
| 29 | ? mergeNames<Fig.TemplateStrings>(arg.template, partial.template) |
| 30 | : arg.template || partial.template, |
| 31 | }); |
| 32 | |
| 33 | const mergeArgArrays = ( |
| 34 | args: Fig.SingleOrArray<Fig.Arg> | undefined, |
| 35 | partials: Fig.SingleOrArray<Fig.Arg> | undefined |
| 36 | ): Fig.SingleOrArray<Fig.Arg> | undefined => { |
| 37 | if (!args || !partials) { |
| 38 | return args || partials; |
| 39 | } |
| 40 | const argArray = makeArray(args); |
| 41 | const partialArray = makeArray(partials); |
| 42 | const result = []; |
| 43 | for (let i = 0; i < Math.max(argArray.length, partialArray.length); i += 1) { |
| 44 | const arg = argArray[i]; |
| 45 | const partial = partialArray[i]; |
| 46 | if (arg !== undefined && partial !== undefined) { |
| 47 | result.push(mergeArgs(arg, partial)); |
| 48 | } else if (partial !== undefined || arg !== undefined) { |
| 49 | result.push(arg || partial); |
| 50 | } |
| 51 | } |
| 52 | return result.length === 1 ? result[0] : result; |
| 53 | }; |
| 54 | |
| 55 | const mergeOptions = (option: Fig.Option, partial: Fig.Option): Fig.Option => ({ |
| 56 | ...option, |
| 57 | ...partial, |
| 58 | name: mergeNames(option.name, partial.name), |
| 59 | args: mergeArgArrays(option.args, partial.args), |
| 60 | exclusiveOn: mergeArrays(option.exclusiveOn, partial.exclusiveOn), |
| 61 | dependsOn: mergeArrays(option.dependsOn, partial.dependsOn), |
| 62 | }); |
| 63 | |
| 64 | const mergeNamedObjectArrays = <T extends NamedObject>( |
| 65 | objects: T[] | undefined, |
| 66 | partials: T[] | undefined, |
| 67 | mergeItems: (a: T, b: T) => T |
| 68 | ): T[] | undefined => { |
| 69 | if (!objects || !partials) { |
| 70 | return objects || partials; |
| 71 | } |
| 72 | const mergedObjects = objects ? [...objects] : []; |
| 73 | |
| 74 | const existingNameIndexMap: Record<string, number> = {}; |
| 75 | for (let i = 0; i < objects.length; i += 1) { |
| 76 | makeArray(objects[i].name).forEach((name) => { |
| 77 | existingNameIndexMap[name] = i; |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | for (let i = 0; i < partials.length; i += 1) { |
| 82 | const partial = partials[i]; |
| 83 | if (!partial) { |
| 84 | throw new Error("Invalid object passed to merge"); |
| 85 | } |
| 86 | const existingNames = makeArray(partial.name).filter((name) => name in existingNameIndexMap); |
| 87 | if (existingNames.length === 0) { |
| 88 | mergedObjects.push(partial); |
| 89 | } else { |
| 90 | const index = existingNameIndexMap[existingNames[0]]; |
| 91 | if (existingNames.some((name) => existingNameIndexMap[name] !== index)) { |
| 92 | throw new Error("Names provided for option matched multiple existing options"); |
| 93 | } |
| 94 | mergedObjects[index] = mergeItems(mergedObjects[index], partial); |
| 95 | } |
| 96 | } |
| 97 | return mergedObjects; |
| 98 | }; |
| 99 | |
| 100 | function mergeOptionArrays( |
| 101 | options: Fig.Option[] | undefined, |
| 102 | partials: Fig.Option[] | undefined |
| 103 | ): Fig.Option[] | undefined { |
| 104 | return mergeNamedObjectArrays(options, partials, mergeOptions); |
| 105 | } |
| 106 | |
| 107 | function mergeSubcommandArrays( |
| 108 | subcommands: Fig.Subcommand[] | undefined, |
| 109 | partials: Fig.Subcommand[] | undefined |
| 110 | ): Fig.Subcommand[] | undefined { |
| 111 | // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 112 | return mergeNamedObjectArrays(subcommands, partials, mergeSubcommands); |
| 113 | } |
| 114 | |
| 115 | export function mergeSubcommands( |
| 116 | subcommand: Fig.Subcommand, |
| 117 | partial: Fig.Subcommand |
| 118 | ): Fig.Subcommand { |
| 119 | return { |
| 120 | ...subcommand, |
| 121 | ...partial, |
| 122 | name: mergeNames(subcommand.name, partial.name), |
| 123 | args: mergeArgArrays(subcommand.args, partial.args), |
| 124 | additionalSuggestions: concatArrays<Fig.Suggestion | string>( |
| 125 | subcommand.additionalSuggestions, |
| 126 | partial.additionalSuggestions |
| 127 | ), |
| 128 | subcommands: mergeSubcommandArrays(subcommand.subcommands, partial.subcommands), |
| 129 | options: mergeOptionArrays(subcommand.options, partial.options), |
| 130 | parserDirectives: |
| 131 | subcommand.parserDirectives && partial.parserDirectives |
| 132 | ? { ...subcommand.parserDirectives, ...partial.parserDirectives } |
| 133 | : subcommand.parserDirectives || partial.parserDirectives, |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | export const applyMixin = ( |
| 138 | spec: Fig.Subcommand, |
| 139 | context: Fig.ShellContext, |
| 140 | mixin: SpecMixin |
| 141 | ): Fig.Subcommand => { |
| 142 | if (typeof mixin === "function") { |
| 143 | return mixin(spec, context); |
| 144 | } |
| 145 | const partial = mixin; |
| 146 | return mergeSubcommands(spec, partial); |
| 147 | }; |