MCPcopy
hub / github.com/midwayjs/midway / loadCommands

Method loadCommands

packages/commander/src/framework.ts:160–282  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

158 }
159
160 private loadCommands() {
161 if (this.isCommandsLoaded) {
162 return;
163 }
164 const modules = DecoratorManager.listModule(CLI_COMMAND_KEY);
165 for (const module of modules) {
166 const metadata: CliCommandOptions = MetadataManager.getMetadata(
167 CLI_COMMAND_KEY,
168 module
169 );
170
171 const cmd = this.program.command(metadata.name);
172 if (metadata.arguments) {
173 cmd.arguments(metadata.arguments);
174 }
175 if (metadata.description) {
176 cmd.description(metadata.description, metadata.argsDescription);
177 }
178 if (metadata.aliases) {
179 metadata.aliases.forEach(alias => cmd.alias(alias));
180 }
181
182 const optionMetadataList: Array<{
183 propertyKey: string;
184 options: CliOptionOptions;
185 }> = MetadataManager.getMetadata(CLI_OPTION_KEY, module) || [];
186
187 for (const optMeta of optionMetadataList) {
188 const opt = optMeta.options;
189 let parser;
190 if (optMeta.propertyKey) {
191 parser = module.prototype[optMeta.propertyKey];
192 }
193
194 if (opt.required) {
195 if (parser) {
196 cmd.requiredOption(
197 opt.flags,
198 opt.description,
199 parser,
200 opt.defaultValue
201 );
202 } else {
203 cmd.requiredOption(opt.flags, opt.description, opt.defaultValue);
204 }
205 } else {
206 if (parser) {
207 cmd.option(opt.flags, opt.description, parser, opt.defaultValue);
208 } else {
209 cmd.option(opt.flags, opt.description, opt.defaultValue);
210 }
211 }
212 }
213
214 cmd.action(async (...args: any[]) => {
215 const commandObj = args[args.length - 1];
216 const actualArgs = args.slice(0, -2);
217 const actualOptions = commandObj.opts();

Callers 2

runMethod · 0.95
runCommandMethod · 0.95

Calls 10

outputResultMethod · 0.95
listModuleMethod · 0.80
getMetadataMethod · 0.80
runWithEntrySpanMethod · 0.80
getMethod · 0.65
applyMiddlewareMethod · 0.65
getAsyncMethod · 0.65
runMethod · 0.65
fnFunction · 0.50

Tested by

no test coverage detected