| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-08 15:39:08 +02:00
										 |  |  | const chalk = require('chalk'); | 
					
						
							| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  | const stopProcess = require('./stop-process'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-08 15:39:08 +02:00
										 |  |  | const DB_ARGS = [ | 
					
						
							| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  |   'dbclient', | 
					
						
							|  |  |  |   'dbhost', | 
					
						
							|  |  |  |   'dbport', | 
					
						
							|  |  |  |   'dbname', | 
					
						
							|  |  |  |   'dbusername', | 
					
						
							|  |  |  |   'dbpassword', | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-08 15:39:08 +02:00
										 |  |  | const VALID_CLIENTS = ['sqlite', 'mysql', 'postgres', 'mongo']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  | module.exports = function parseDatabaseArguments({ scope, args }) { | 
					
						
							|  |  |  |   const argKeys = Object.keys(args); | 
					
						
							| 
									
										
										
										
											2019-07-08 15:39:08 +02:00
										 |  |  |   const matchingArgs = DB_ARGS.filter(key => argKeys.includes(key)); | 
					
						
							|  |  |  |   const missingArgs = DB_ARGS.filter(key => !argKeys.includes(key)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (matchingArgs.length === 0) return; | 
					
						
							| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-08 15:39:08 +02:00
										 |  |  |   if (matchingArgs.length !== DB_ARGS.length && args.dbclient !== 'sqlite') { | 
					
						
							|  |  |  |     return stopProcess( | 
					
						
							|  |  |  |       `Required database arguments are missing: ${missingArgs.join(', ')}.` | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-08 15:39:08 +02:00
										 |  |  |   if (!VALID_CLIENTS.includes(args.dbclient)) { | 
					
						
							| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  |     return stopProcess( | 
					
						
							| 
									
										
										
										
											2019-07-08 15:39:08 +02:00
										 |  |  |       `Invalid client ${chalk.yellow( | 
					
						
							|  |  |  |         args.dbclient | 
					
						
							|  |  |  |       )}. Possible choices: ${VALID_CLIENTS.join(', ')}.`
 | 
					
						
							| 
									
										
										
										
											2019-06-20 16:38:15 +02:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   scope.dbforce = args.dbforce !== undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const database = { | 
					
						
							|  |  |  |     settings: { | 
					
						
							|  |  |  |       client: args.dbclient, | 
					
						
							|  |  |  |       host: args.dbhost, | 
					
						
							|  |  |  |       srv: args.dbsrv, | 
					
						
							|  |  |  |       port: args.dbport, | 
					
						
							|  |  |  |       database: args.dbname, | 
					
						
							|  |  |  |       username: args.dbusername, | 
					
						
							|  |  |  |       password: args.dbpassword, | 
					
						
							|  |  |  |       filename: args.dbfile, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     options: {}, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (args.dbauth !== undefined) { | 
					
						
							|  |  |  |     database.options.authenticationDatabase = args.dbauth; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (args.dbssl !== undefined) { | 
					
						
							|  |  |  |     if (args.dbclient === 'mongo') { | 
					
						
							|  |  |  |       database.options.ssl = args.dbssl === 'true'; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       database.settings.ssl = args.dbssl === 'true'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   scope.database = database; | 
					
						
							|  |  |  | }; |