-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f45cf33
commit 17edd89
Showing
8 changed files
with
197 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/cli-application/internal-app-commands/cmd-request-cmd.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { iCliCommand } from "../../models/cli-command"; | ||
import { iCliCommandExecutor } from "../../models/cli-command-executor"; | ||
import { iCliCommandParam } from "../../models/cli-command-param"; | ||
import { iCliOutputter } from "../../models/cli-outputter"; | ||
import EventEmitter from "events"; | ||
import { EVENTS } from "../../events/events"; | ||
|
||
/** | ||
* This is an internal command to be used by the application to execute other commands | ||
*/ | ||
export class CommandRequestorCommand implements iCliCommand { | ||
name: string = "_request_cmd"; | ||
|
||
displayText: string = ""; | ||
|
||
tokens: string[] = []; | ||
|
||
requiredParams?: iCliCommandParam[] | undefined = [{ | ||
name: "cmdNameToken", | ||
displayText: "Enter the next command to execute" | ||
}]; | ||
|
||
constructor( | ||
private commands: iCliCommand[], | ||
private cmdExecutor: iCliCommandExecutor, | ||
private eventEmitter: EventEmitter | ||
) {} | ||
|
||
async execute(userParamsInput: { [key: string]: any; }, cliOutputter: iCliOutputter): Promise<void> { | ||
const cmdNameToken = userParamsInput.cmdNameToken.toLowerCase(); | ||
const cmd = this.commands.find(cmd => { | ||
return cmd.name === cmdNameToken || !!cmd.tokens.find(tkn => tkn === cmdNameToken) | ||
}); | ||
if (!cmd) { | ||
cliOutputter.pushError(`There is no command with the name or token ${cmdNameToken}`); | ||
return; | ||
} | ||
|
||
this.eventEmitter.emit(EVENTS.cmdDefinitionReceived, cmd); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/cli-application/internal-app-commands/multi-cmd-exec-cmd.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { iCliCommand } from "../../models/cli-command"; | ||
import { iCliCommandExecutor } from "../../models/cli-command-executor"; | ||
import { iCliCommandParam } from "../../models/cli-command-param"; | ||
import { iCliOutputter } from "../../models/cli-outputter"; | ||
import { iCliUserInputRequestor } from "../../models/cli-user-input-requestor"; | ||
|
||
/** | ||
* This is an internal command to be used by the application to execute other commands | ||
*/ | ||
export class MultiCommandExecutorCommand implements iCliCommand { | ||
name: string = "_multi_cmd"; | ||
|
||
displayText: string = ""; | ||
|
||
tokens: string[] = []; | ||
|
||
constructor( | ||
private commands: iCliCommand[], | ||
private cmdExecutor: iCliCommandExecutor, | ||
private cliUserInputRequestor: iCliUserInputRequestor | ||
) {} | ||
|
||
//we need to manually do the work of getting the commands + params | ||
async execute(userParamsInput: {}, cliOutputter: iCliOutputter): Promise<void> { | ||
const cmdNameToken = userParamsInput.cmdNameToken.toLowerCase(); | ||
const cmd = this.commands.find(cmd => { | ||
return cmd.name === cmdNameToken || !!cmd.tokens.find(tkn => tkn === cmdNameToken) | ||
}); | ||
if (!cmd) { | ||
cliOutputter.pushError(`There is no command with the name or token ${cmdNameToken}`); | ||
return; | ||
} | ||
|
||
await this.cmdExecutor.execute(cmd!); | ||
} | ||
|
||
private async getCmdParams(cliCmd: iCliCommand): Promise<{[key: string]: any}> { | ||
const paramsInput: any = {}; | ||
const params = cliCmd.requiredParams || []; | ||
for (let i = 0; i < params.length; i++) { | ||
const param = params[i]; | ||
const input = await this.cliUserInputRequestor.awaitInput(param); | ||
if (input[param.name]) { | ||
throw new Error( | ||
`CliCommandExecutor: multiple input params have the same name.` | ||
); | ||
} | ||
paramsInput[param.name] = input; | ||
} | ||
return paramsInput; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const EVENTS = { | ||
quitApp: "quitApp" | ||
quitApp: "quitApp", | ||
cmdDefinitionReceived: "cmdDefinitionReceived" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { iCliCommandParam } from "./cli-command-param"; | ||
|
||
export interface iCliUserInputRequestor { | ||
awaitInput(param: iCliCommandParam): Promise<any>; | ||
awaitInput(param: iCliCommandParam): Promise<any>; | ||
} |