96 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-08-26 14:19:03 +02:00
'use strict';
const execa = require('execa');
2018-03-23 12:44:17 +01:00
const _ = require('lodash');
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);
};
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;
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
const hasAdmin = await strapi.admin.services.user.exists();
2019-09-09 16:06:54 +02:00
return ctx.send({
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 {
const { plugin } = ctx.request.body;
2019-12-01 23:05:58 +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}...`);
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) {
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) {
try {
const plugins = Object.keys(strapi.plugins).reduce((acc, key) => {
acc[key] = _.get(strapi.plugins, [key, 'package', 'strapi'], {
name: key,
});
return acc;
}, {});
ctx.send({ plugins });
2019-04-02 22:45:21 +02:00
} catch (err) {
strapi.log.error(err);
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
}
},
2019-07-15 19:33:07 +02:00
async uninstallPlugin(ctx) {
try {
const { plugin } = ctx.params;
2019-12-01 23:05:58 +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}...`);
await execa('npm', ['run', 'strapi', '--', 'uninstall', plugin, '-d']);
2017-11-02 17:05:37 +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) {
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' }] }]);
}
2019-04-02 22:45:21 +02:00
},
2016-08-26 14:19:03 +02:00
};