| 
									
										
										
										
											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-28 12:40:45 -08:00
										 |  |  | export async function configAuthentication( | 
					
						
							|  |  |  |   id: string, | 
					
						
							|  |  |  |   username: string, | 
					
						
							|  |  |  |   password: string | 
					
						
							|  |  |  | ) { | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  |   if (id && username && password) { | 
					
						
							| 
									
										
										
										
											2019-11-28 12:54:29 -08:00
										 |  |  |     console.log( | 
					
						
							|  |  |  |       `creating ${SETTINGS_FILE} with server-id: ${id}, username: ${username}, and a password` | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-12-06 14:25:41 -05:00
										 |  |  |     const directory: string = path.join(os.homedir(), M2_DIR); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |     await io.mkdirP(directory); | 
					
						
							|  |  |  |     core.debug(`created directory ${directory}`); | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  |     await write(directory, generate(id, username, password)); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     core.debug( | 
					
						
							| 
									
										
										
										
											2019-11-28 12:54:29 -08:00
										 |  |  |       `no ${SETTINGS_FILE} without server-id: ${id}, username: ${username}, and a password` | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-10 09:26:51 -08:00
										 |  |  | function escapeXML(value: string) { | 
					
						
							|  |  |  |   return value | 
					
						
							|  |  |  |     .replace(/&/g, '&') | 
					
						
							|  |  |  |     .replace(/</g, '<') | 
					
						
							|  |  |  |     .replace(/>/g, '>') | 
					
						
							|  |  |  |     .replace(/"/g, '"') | 
					
						
							|  |  |  |     .replace(/'/g, '''); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | // only exported for testing purposes
 | 
					
						
							| 
									
										
										
										
											2019-11-28 12:40:08 -08:00
										 |  |  | export function generate(id: string, username: string, password: string) { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   return `
 | 
					
						
							|  |  |  |   <settings> | 
					
						
							|  |  |  |       <servers> | 
					
						
							|  |  |  |         <server> | 
					
						
							| 
									
										
										
										
											2019-12-10 09:26:51 -08:00
										 |  |  |           <id>${escapeXML(id)}</id> | 
					
						
							|  |  |  |           <username>${escapeXML(username)}</username> | 
					
						
							|  |  |  |           <password>${escapeXML(password)}</password> | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |         </server> | 
					
						
							|  |  |  |       </servers> | 
					
						
							|  |  |  |   </settings> | 
					
						
							|  |  |  |   `;
 | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function write(directory: string, settings: string) { | 
					
						
							| 
									
										
										
										
											2019-12-04 23:54:21 -05:00
										 |  |  |   const options = {encoding: 'utf-8', flag: 'wx'}; // 'wx': Like 'w' but fails if path exists
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:25:21 -08:00
										 |  |  |   const location = path.join(directory, SETTINGS_FILE); | 
					
						
							| 
									
										
										
										
											2019-12-06 14:28:17 -05:00
										 |  |  |   console.log(`writing ${location}`); | 
					
						
							| 
									
										
										
										
											2019-12-04 23:54:21 -05:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2019-12-05 00:43:41 -05:00
										 |  |  |     return fs.writeFileSync(location, settings, options); | 
					
						
							| 
									
										
										
										
											2019-12-04 23:54:21 -05:00
										 |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2019-12-06 14:32:51 -05:00
										 |  |  |     if (e.code == 'EEXIST') { | 
					
						
							| 
									
										
										
										
											2019-12-06 14:28:34 -05:00
										 |  |  |       console.warn(`overwriting existing file ${location}`); | 
					
						
							| 
									
										
										
										
											2019-12-06 14:32:51 -05:00
										 |  |  |       return fs.writeFileSync(location, settings, { | 
					
						
							|  |  |  |         encoding: 'utf-8', | 
					
						
							|  |  |  |         flag: 'w' | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-12-04 23:54:21 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     throw e; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-11-15 16:01:13 -08:00
										 |  |  | } |