| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  | import * as plugins from '../npmci.plugins'; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:34:38 +02:00
										 |  |  | import * as paths from '../npmci.paths'; | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  | import * as npmciConfig from '../npmci.config'; | 
					
						
							| 
									
										
										
										
											2018-05-27 14:34:38 +02:00
										 |  |  | import { bash, bashNoError, nvmAvailable } from '../npmci.bash'; | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * handle cli input | 
					
						
							|  |  |  |  * @param argvArg | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  | export let handleCli = async argvArg => { | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   if (argvArg._.length >= 3) { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     let action: string = argvArg._[1]; | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |     switch (action) { | 
					
						
							|  |  |  |       case 'install': | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |         await install(argvArg._[2]); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |         plugins.beautylog.error(`>>npmci node ...<< action >>${action}<< not supported`); | 
					
						
							|  |  |  |         process.exit(1); | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     plugins.beautylog.error( | 
					
						
							|  |  |  |       `>>npmci node ...<< cli arguments invalid... Please read the documentation.` | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-03-08 14:50:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Install a specific version of node | 
					
						
							|  |  |  |  * @param versionArg | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  | export let install = async versionArg => { | 
					
						
							|  |  |  |   plugins.beautylog.log(`now installing node version ${versionArg}`); | 
					
						
							|  |  |  |   let version: string; | 
					
						
							| 
									
										
										
										
											2017-02-19 14:46:05 +01:00
										 |  |  |   if (versionArg === 'stable') { | 
					
						
							| 
									
										
										
										
											2018-07-02 23:09:52 +02:00
										 |  |  |     version = '10'; | 
					
						
							| 
									
										
										
										
											2017-02-19 14:46:05 +01:00
										 |  |  |   } else if (versionArg === 'lts') { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     version = '8'; | 
					
						
							| 
									
										
										
										
											2017-02-19 14:46:05 +01:00
										 |  |  |   } else if (versionArg === 'legacy') { | 
					
						
							| 
									
										
										
										
											2018-07-02 23:09:52 +02:00
										 |  |  |     version = '9'; | 
					
						
							| 
									
										
										
										
											2017-02-19 14:46:05 +01:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     version = versionArg; | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-03-11 01:10:37 +01:00
										 |  |  |   if (await nvmAvailable.promise) { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     await bash(`nvm install ${version} && nvm alias default ${version}`); | 
					
						
							|  |  |  |     plugins.beautylog.success(`Node version ${version} successfully installed!`); | 
					
						
							| 
									
										
										
										
											2017-02-19 14:46:05 +01:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     plugins.beautylog.warn('Nvm not in path so staying at installed node version!'); | 
					
						
							| 
									
										
										
										
											2017-08-27 15:24:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |   await bash('node -v'); | 
					
						
							|  |  |  |   await bash('npm -v'); | 
					
						
							| 
									
										
										
										
											2018-05-27 14:34:38 +02:00
										 |  |  |   await bash(`npm config set cache ${paths.NpmciCacheDir}  --global `); | 
					
						
							| 
									
										
										
										
											2017-02-19 14:46:05 +01:00
										 |  |  |   // lets look for further config
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |   await npmciConfig.getConfig().then(async configArg => { | 
					
						
							|  |  |  |     plugins.beautylog.log('Now checking for needed global npm tools...'); | 
					
						
							|  |  |  |     for (let npmTool of configArg.npmGlobalTools) { | 
					
						
							|  |  |  |       plugins.beautylog.info(`Checking for global "${npmTool}"`); | 
					
						
							|  |  |  |       let whichOutput: string = await bashNoError(`which ${npmTool}`); | 
					
						
							|  |  |  |       let toolAvailable: boolean = !(/not\sfound/.test(whichOutput) || whichOutput === ''); | 
					
						
							|  |  |  |       if (toolAvailable) { | 
					
						
							|  |  |  |         plugins.beautylog.log(`Tool ${npmTool} is available`); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         plugins.beautylog.info(`globally installing ${npmTool} from npm`); | 
					
						
							| 
									
										
										
										
											2018-05-27 14:34:38 +02:00
										 |  |  |         await bash(`npm install ${npmTool} -q -g`); | 
					
						
							| 
									
										
										
										
											2017-02-19 14:46:05 +01:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:25:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     plugins.beautylog.success('all global npm tools specified in npmextra.json are now available!'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; |