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 addPluginCommand(plugin) { 22 const pluginOption = plugin.pluginOptions; 23 if (!pluginOption) { 24 return; 25 } 26 const pluginCommand = this.program.name(pluginOption.name) 27 .description(pluginOption.description) 28 .version(pluginOption.version) 29 .action((opts) => { 30 plugin.start(opts); 31 plugin.stop(); 32 }); 33 pluginOption.commands.forEach((command) => { 34 if (command.isRequiredOption) { 35 pluginCommand.requiredOption(...command.options); 36 } else { 37 pluginCommand.option(...command.options); 38 } 39 }); 40 } 41 buildCommands() { 42 this.program.parse(); 43 } 44} 45class ToolboxEntry { 46 commandBuilder; 47 constructor() { 48 this.commandBuilder = new ToolBoxCommander(); 49 } 50 runPlugins() { 51 const configuration = toolboxConfig.getToolConfiguration(); 52 configuration.plugins.forEach((plugin) => { 53 this.commandBuilder.addPluginCommand(plugin); 54 }); 55 this.commandBuilder.buildCommands(); 56 } 57} 58function main() { 59 const entry = new ToolboxEntry(); 60 entry.runPlugins(); 61} 62main(); 63