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');
|
2016-11-24 16:19:01 +01:00
|
|
|
|
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 = {
|
2019-09-09 16:06:54 +02:00
|
|
|
async init(ctx) {
|
|
|
|
const currentEnvironment = strapi.app.env;
|
2020-04-09 16:27:29 +02:00
|
|
|
const uuid = strapi.config.get('uuid', false);
|
|
|
|
const autoReload = strapi.config.get('autoReload', false);
|
|
|
|
const strapiVersion = strapi.config.get('info.strapi', null);
|
2019-09-09 16:06:54 +02:00
|
|
|
|
2020-05-22 16:09:37 +02:00
|
|
|
const hasAdmin = await strapi.admin.services.user.exists();
|
2020-05-22 11:15:06 +02:00
|
|
|
|
2019-09-09 16:06:54 +02:00
|
|
|
return ctx.send({
|
2020-05-22 11:15:06 +02:00
|
|
|
data: { uuid, currentEnvironment, autoReload, strapiVersion, hasAdmin },
|
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)) {
|
2019-12-01 23:05:58 +01:00
|
|
|
return ctx.badRequest('Invalid plugin name');
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-04-30 18:01:59 +02:00
|
|
|
strapi.log.error(err);
|
2017-12-16 17:35:16 +01:00
|
|
|
strapi.reload.isWatching = true;
|
2018-05-15 16:08:47 -04:00
|
|
|
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
|
2017-12-16 17:35:16 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
async plugins(ctx) {
|
2017-11-09 11:25:01 +01:00
|
|
|
try {
|
2017-11-09 13:30:54 +01:00
|
|
|
const plugins = Object.keys(strapi.plugins).reduce((acc, key) => {
|
2019-04-26 19:19:00 +02:00
|
|
|
acc[key] = _.get(strapi.plugins, [key, 'package', 'strapi'], {
|
|
|
|
name: key,
|
|
|
|
});
|
2017-11-09 13:30:54 +01:00
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
ctx.send({ plugins });
|
2019-04-02 22:45:21 +02:00
|
|
|
} catch (err) {
|
2019-04-26 19:19:00 +02:00
|
|
|
strapi.log.error(err);
|
2018-05-15 16:08:47 -04:00
|
|
|
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
|
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)) {
|
2019-12-01 23:05:58 +01:00
|
|
|
return ctx.badRequest('Invalid plugin name');
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-04-30 18:01:59 +02:00
|
|
|
strapi.log.error(err);
|
2017-11-02 17:05:37 +01:00
|
|
|
strapi.reload.isWatching = true;
|
2018-05-15 16:08:47 -04:00
|
|
|
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
|
2017-11-02 16:49:10 +01:00
|
|
|
}
|
2019-04-02 22:45:21 +02:00
|
|
|
},
|
2016-08-26 14:19:03 +02:00
|
|
|
};
|