|
| 1 | +const _ = require("lodash"); |
| 2 | +const { root_command, skip_command } = require("./constants"); |
| 3 | +const { parseArgs } = require("./parse-args"); |
| 4 | +const didYouMean = require("didyoumean"); |
| 5 | + |
| 6 | +didYouMean.threshold = 0.5; |
| 7 | + |
| 8 | +const execute = (params, commands, instance) => { |
| 9 | + const args = parseArgs(params.args, params.from); |
| 10 | + |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + const { command, usageOptions, error } = processArgs(commands, args); |
| 13 | + |
| 14 | + if (error) { |
| 15 | + reject(new Error(error)); |
| 16 | + } |
| 17 | + |
| 18 | + if (!usageOptions.length && command.name === root_command) { |
| 19 | + usageOptions.push(command.options.find((option) => option.flags.name === "help")); |
| 20 | + } |
| 21 | + |
| 22 | + const operationOptions = usageOptions.filter((option) => option.operation); |
| 23 | + if (operationOptions.length) { |
| 24 | + operationOptions[0].operation(); |
| 25 | + resolve({ |
| 26 | + command: skip_command, |
| 27 | + options: {}, |
| 28 | + }); |
| 29 | + return; |
| 30 | + } else { |
| 31 | + let error = ""; |
| 32 | + |
| 33 | + const processUserOptionData = (data, option) => { |
| 34 | + if (!data.length && !option.flags.value) { |
| 35 | + return !option.flags.isNoFlag; |
| 36 | + } |
| 37 | + if (option.flags.value) { |
| 38 | + if (option.flags.value.variadic) { |
| 39 | + return data.reduce((acc, d) => { |
| 40 | + acc.push(...d.split(",").map(option.flags.value.formatter)); |
| 41 | + return acc; |
| 42 | + }, []); |
| 43 | + } else { |
| 44 | + return option.flags.value.formatter(data[0] || option.default); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return option.default; |
| 49 | + }; |
| 50 | + |
| 51 | + const parsedOptionsObject = command.options.reduce((acc, option) => { |
| 52 | + if (error) return acc; |
| 53 | + |
| 54 | + const userOption = usageOptions.find((o) => o.flags.name === option.flags.name); |
| 55 | + |
| 56 | + if (!userOption && option.required) { |
| 57 | + error = `required option '${option.flags.raw}' not specified`; |
| 58 | + return acc; |
| 59 | + } |
| 60 | + |
| 61 | + if (userOption) { |
| 62 | + acc[option.flags.name] = processUserOptionData(userOption.$data, option); |
| 63 | + } else { |
| 64 | + acc[option.flags.name] = option.default; |
| 65 | + } |
| 66 | + |
| 67 | + return acc; |
| 68 | + }, {}); |
| 69 | + |
| 70 | + if (error) { |
| 71 | + reject(new Error(error)); |
| 72 | + } else { |
| 73 | + resolve({ |
| 74 | + command: command.name === root_command ? null : command.name, |
| 75 | + options: parsedOptionsObject, |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | +}; |
| 81 | + |
| 82 | +const processArgs = (commands, args) => { |
| 83 | + let command = null; |
| 84 | + let usageOptions = []; |
| 85 | + let walkingOption = null; |
| 86 | + let error = ""; |
| 87 | + |
| 88 | + let allFlagKeys = []; |
| 89 | + |
| 90 | + _.forEach(args, (arg, i) => { |
| 91 | + if (error) return; |
| 92 | + |
| 93 | + if (i === 0) { |
| 94 | + command = commands[arg] || commands[root_command]; |
| 95 | + allFlagKeys = command.options.reduce((acc, option) => [...acc, ...option.flags.keys], []); |
| 96 | + } |
| 97 | + if (arg.startsWith("-")) { |
| 98 | + const option = command.options.find((option) => option.flags.keys.includes(arg)); |
| 99 | + |
| 100 | + if (!option) { |
| 101 | + const tip = didYouMean(arg, allFlagKeys); |
| 102 | + error = `unknown option ${arg}${tip ? `\n(Did you mean ${tip} ?)` : ""}`; |
| 103 | + } |
| 104 | + |
| 105 | + if (option) { |
| 106 | + if (walkingOption && walkingOption.flags.name === option.flags.name) { |
| 107 | + return; |
| 108 | + } |
| 109 | + const existedOption = usageOptions.find((o) => o.flags.name === option.flags.name); |
| 110 | + if (existedOption) { |
| 111 | + walkingOption = existedOption; |
| 112 | + } else { |
| 113 | + walkingOption = { |
| 114 | + ...option, |
| 115 | + $data: [], |
| 116 | + }; |
| 117 | + usageOptions.push(walkingOption); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (walkingOption) { |
| 125 | + walkingOption.$data.push(arg); |
| 126 | + } |
| 127 | + }); |
| 128 | + command = command || commands[root_command]; |
| 129 | + |
| 130 | + return { |
| 131 | + command, |
| 132 | + usageOptions, |
| 133 | + error, |
| 134 | + }; |
| 135 | +}; |
| 136 | + |
| 137 | +module.exports = { |
| 138 | + execute, |
| 139 | +}; |
0 commit comments