| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  | import * as plugins from './smartenv.plugins'; | 
					
						
							|  |  |  | import * as interfaces from './interfaces'; | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // interfaces
 | 
					
						
							|  |  |  | export interface IEnvObject { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   name: string; | 
					
						
							|  |  |  |   value: string; | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Smartenv class that makes it easy | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  | export class Smartenv { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   get runtimeEnv() { | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     if (typeof window !== 'undefined') { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       return 'browser'; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } else if (typeof process !== 'undefined') { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       return 'node'; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   get isBrowser(): boolean { | 
					
						
							|  |  |  |     return !this.isNode; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   get userAgent(): string { | 
					
						
							|  |  |  |     if (this.isBrowser) { | 
					
						
							|  |  |  |       // make sure we are in Browser
 | 
					
						
							|  |  |  |       return navigator.userAgent; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       return 'undefined'; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   get isNode(): boolean { | 
					
						
							|  |  |  |     return this.runtimeEnv === 'node'; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   get nodeVersion(): string { | 
					
						
							|  |  |  |     return process.version; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   get isCI(): boolean { | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     if (this.isNode) { | 
					
						
							|  |  |  |       if (process.env.CI) { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |         return false; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   async isMacAsync(): Promise<boolean> { | 
					
						
							|  |  |  |     if (this.isNode) { | 
					
						
							|  |  |  |       let os = await import('os'); | 
					
						
							|  |  |  |       return os.platform() === 'darwin'; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async isWindowsAsync(): Promise<boolean> { | 
					
						
							|  |  |  |     if (this.isNode) { | 
					
						
							|  |  |  |       let os = await import('os'); | 
					
						
							|  |  |  |       return os.platform() === 'win32'; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   async isLinuxAsync(): Promise<boolean> { | 
					
						
							|  |  |  |     if (this.isNode) { | 
					
						
							|  |  |  |       let os = await import('os'); | 
					
						
							|  |  |  |       return os.platform() === 'linux'; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * get environment variables that fit the description | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-02-14 00:12:21 +01:00
										 |  |  |   // get envVars (regexArg: RegExp) {
 | 
					
						
							|  |  |  |   //   let EnvironmentArray = []
 | 
					
						
							|  |  |  |   //   // TODO: plugins.smartparam.forEachMinimatch()
 | 
					
						
							|  |  |  |   // }
 | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * prints the environment to console | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |   async printEnv() { | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  |     if (this.isNode) { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       console.log('running on NODE'); | 
					
						
							|  |  |  |       let smartenvVersion = require('../package.json').version; | 
					
						
							|  |  |  |       console.log( | 
					
						
							|  |  |  |         'node version is ' + this.nodeVersion + ' and smartenv version is ' + smartenvVersion | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-06-17 08:46:28 +02:00
										 |  |  |       console.log('running on BROWSER'); | 
					
						
							|  |  |  |       console.log('browser is ' + this.userAgent); | 
					
						
							| 
									
										
										
										
											2017-05-17 15:59:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |