/* Copyright IBM Corp. 2017 */ import { Options } from './../options'; import { Command } from './command'; import { CreateLayoutCommand } from './create.layout.command'; import { EmptyCommand } from './empty'; import { CreateReactCommand } from './create.react.command'; import { CreateVueCommand } from './create.vue.command'; interface CommandConstructor { new(opts: Options): Command; } interface CommandByType { [type: string]: CommandConstructor; } interface Commands { [cmd: string]: CommandByType; } function _register(aCommand: string, aType: string, aConstructor: CommandConstructor, aMap: Commands): void { // types const byType = aMap[aCommand] || (aMap[aCommand] = {}); byType[aType] = aConstructor; } export class CommandFactory { private commands: Commands = {}; constructor() { const cmd = this.commands; _register(CreateReactCommand.COMMAND, CreateReactCommand.TYPE, CreateReactCommand, cmd); _register(CreateVueCommand.COMMAND, CreateVueCommand.TYPE, CreateVueCommand, cmd); // for eric's testing workflow _register(CreateLayoutCommand.COMMAND, CreateLayoutCommand.TYPE, CreateLayoutCommand, cmd); } create(options: Options): Command { // get the type const cmd = options.command || 'help'; const type = options.commandType || 'help'; // lookup const byType = this.commands[cmd] || {}; const creator: CommandConstructor = byType[type] || EmptyCommand; // construct the command return new creator(options); } }