1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const commander = require('commander'); 17const toolboxConfig = require('./toolbox.config'); 18class ToolBoxCommander { 19 program = new commander.Command(); 20 constructor() {} 21 22 addPluginCommand(plugin) { 23 const pluginOption = plugin.getPluginOptions(); 24 if (!pluginOption) { 25 return; 26 } 27 const pluginCommand = this.program.name(pluginOption.name) 28 .description(pluginOption.description) 29 .version(pluginOption.version) 30 .action((opts) => { 31 plugin.start(opts); 32 plugin.stop(); 33 }); 34 pluginOption.commands.forEach((command) => { 35 if (command.isRequiredOption) { 36 pluginCommand.requiredOption(...command.options); 37 } else { 38 pluginCommand.option(...command.options); 39 } 40 }); 41 } 42 43 buildCommands() { 44 this.program.parse(); 45 } 46} 47class ToolboxEntry { 48 commandBuilder; 49 constructor() { 50 this.commandBuilder = new ToolBoxCommander(); 51 } 52 53 runPlugins() { 54 const configuration = toolboxConfig.getToolConfiguration(); 55 configuration.plugins.forEach((plugin) => { 56 this.commandBuilder.addPluginCommand(plugin); 57 }); 58 this.commandBuilder.buildCommands(); 59 } 60} 61function main() { 62 const entry = new ToolboxEntry(); 63 entry.runPlugins(); 64} 65main();