167 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-08-26 14:19:03 +02:00
'use strict';
const path = require('path');
2019-01-28 16:23:47 +01:00
const shell = require('shelljs');
2018-03-23 12:44:17 +01:00
const _ = require('lodash');
2016-08-26 14:19:03 +02:00
/**
* A set of functions called "actions" for `Admin`
*/
module.exports = {
2018-03-08 10:32:01 +01:00
getCurrentEnvironment: async ctx => {
try {
2019-04-02 22:45:21 +02:00
const autoReload = _.get(
strapi.config.currentEnvironment,
'server.autoReload.enabled',
false,
);
return ctx.send({ autoReload, currentEnvironment: strapi.app.env });
} catch (err) {
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
2018-03-08 10:32:01 +01:00
}
},
2018-04-12 23:45:17 +02:00
getStrapiVersion: async ctx => {
try {
const strapiVersion = _.get(strapi.config, 'info.strapi', null);
return ctx.send({ strapiVersion });
2019-04-02 22:45:21 +02:00
} catch (err) {
return ctx.badRequest(null, [
{ messages: [{ id: 'The version is not available' }] },
]);
2018-04-12 23:45:17 +02:00
}
},
2018-04-05 11:13:49 +02:00
getGaConfig: async ctx => {
2018-03-23 12:44:17 +01:00
try {
ctx.send({ uuid: _.get(strapi.config, 'uuid', false) });
2019-04-02 22:45:21 +02:00
} catch (err) {
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
2018-03-23 12:44:17 +01:00
}
},
2018-04-05 11:13:49 +02:00
getLayout: async ctx => {
try {
const layout = require('../config/layout.js');
return ctx.send({ layout });
2019-04-02 22:45:21 +02:00
} catch (err) {
return ctx.badRequest(null, [
{ messages: [{ id: 'An error occurred' }] },
]);
2018-04-05 11:13:49 +02:00
}
},
2017-12-16 17:35:16 +01:00
installPlugin: async ctx => {
try {
2017-12-20 11:23:50 +01:00
const { plugin, port } = ctx.request.body;
2019-04-02 22:45:21 +02:00
const strapiBin = path.join(
process.cwd(),
'node_modules',
'strapi',
'bin',
'strapi',
);
2017-12-16 17:35:16 +01:00
strapi.reload.isWatching = false;
strapi.log.info(`Installing ${plugin}...`);
2019-04-02 22:45:21 +02:00
shell.exec(
`node ${strapiBin} install ${plugin} ${port === '4000' ? '--dev' : ''}`,
{ silent: true },
);
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;
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
2017-12-16 17:35:16 +01:00
}
},
plugins: async ctx => {
try {
const plugins = Object.keys(strapi.plugins).reduce((acc, key) => {
acc[key] = strapi.plugins[key].package.strapi;
return acc;
}, {});
ctx.send({ plugins });
2019-04-02 22:45:21 +02:00
} catch (err) {
2018-05-15 16:08:47 -04:00
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
}
},
uninstallPlugin: async ctx => {
try {
const { plugin } = ctx.params;
2019-04-02 22:45:21 +02:00
const strapiBin = path.join(
process.cwd(),
'node_modules',
'strapi',
'bin',
'strapi',
);
2017-11-02 17:05:37 +01:00
strapi.reload.isWatching = false;
strapi.log.info(`Uninstalling ${plugin}...`);
2019-04-02 22:45:21 +02:00
shell.exec(`node ${strapiBin} uninstall ${plugin}`, { silent: true });
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) {
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
},
/**
* Create a/an admin record.
*
* @return {Object}
*/
create: async ctx => {
const values = ctx.request.body;
if (values.password) {
values.password = await strapi.plugins[
'users-permissions'
].services.user.hashPassword(values);
}
2019-04-26 10:17:04 +02:00
const data = await strapi.admin.queries('administrator', 'admin').create(values);
// Send 201 `created`
ctx.created(data);
},
/**
* Update a/an admin record.
*
* @return {Object}
*/
update: async ctx => {
const values = ctx.request.body;
if (values.password) {
values.password = await strapi.plugins[
'users-permissions'
].services.user.hashPassword(values);
}
2019-04-26 10:17:04 +02:00
const data = await strapi.admin.queries('administrator', 'admin')
.update(Object.assign({}, ctx.params, values));
// Send 200 `ok`
ctx.send(data);
},
2016-08-26 14:19:03 +02:00
};