Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | 1x 1x 1x 1x 1x 1x 1x 72x 72x 72x 72x 16x 16x 72x 16x 16x 72x 32x 32x 32x 32x 128x 24x 104x 24x 80x 80x 128x 32x 24x 24x 24x 40x 40x 40x 32x 32x 72x 72x 40x 16x 32x 32x 8x 24x 24x 16x 24x 16x 16x 24x 16x 16x 24x 16x 24x 16x 16x 24x 16x 24x 16x 2x 16x 40x 40x 8x 40x 40x 40x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 8x 8x 8x 8x 8x 8x 8x 8x | import _merge from "lodash/merge"; import _flatten from "lodash/flatten"; import _isString from "lodash/isString"; import { Prompt } from "makitso-prompt"; import { start } from "./command"; import { Context } from "./context"; import { YargsParserOptions, Argument, Command, Commands, Def, Option, Plugin, PluginSet } from "./types"; import { debug } from "./debug"; /** * * @param {String} arg - name and description * @returns {Object} arg */ function parseArgument(arg: string): Argument { const args = arg.split(/\s+/); let [name] = args; const [, ...description] = args; let isMulti; let isOptional; if (/^\[/.test(name)) { isOptional = true; name = name.replace(/[[\]]/g, ""); } if (/\.\.\.$/.test(name)) { isMulti = true; name = name.replace(/\.\.\.$/, ""); } return { name, description: description.join(" "), isMulti: Boolean(isMulti), isOptional: Boolean(isOptional), string: arg }; } /** * * @param {String} opt - flags and description * @returns {Object} opt */ function parseOption(opt: string) { const optBits = opt.split(/\s+/); debug({ optBits }); const initial: Option = { name: "", alias: "", description: "", parseOpt: { boolean: false } }; const option = optBits.reduce((result, val) => { if (/^--\w/.test(val)) { result.name = val.replace(/^--/, ""); } else if (/^-\w/.test(val)) { result.alias = val.replace(/^-/, ""); } else Iif (/^\{\w+}$/.test(val)) { const parseOpt = val.replace(/^\{/, "").replace(/}$/, ""); if (parseOpt === "bool" || parseOpt === "boolean") { result.parseOpt.boolean = true; } } else { result.description = `${result.description} ${val}`; } return result; }, initial); return option; } function optionsToArgsParserOpts(options: Option[]) { return options.reduce((Iresult = {}, opt) => { result[opt.name] = opt.alias; return result; }, {} as YargsParserOptions["alias"]); } function parseCommandDef(command: Command) { const def: Partial<Def> = command; def.argsLookup = {}; if (def.arguments) { def.args = def.arguments.map(parseArgument); def.argsLookup = def.args.reduce((lu, arg, idx) => { lu ? (lu[arg.name] = idx) : (lu = { [arg.name]: idx }); return lu; }, {} as Def["argsLookup"]); } if (def.options) { def.opts = def.options.reduce((opts, opt) => { // parse option and discard if name matches a positional arg const pOpt = parseOption(opt); if (def.argsLookup?.[pOpt.name] !== undefined) { return opts; } opts ? opts.push(pOpt) : (opts = [pOpt]); return opts; }, [] as Def["opts"]); def.optsLookup = def.opts?.reduce((lu, opt) => { if (opt.name) { Iif (!lu) lu = {}; lu[opt.name] = true; } if (opt.alias) { Iif (!lu) lu = {}; lu[opt.alias] = true; } return lu; }, {} as Def["optsLookup"]); def.aliasLookup = def.opts?.reduce((lu, opt) => { if (opt.alias) { Iif (!lu) lu = {}; lu[opt.alias] = opt.name; } return lu; }, {} as Def["aliasLookup"]); def.opts?.forEach(opt => { Iif (opt.parseOpt.boolean) { if (!def.argsParserOpts?.boolean) { def.argsParserOpts = { boolean: [opt.name] }; } else { def.argsParserOpts.boolean.push(opt.name); } } }); if (!def.argsParserOpts) { def.argsParserOpts = {}; } Eif (def.opts) def.argsParserOpts.alias = optionsToArgsParserOpts(def.opts); } debug({ def }); return def; } function parseCommandDefs(commands: Commands) { for (const key in commands) { // _forEach(commands, (def, key) => { const command = commands[key]; Iif (command.commands) { parseCommandDefs(command.commands); } else { commands[key] = parseCommandDef(command); } } } function mergeCommands(pluginSet: PluginSet, plugin: Plugin) { const commands = plugin.commands; Eif (commands) { parseCommandDefs(commands); _merge(pluginSet.commands, plugin.commands); } return pluginSet; } /** * Add a plugin to a plugin set * * @param {Object} pluginSet - registered plugins * @param {Object} pluginSet.schema - schema * @param {Object} pluginSet.stores - stores * @param {Object} pluginSet.commands - commands * @param {Object} plugin - plugin to register * @param {Object} plugin.schema - schema * @param {Object} plugin.stores - stores * @param {Object} plugin.commands - commands * @returns {Object} the updated pluginSet */ function addPlugin(pluginSet: PluginSet, plugin: Plugin) { Iif (plugin.schema) { _merge(pluginSet.schema, plugin.schema); } Iif (plugin.stores) { _merge(pluginSet.stores, plugin.stores); } Eif (plugin.commands) { pluginSet = mergeCommands(pluginSet, plugin); } return pluginSet; } /** * merge one or more plugins into a plugin set * * @param {Object[]|Object} plugins - a single plugin or list of plugins * @returns {Object} pluginSet */ function createPluginSet(plugins: Plugin | Plugin[]) { Eif (!Array.isArray(plugins)) { plugins = [plugins]; } return Promise.all(_flatten(plugins)).then(pluginList => { let pluginSet = { schema: {}, stores: {}, commands: {} }; pluginList.forEach(plugin => { pluginSet = addPlugin(pluginSet, plugin); }); return pluginSet; }); } /** * Start the Makitso app * * @param {Object} args - args * @param {Object} options - * @param {String|Prompt} [args.commandPrompt="Makitso>"] - prompt text or makitso-prompt instance * @returns {Promise} makitso */ type MakitsoArgs = { plugins: Plugin | Plugin[]; commandPrompt?: string | Prompt; options?: { app: { description: string; }; prompt: { message: string; }; }; }; export function Makitso(args: MakitsoArgs) { const { commandPrompt, plugins } = args; let prompt: Prompt; Eif (commandPrompt) { Iif (_isString(commandPrompt)) { prompt = new Prompt({ prompt: commandPrompt }); } else { prompt = commandPrompt; } } else { prompt = new Prompt(); } return createPluginSet(plugins).then(pluginSet => { const { schema, stores, commands } = pluginSet; const context = Context({ schema, stores, commands }); return start({ context, commands, prompt }); }); } |