| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | import * as fs from 'fs'; | 
					
						
							|  |  |  | import * as os from 'os'; | 
					
						
							|  |  |  | import * as path from 'path'; | 
					
						
							|  |  |  | import * as core from '@actions/core'; | 
					
						
							|  |  |  | import * as io from '@actions/io'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | export const M2_DIR = '.m2'; | 
					
						
							|  |  |  | export const SETTINGS_FILE = 'settings.xml'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | export async function configAuthentication(username: string, password: string) { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   if (username && password) { | 
					
						
							|  |  |  |     core.debug(`configAuthentication with ${username} and a password`); | 
					
						
							|  |  |  |     const directory: string = path.join(os.homedir(), M2_DIR); | 
					
						
							|  |  |  |     await io.mkdirP(directory); | 
					
						
							|  |  |  |     core.debug(`created directory ${directory}`); | 
					
						
							|  |  |  |     await write(directory, generate(username, password)); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     core.debug( | 
					
						
							|  |  |  |       `no auth without username: ${username} and password: ${password}` | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // only exported for testing purposes
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  | export function generate(username: string, password: string) { | 
					
						
							|  |  |  |   return `
 | 
					
						
							|  |  |  |   <settings> | 
					
						
							|  |  |  |       <servers> | 
					
						
							|  |  |  |         <server> | 
					
						
							|  |  |  |           <username>${username}</username> | 
					
						
							|  |  |  |           <password>${password}</password> | 
					
						
							|  |  |  |         </server> | 
					
						
							|  |  |  |       </servers> | 
					
						
							|  |  |  |   </settings> | 
					
						
							|  |  |  |   `;
 | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function write(directory: string, settings: string) { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   const options = {encoding: 'utf-8'}; | 
					
						
							|  |  |  |   const location = path.join(directory, SETTINGS_FILE); | 
					
						
							|  |  |  |   core.debug(`writing ${location}`); | 
					
						
							|  |  |  |   return fs.writeFileSync(location, settings, options); | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } |