| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  | import * as plugins from './smartcli.plugins.js'; | 
					
						
							| 
									
										
										
										
											2016-08-26 11:52:09 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | // interfaces
 | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  | export interface ICommandObservableObject { | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |   commandName: string; | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   subject: plugins.smartrx.rxjs.Subject<any>; | 
					
						
							| 
									
										
										
										
											2016-10-15 00:56:02 +02:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-10 03:10:21 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 20:25:17 +00:00
										 |  |  |  | const logger = new plugins.smartlog.ConsoleLog(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  | /** | 
					
						
							|  |  |  |  |  * class to create a new instance of Smartcli. Handles parsing of command line arguments. | 
					
						
							|  |  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-06-10 02:27:04 +02:00
										 |  |  |  | export class Smartcli { | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   /** | 
					
						
							|  |  |  |  |    * this Deferred contains the parsed result in the end | 
					
						
							|  |  |  |  |    */ | 
					
						
							|  |  |  |  |   public parseCompleted = plugins.smartpromise.defer<any>(); | 
					
						
							|  |  |  |  |    | 
					
						
							|  |  |  |  |    | 
					
						
							|  |  |  |  |   public version: string; | 
					
						
							| 
									
										
										
										
											2020-04-13 21:53:18 +00:00
										 |  |  |  |   private checkForEnvCliCall = true; | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |    * map of all Trigger/Observable objects to keep track | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   private commandObservableMap = new plugins.lik.ObjectMap<ICommandObservableObject>(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							|  |  |  |  |    * maps alias | 
					
						
							|  |  |  |  |    */ | 
					
						
							|  |  |  |  |   public aliasObject: {[key: string]: string[]} = {}; | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |    * The constructor of Smartcli | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   constructor() {} | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |   /** | 
					
						
							|  |  |  |  |    * halts any execution of commands if (process.env.CLI_CALL === false) | 
					
						
							|  |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-04-13 21:53:18 +00:00
										 |  |  |  |   disableEnvCliCall() { | 
					
						
							|  |  |  |  |     this.checkForEnvCliCall = false; | 
					
						
							| 
									
										
										
										
											2017-10-12 22:44:34 +02:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   /** | 
					
						
							|  |  |  |  |    * adds an alias, meaning one equals the other in terms of command execution. | 
					
						
							|  |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public addCommandAlias(originalArg, aliasArg): void { | 
					
						
							|  |  |  |  |     this.aliasObject[originalArg] = this.aliasObject[originalArg] || []; | 
					
						
							|  |  |  |  |     this.aliasObject[originalArg].push(aliasArg); | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							|  |  |  |  |    * adds a Command by returning a Promise that reacts to the specific commandString given. | 
					
						
							|  |  |  |  |    * Note: in e.g. "npm install something" the "install" is considered the command. | 
					
						
							|  |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public addCommand(commandNameArg: string): plugins.smartrx.rxjs.Subject<any> { | 
					
						
							|  |  |  |  |     let commandSubject: plugins.smartrx.rxjs.Subject<any>; | 
					
						
							|  |  |  |  |     const existingCommandSubject = this.getCommandSubject(commandNameArg); | 
					
						
							|  |  |  |  |     commandSubject = existingCommandSubject || new plugins.smartrx.rxjs.Subject<any>(); | 
					
						
							|  |  |  |  |      | 
					
						
							|  |  |  |  |     this.commandObservableMap.add({ | 
					
						
							|  |  |  |  |       commandName: commandNameArg, | 
					
						
							|  |  |  |  |       subject: commandSubject, | 
					
						
							|  |  |  |  |     }) | 
					
						
							|  |  |  |  |     return commandSubject; | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							|  |  |  |  |    * execute trigger by name | 
					
						
							|  |  |  |  |    * @param commandNameArg - the name of the command to trigger | 
					
						
							|  |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public triggerCommand(commandNameArg: string, argvObject: any) { | 
					
						
							|  |  |  |  |     const triggerSubject = this.getCommandSubject(commandNameArg); | 
					
						
							|  |  |  |  |     triggerSubject.next(argvObject); | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |     return triggerSubject; | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public getCommandSubject(commandNameArg: string) { | 
					
						
							|  |  |  |  |     const triggerObservableObject = this.commandObservableMap.findSync( | 
					
						
							| 
									
										
										
										
											2021-04-07 20:25:17 +00:00
										 |  |  |  |       (triggerObservableObjectArg) => { | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |         return triggerObservableObjectArg.commandName === commandNameArg; | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |       } | 
					
						
							|  |  |  |  |     ); | 
					
						
							|  |  |  |  |     if (triggerObservableObject) { | 
					
						
							|  |  |  |  |       return triggerObservableObject.subject; | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |       return null; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							|  |  |  |  |    * allows to specify help text to be printed above the rest of the help text | 
					
						
							|  |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public addHelp(optionsArg: { helpText: string }) { | 
					
						
							| 
									
										
										
										
											2021-04-07 20:25:17 +00:00
										 |  |  |  |     this.addCommand('help').subscribe((argvArg) => { | 
					
						
							|  |  |  |  |       logger.log('info', optionsArg.helpText); | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							|  |  |  |  |    * specify version to be printed for -v --version | 
					
						
							|  |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public addVersion(versionArg: string) { | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |     this.version = versionArg; | 
					
						
							|  |  |  |  |     this.addCommandAlias('v', 'version'); | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |     this.parseCompleted.promise.then(argv => { | 
					
						
							|  |  |  |  |       if (argv.v) { | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |         console.log(this.version); | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |    * adds a trigger that is called when no command is specified | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public standardCommand(): plugins.smartrx.rxjs.Subject<any> { | 
					
						
							|  |  |  |  |     const standardSubject = this.addCommand('standardCommand'); | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |     return standardSubject; | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |   /** | 
					
						
							|  |  |  |  |    * start the process of evaluating commands | 
					
						
							|  |  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |   public startParse(): void { | 
					
						
							|  |  |  |  |     // if we check for cli env calls, we might want to abort here.
 | 
					
						
							| 
									
										
										
										
											2020-04-13 21:53:18 +00:00
										 |  |  |  |     if (!process.env.CLI_CALL && this.checkForEnvCliCall) { | 
					
						
							| 
									
										
										
										
											2020-05-29 17:48:17 +00:00
										 |  |  |  |       console.log( | 
					
						
							|  |  |  |  |         `note: @pushrocks/smartcli: You called .startParse() on a SmartCli instance. However process.env.CLI_CALL being absent prevented parsing.` | 
					
						
							|  |  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2020-04-13 21:53:18 +00:00
										 |  |  |  |       return; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-03 17:00:36 +02:00
										 |  |  |  |     const parsedYArgs = plugins.yargsParser(process.argv); | 
					
						
							|  |  |  |  |      | 
					
						
							|  |  |  |  |     // lets handle commands
 | 
					
						
							|  |  |  |  |     let counter = 0; | 
					
						
							|  |  |  |  |     let foundCommand = false; | 
					
						
							|  |  |  |  |     parsedYArgs._.map((commandPartArg) => { | 
					
						
							|  |  |  |  |       counter ++; | 
					
						
							|  |  |  |  |       if (typeof commandPartArg === 'number') { | 
					
						
							|  |  |  |  |         return true; | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |       if (counter <= 2 && !foundCommand) { | 
					
						
							|  |  |  |  |         const isPath = commandPartArg.startsWith('/'); | 
					
						
							|  |  |  |  |         foundCommand = !isPath; | 
					
						
							|  |  |  |  |         return foundCommand; | 
					
						
							|  |  |  |  |       } else { | 
					
						
							|  |  |  |  |         return true; | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     }) | 
					
						
							|  |  |  |  |     for (const command of this.commandObservableMap.getArray()) { | 
					
						
							|  |  |  |  |       if (!parsedYArgs._[0]) { | 
					
						
							|  |  |  |  |         const standardCommand = this.commandObservableMap.findSync(commandArg => { | 
					
						
							|  |  |  |  |           return commandArg.commandName === "standardCommand"; | 
					
						
							|  |  |  |  |         }); | 
					
						
							|  |  |  |  |         if (standardCommand) { | 
					
						
							|  |  |  |  |           standardCommand.subject.next(parsedYArgs); | 
					
						
							|  |  |  |  |         } else { | 
					
						
							|  |  |  |  |           console.log('no smartcli standard task was created or assigned.'); | 
					
						
							|  |  |  |  |         }; | 
					
						
							|  |  |  |  |         break; | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |       if (command.commandName === parsedYArgs._[0]) { | 
					
						
							|  |  |  |  |         command.subject.next(parsedYArgs); | 
					
						
							|  |  |  |  |         break; | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |       if (this.aliasObject[parsedYArgs[0]]) { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     this.parseCompleted.resolve(parsedYArgs); | 
					
						
							| 
									
										
										
										
											2018-05-03 12:10:39 +02:00
										 |  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2017-04-22 21:03:28 +02:00
										 |  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-10-15 00:56:02 +02:00
										 |  |  |  | } |