mirror of
				https://github.com/strapi/strapi.git
				synced 2025-11-03 19:36:20 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			107 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
'use strict';
 | 
						|
 | 
						|
const execa = require('execa');
 | 
						|
const _ = require('lodash');
 | 
						|
// eslint-disable-next-line node/no-extraneous-require
 | 
						|
const ee = require('@strapi/strapi/lib/utils/ee');
 | 
						|
 | 
						|
const { getService } = require('../utils');
 | 
						|
 | 
						|
const PLUGIN_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-_]+$/;
 | 
						|
 | 
						|
/**
 | 
						|
 * Validates a plugin name format
 | 
						|
 */
 | 
						|
const isValidPluginName = plugin => {
 | 
						|
  return _.isString(plugin) && !_.isEmpty(plugin) && PLUGIN_NAME_REGEX.test(plugin);
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * A set of functions called "actions" for `Admin`
 | 
						|
 */
 | 
						|
 | 
						|
module.exports = {
 | 
						|
  // 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() {
 | 
						|
    // FIXME
 | 
						|
    try {
 | 
						|
      return { data: { isEE: strapi.EE, features: ee.features.getEnabled() } };
 | 
						|
    } catch (err) {
 | 
						|
      return { data: { isEE: false, features: [] } };
 | 
						|
    }
 | 
						|
  },
 | 
						|
 | 
						|
  async init() {
 | 
						|
    const uuid = strapi.config.get('uuid', false);
 | 
						|
    const hasAdmin = await getService('user').exists();
 | 
						|
 | 
						|
    return { data: { uuid, hasAdmin } };
 | 
						|
  },
 | 
						|
 | 
						|
  async information() {
 | 
						|
    const currentEnvironment = strapi.config.get('environment');
 | 
						|
    const autoReload = strapi.config.get('autoReload', false);
 | 
						|
    const strapiVersion = strapi.config.get('info.strapi', null);
 | 
						|
    const nodeVersion = process.version;
 | 
						|
    const communityEdition = !strapi.EE;
 | 
						|
 | 
						|
    return {
 | 
						|
      data: { currentEnvironment, autoReload, strapiVersion, nodeVersion, communityEdition },
 | 
						|
    };
 | 
						|
  },
 | 
						|
 | 
						|
  async installPlugin(ctx) {
 | 
						|
    try {
 | 
						|
      const { plugin } = ctx.request.body;
 | 
						|
 | 
						|
      if (!isValidPluginName(plugin)) {
 | 
						|
        return ctx.badRequest('Invalid plugin name');
 | 
						|
      }
 | 
						|
 | 
						|
      strapi.reload.isWatching = false;
 | 
						|
 | 
						|
      strapi.log.info(`Installing ${plugin}...`);
 | 
						|
      await execa('npm', ['run', 'strapi', '--', 'install', plugin]);
 | 
						|
 | 
						|
      ctx.send({ ok: true });
 | 
						|
 | 
						|
      strapi.reload();
 | 
						|
    } catch (err) {
 | 
						|
      strapi.log.error(err);
 | 
						|
      strapi.reload.isWatching = true;
 | 
						|
      ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
 | 
						|
    }
 | 
						|
  },
 | 
						|
 | 
						|
  async plugins(ctx) {
 | 
						|
    // TODO: use name from plugin package.json info
 | 
						|
    const plugins = _.mapValues(strapi.plugins, (_, key) => ({ name: key }));
 | 
						|
    ctx.send({ plugins });
 | 
						|
  },
 | 
						|
 | 
						|
  async uninstallPlugin(ctx) {
 | 
						|
    try {
 | 
						|
      const { plugin } = ctx.params;
 | 
						|
 | 
						|
      if (!isValidPluginName(plugin)) {
 | 
						|
        return ctx.badRequest('Invalid plugin name');
 | 
						|
      }
 | 
						|
 | 
						|
      strapi.reload.isWatching = false;
 | 
						|
 | 
						|
      strapi.log.info(`Uninstalling ${plugin}...`);
 | 
						|
      await execa('npm', ['run', 'strapi', '--', 'uninstall', plugin, '-d']);
 | 
						|
 | 
						|
      ctx.send({ ok: true });
 | 
						|
 | 
						|
      strapi.reload();
 | 
						|
    } catch (err) {
 | 
						|
      strapi.log.error(err);
 | 
						|
      strapi.reload.isWatching = true;
 | 
						|
      ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
 | 
						|
    }
 | 
						|
  },
 | 
						|
};
 |