| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-23 14:53:00 +01:00
										 |  |  | const { resolve } = require('path'); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  | const commander = require('commander'); | 
					
						
							| 
									
										
										
										
											2021-11-23 14:53:00 +01:00
										 |  |  | const { checkInstallPath, generateNewApp } = require('@strapi/generate-new'); | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  | const promptUser = require('./utils/prompt-user'); | 
					
						
							| 
									
										
										
										
											2022-02-21 12:52:20 +01:00
										 |  |  | // eslint-disable-next-line import/extensions
 | 
					
						
							| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | const packageJson = require('./package.json'); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  | const program = new commander.Command(); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 15:36:38 +01:00
										 |  |  | const databaseOptions = [ | 
					
						
							| 
									
										
										
										
											2021-11-10 17:55:31 +01:00
										 |  |  |   'dbclient', | 
					
						
							|  |  |  |   'dbhost', | 
					
						
							|  |  |  |   'dbport', | 
					
						
							|  |  |  |   'dbname', | 
					
						
							|  |  |  |   'dbusername', | 
					
						
							|  |  |  |   'dbpassword', | 
					
						
							|  |  |  |   'dbssl', | 
					
						
							|  |  |  |   'dbfile', | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  | program | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |   .command(packageJson.name) | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  |   .version(packageJson.version) | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  |   .arguments('[directory]') | 
					
						
							| 
									
										
										
										
											2019-07-08 09:20:34 +02:00
										 |  |  |   .option('--no-run', 'Do not start the application after it is created') | 
					
						
							| 
									
										
										
										
											2020-08-27 10:53:38 +02:00
										 |  |  |   .option('--use-npm', 'Force usage of npm instead of yarn to create the project') | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  |   .option('--debug', 'Display database connection error') | 
					
						
							|  |  |  |   .option('--quickstart', 'Quickstart app creation') | 
					
						
							|  |  |  |   .option('--dbclient <dbclient>', 'Database client') | 
					
						
							|  |  |  |   .option('--dbhost <dbhost>', 'Database host') | 
					
						
							|  |  |  |   .option('--dbport <dbport>', 'Database port') | 
					
						
							|  |  |  |   .option('--dbname <dbname>', 'Database name') | 
					
						
							|  |  |  |   .option('--dbusername <dbusername>', 'Database username') | 
					
						
							|  |  |  |   .option('--dbpassword <dbpassword>', 'Database password') | 
					
						
							|  |  |  |   .option('--dbssl <dbssl>', 'Database SSL') | 
					
						
							|  |  |  |   .option('--dbfile <dbfile>', 'Database file path for sqlite') | 
					
						
							|  |  |  |   .option('--dbforce', 'Overwrite database content if any') | 
					
						
							| 
									
										
										
										
											2020-08-27 10:53:38 +02:00
										 |  |  |   .option('--template <templateurl>', 'Specify a Strapi template') | 
					
						
							| 
									
										
										
										
											2022-03-07 11:09:26 +01:00
										 |  |  |   .option('--ts, --typescript', 'Use TypeScript to generate the project') | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  |   .description('create a new application') | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |   .action((directory, options) => { | 
					
						
							|  |  |  |     initProject(directory, program, options); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  |   }) | 
					
						
							|  |  |  |   .parse(process.argv); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  | function generateApp(projectName, options) { | 
					
						
							|  |  |  |   if (!projectName) { | 
					
						
							|  |  |  |     console.error('Please specify the <directory> of your project when using --quickstart'); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-01-24 15:39:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  |   return generateNewApp(projectName, options).then(() => { | 
					
						
							|  |  |  |     if (process.platform === 'win32') { | 
					
						
							|  |  |  |       process.exit(0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-06-20 15:45:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  | async function initProject(projectName, program, inputOptions) { | 
					
						
							| 
									
										
										
										
											2021-11-29 10:11:51 +01:00
										 |  |  |   if (projectName) { | 
					
						
							|  |  |  |     await checkInstallPath(resolve(projectName)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-23 14:53:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-18 10:57:50 +02:00
										 |  |  |   const programFlags = program.options | 
					
						
							|  |  |  |     .reduce((acc, { short, long }) => [...acc, short, long], []) | 
					
						
							|  |  |  |     .filter(Boolean); | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |   if (inputOptions.template && programFlags.includes(inputOptions.template)) { | 
					
						
							|  |  |  |     console.error(`${inputOptions.template} is not a valid template`); | 
					
						
							| 
									
										
										
										
											2022-08-18 10:57:50 +02:00
										 |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |   const hasDatabaseOptions = databaseOptions.some((opt) => inputOptions[opt]); | 
					
						
							| 
									
										
										
										
											2021-11-10 17:55:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |   if (inputOptions.quickstart && hasDatabaseOptions) { | 
					
						
							| 
									
										
										
										
											2021-11-10 17:55:31 +01:00
										 |  |  |     console.error( | 
					
						
							| 
									
										
										
										
											2021-11-16 15:36:38 +01:00
										 |  |  |       `The quickstart option is incompatible with the following options: ${databaseOptions.join( | 
					
						
							| 
									
										
										
										
											2021-11-10 17:55:31 +01:00
										 |  |  |         ', ' | 
					
						
							|  |  |  |       )}`
 | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 15:36:38 +01:00
										 |  |  |   if (hasDatabaseOptions) { | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |     inputOptions.quickstart = false; // Will disable the quickstart question because != 'undefined'
 | 
					
						
							| 
									
										
										
										
											2021-11-10 17:55:31 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |   if (inputOptions.quickstart) { | 
					
						
							|  |  |  |     return generateApp(projectName, inputOptions); | 
					
						
							| 
									
										
										
										
											2021-04-27 08:54:11 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |   const prompt = await promptUser(projectName, inputOptions, hasDatabaseOptions); | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  |   const directory = prompt.directory || projectName; | 
					
						
							| 
									
										
										
										
											2021-11-29 10:11:51 +01:00
										 |  |  |   await checkInstallPath(resolve(directory)); | 
					
						
							| 
									
										
										
										
											2021-11-29 10:05:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  |   const options = { | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |     template: inputOptions.template, | 
					
						
							|  |  |  |     quickstart: prompt.quick || inputOptions.quickstart, | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const generateStrapiAppOptions = { | 
					
						
							| 
									
										
										
										
											2022-12-02 09:38:44 +01:00
										 |  |  |     ...inputOptions, | 
					
						
							| 
									
										
										
										
											2021-06-02 14:55:33 +02:00
										 |  |  |     ...options, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return generateApp(directory, generateStrapiAppOptions); | 
					
						
							|  |  |  | } |