| 
									
										
										
										
											2016-08-26 14:19:03 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-30 18:01:59 +02:00
										 |  |  | const execa = require('execa'); | 
					
						
							| 
									
										
										
										
											2018-03-23 12:44:17 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  | const { ValidationError } = require('@strapi/utils').errors; | 
					
						
							| 
									
										
										
										
											2021-07-01 15:50:11 +02:00
										 |  |  | // eslint-disable-next-line node/no-extraneous-require
 | 
					
						
							| 
									
										
										
										
											2021-07-12 17:25:54 +02:00
										 |  |  | const ee = require('@strapi/strapi/lib/utils/ee'); | 
					
						
							| 
									
										
										
										
											2021-07-01 15:50:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-04 19:39:40 +02:00
										 |  |  | const { getService } = require('../utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-07 14:15:16 +01:00
										 |  |  | const PLUGIN_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-_]+$/; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Validates a plugin name format | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const isValidPluginName = plugin => { | 
					
						
							| 
									
										
										
										
											2020-03-06 19:16:23 +01:00
										 |  |  |   return _.isString(plugin) && !_.isEmpty(plugin) && PLUGIN_NAME_REGEX.test(plugin); | 
					
						
							| 
									
										
										
										
											2020-01-07 14:15:16 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 14:19:03 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A set of functions called "actions" for `Admin` | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2021-07-01 14:30:52 +02:00
										 |  |  |   // TODO very temporary to check the switch ee/ce
 | 
					
						
							|  |  |  |   // When removing this we need to update the /admin/src/index.js file
 | 
					
						
							|  |  |  |   // where we set the strapi.window.isEE value
 | 
					
						
							|  |  |  |   async getProjectType() { | 
					
						
							| 
									
										
										
										
											2021-07-12 17:25:54 +02:00
										 |  |  |     // FIXME
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       return { data: { isEE: strapi.EE, features: ee.features.getEnabled() } }; | 
					
						
							|  |  |  |     } catch (err) { | 
					
						
							|  |  |  |       return { data: { isEE: false, features: [] } }; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-01 14:30:52 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 11:26:17 +02:00
										 |  |  |   async init() { | 
					
						
							| 
									
										
										
										
											2020-04-09 16:27:29 +02:00
										 |  |  |     const uuid = strapi.config.get('uuid', false); | 
					
						
							| 
									
										
										
										
											2021-07-28 15:32:21 +02:00
										 |  |  |     const hasAdmin = await getService('user').exists(); | 
					
						
							| 
									
										
										
										
											2020-10-06 11:26:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { data: { uuid, hasAdmin } }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async information() { | 
					
						
							| 
									
										
										
										
											2021-09-01 19:55:16 +02:00
										 |  |  |     const currentEnvironment = strapi.config.get('environment'); | 
					
						
							| 
									
										
										
										
											2020-04-09 16:27:29 +02:00
										 |  |  |     const autoReload = strapi.config.get('autoReload', false); | 
					
						
							|  |  |  |     const strapiVersion = strapi.config.get('info.strapi', null); | 
					
						
							| 
									
										
										
										
											2020-10-06 11:26:17 +02:00
										 |  |  |     const nodeVersion = process.version; | 
					
						
							|  |  |  |     const communityEdition = !strapi.EE; | 
					
						
							| 
									
										
										
										
											2019-09-09 16:06:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 11:26:17 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |       data: { currentEnvironment, autoReload, strapiVersion, nodeVersion, communityEdition }, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-09-09 16:06:54 +02:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 19:33:07 +02:00
										 |  |  |   async installPlugin(ctx) { | 
					
						
							| 
									
										
										
										
											2017-12-16 17:35:16 +01:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2019-04-26 19:19:00 +02:00
										 |  |  |       const { plugin } = ctx.request.body; | 
					
						
							| 
									
										
										
										
											2019-12-01 23:05:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-07 14:15:16 +01:00
										 |  |  |       if (!isValidPluginName(plugin)) { | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |         throw new ValidationError('Invalid plugin name'); | 
					
						
							| 
									
										
										
										
											2019-12-01 23:05:58 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 17:35:16 +01:00
										 |  |  |       strapi.reload.isWatching = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       strapi.log.info(`Installing ${plugin}...`); | 
					
						
							| 
									
										
										
										
											2019-04-30 18:01:59 +02:00
										 |  |  |       await execa('npm', ['run', 'strapi', '--', 'install', plugin]); | 
					
						
							| 
									
										
										
										
											2017-12-16 17:35:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       ctx.send({ ok: true }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       strapi.reload(); | 
					
						
							| 
									
										
										
										
											2019-04-02 22:45:21 +02:00
										 |  |  |     } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-12-16 17:35:16 +01:00
										 |  |  |       strapi.reload.isWatching = true; | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |       throw err; | 
					
						
							| 
									
										
										
										
											2017-12-16 17:35:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 19:33:07 +02:00
										 |  |  |   async plugins(ctx) { | 
					
						
							| 
									
										
										
										
											2021-11-04 12:01:49 +01:00
										 |  |  |     const enabledPlugins = strapi.config.get('enabledPlugins'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const plugins = Object.entries(enabledPlugins).map(([key, plugin]) => ({ | 
					
						
							|  |  |  |       name: plugin.info.name || key, | 
					
						
							|  |  |  |       displayName: plugin.info.displayName || plugin.info.name || key, | 
					
						
							|  |  |  |       description: plugin.info.description || '', | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-20 13:12:37 +02:00
										 |  |  |     ctx.send({ plugins }); | 
					
						
							| 
									
										
										
										
											2017-11-09 11:25:01 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 19:33:07 +02:00
										 |  |  |   async uninstallPlugin(ctx) { | 
					
						
							| 
									
										
										
										
											2017-11-02 16:49:10 +01:00
										 |  |  |     try { | 
					
						
							|  |  |  |       const { plugin } = ctx.params; | 
					
						
							| 
									
										
										
										
											2019-12-01 23:05:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-07 14:15:16 +01:00
										 |  |  |       if (!isValidPluginName(plugin)) { | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |         throw new ValidationError('Invalid plugin name'); | 
					
						
							| 
									
										
										
										
											2019-12-01 23:05:58 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-02 17:05:37 +01:00
										 |  |  |       strapi.reload.isWatching = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       strapi.log.info(`Uninstalling ${plugin}...`); | 
					
						
							| 
									
										
										
										
											2019-05-13 18:38:13 +02:00
										 |  |  |       await execa('npm', ['run', 'strapi', '--', 'uninstall', plugin, '-d']); | 
					
						
							| 
									
										
										
										
											2017-11-02 17:05:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-02 16:49:10 +01:00
										 |  |  |       ctx.send({ ok: true }); | 
					
						
							| 
									
										
										
										
											2017-11-02 17:05:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       strapi.reload(); | 
					
						
							| 
									
										
										
										
											2019-04-02 22:45:21 +02:00
										 |  |  |     } catch (err) { | 
					
						
							| 
									
										
										
										
											2017-11-02 17:05:37 +01:00
										 |  |  |       strapi.reload.isWatching = true; | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |       throw err; | 
					
						
							| 
									
										
										
										
											2017-11-02 16:49:10 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-02 22:45:21 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2016-08-26 14:19:03 +02:00
										 |  |  | }; |