1- import fs = require ( "fs" ) ;
2- import path = require ( "path" ) ;
3- import errors = require ( "./errors" ) ;
4- import options = require ( "./options" ) ;
1+ import * as fs from "fs" ;
2+ import * as path from "path" ;
3+ import * as errors from "./errors" ;
4+ import * as options from "./options" ;
55
66export class CommandExecutor implements ICommandExecutor {
7-
8- public execute ( ) : void {
9- var commandName = this . getCommandName ( ) ;
10- var commandArguments = this . getCommandArguments ( ) ;
11-
12- return this . executeCore ( commandName , commandArguments ) ;
13- }
14-
15- private executeCore ( commandName : string , commandArguments : string [ ] ) : void {
16- try {
17- let filePath = path . join ( __dirname , "commands" , commandName + ".js" ) ;
18- if ( fs . existsSync ( filePath ) ) {
19- var command : ICommand = new ( require ( filePath ) . Command ) ( ) ;
20- if ( ! command ) {
21- errors . fail ( "Unable to resolve commandName %s" , commandName ) ;
22- }
23-
24- command . execute ( commandArguments ) ;
25- }
26-
27- } catch ( e ) {
28- if ( options . debug ) {
29- throw e ;
30- } else {
31- console . log ( "\x1B[31;1m" + e . message + "\x1B[0m" ) ;
32- }
33- }
34- }
35-
36- private getCommandArguments ( ) : string [ ] {
37- var remaining = options . _ ;
38- return remaining . length > 1 ? remaining . slice ( 1 ) : [ ] ;
39- }
40-
41- private getCommandName ( ) : string {
42- var remaining = options . _ ;
43- return remaining . length > 0 ? remaining [ 0 ] . toLowerCase ( ) : "help" ;
44- }
45- }
7+ public execute ( ) : void {
8+ var commandName = this . getCommandName ( ) ;
9+ var commandArguments = this . getCommandArguments ( ) ;
10+
11+ return this . executeCore ( commandName , commandArguments ) ;
12+ }
13+
14+ private executeCore ( commandName : string , commandArguments : string [ ] ) : void {
15+ try {
16+ let filePath = path . join ( __dirname , "commands" , commandName + ".js" ) ;
17+ if ( fs . existsSync ( filePath ) ) {
18+ var command : ICommand = new ( require ( filePath ) . Command ) ( ) ;
19+ if ( ! command ) {
20+ errors . fail ( "Unable to resolve commandName %s" , commandName ) ;
21+ }
22+
23+ command . execute ( commandArguments ) ;
24+ }
25+ } catch ( e ) {
26+ if ( options . debug ) {
27+ throw e ;
28+ } else {
29+ console . log ( "\x1B[31;1m" + e . message + "\x1B[0m" ) ;
30+ }
31+ }
32+ }
33+
34+ private getCommandArguments ( ) : string [ ] {
35+ var remaining = options . _ ;
36+ return remaining . length > 1 ? remaining . slice ( 1 ) : [ ] ;
37+ }
38+
39+ private getCommandName ( ) : string {
40+ var remaining = options . _ ;
41+ return remaining . length > 0 ? remaining [ 0 ] . toLowerCase ( ) : "help" ;
42+ }
43+ }
0 commit comments