| 
									
										
										
										
											2019-08-29 20:26:23 +02:00
										 |  |  | import * as plugins from '../npmci.plugins'; | 
					
						
							|  |  |  | import * as paths from '../npmci.paths'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { logger } from '../npmci.logging'; | 
					
						
							|  |  |  | import { bash, bashNoError, nvmAvailable } from '../npmci.bash'; | 
					
						
							|  |  |  | import { Npmci } from '../npmci.classes.npmci'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class NpmciNodeJsManager { | 
					
						
							|  |  |  |   public npmciRef: Npmci; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(npmciRefArg: Npmci) { | 
					
						
							|  |  |  |     this.npmciRef = npmciRefArg; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * handle cli input | 
					
						
							|  |  |  |    * @param argvArg | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-10-19 03:09:50 +02:00
										 |  |  |   public async handleCli(argvArg: any) { | 
					
						
							| 
									
										
										
										
											2019-08-29 20:26:23 +02:00
										 |  |  |     if (argvArg._.length >= 3) { | 
					
						
							|  |  |  |       const action: string = argvArg._[1]; | 
					
						
							|  |  |  |       switch (action) { | 
					
						
							|  |  |  |         case 'install': | 
					
						
							|  |  |  |           await this.install(argvArg._[2]); | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |           logger.log('error', `>>npmci node ...<< action >>${action}<< not supported`); | 
					
						
							|  |  |  |           process.exit(1); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       logger.log( | 
					
						
							|  |  |  |         'error', | 
					
						
							|  |  |  |         `>>npmci node ...<< cli arguments invalid... Please read the documentation.` | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       process.exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Install a specific version of node | 
					
						
							|  |  |  |    * @param versionArg | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-10-19 03:09:50 +02:00
										 |  |  |   public async install(versionArg: any) { | 
					
						
							| 
									
										
										
										
											2019-08-29 20:26:23 +02:00
										 |  |  |     logger.log('info', `now installing node version ${versionArg}`); | 
					
						
							|  |  |  |     let version: string; | 
					
						
							|  |  |  |     if (versionArg === 'stable') { | 
					
						
							| 
									
										
										
										
											2021-10-19 03:09:50 +02:00
										 |  |  |       version = '16'; | 
					
						
							| 
									
										
										
										
											2019-08-29 20:26:23 +02:00
										 |  |  |     } else if (versionArg === 'lts') { | 
					
						
							| 
									
										
										
										
											2021-10-19 03:09:50 +02:00
										 |  |  |       version = '14'; | 
					
						
							| 
									
										
										
										
											2019-08-29 20:26:23 +02:00
										 |  |  |     } else if (versionArg === 'legacy') { | 
					
						
							| 
									
										
										
										
											2021-10-19 03:09:50 +02:00
										 |  |  |       version = '12'; | 
					
						
							| 
									
										
										
										
											2019-08-29 20:26:23 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       version = versionArg; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (await nvmAvailable.promise) { | 
					
						
							|  |  |  |       await bash(`nvm install ${version} && nvm alias default ${version}`); | 
					
						
							|  |  |  |       logger.log('success', `Node version ${version} successfully installed!`); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       logger.log('warn', 'Nvm not in path so staying at installed node version!'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     logger.log('info', 'now installing latest npm version'); | 
					
						
							|  |  |  |     await bash('npm install -g npm'); | 
					
						
							|  |  |  |     await bash('node -v'); | 
					
						
							|  |  |  |     await bash('npm -v'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // lets look for further config
 | 
					
						
							|  |  |  |     const config = await this.npmciRef.npmciConfig.getConfig(); | 
					
						
							|  |  |  |     logger.log('info', 'Now checking for needed global npm tools...'); | 
					
						
							|  |  |  |     for (const npmTool of config.npmGlobalTools) { | 
					
						
							|  |  |  |       logger.log('info', `Checking for global "${npmTool}"`); | 
					
						
							|  |  |  |       const whichOutput: string = await bashNoError(`which ${npmTool}`); | 
					
						
							|  |  |  |       const toolAvailable: boolean = !(/not\sfound/.test(whichOutput) || whichOutput === ''); | 
					
						
							|  |  |  |       if (toolAvailable) { | 
					
						
							|  |  |  |         logger.log('info', `Tool ${npmTool} is available`); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         logger.log('info', `globally installing ${npmTool} from npm`); | 
					
						
							|  |  |  |         await bash(`npm install ${npmTool} -q -g`); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     logger.log('success', 'all global npm tools specified in npmextra.json are now available!'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |