strapi/controllers/Admin.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-08-26 14:19:03 +02:00
'use strict';
2016-08-30 15:07:53 +02:00
const $ = require('cheerio');
const fs = require('fs');
2016-08-26 14:19:03 +02:00
const path = require('path');
2016-08-30 15:07:53 +02:00
const sendfile = require('koa-sendfile');
const _ = require('lodash');
2016-08-26 14:19:03 +02:00
/**
* A set of functions called "actions" for `Admin`
*/
module.exports = {
index: function *() {
2016-08-30 15:07:53 +02:00
// Read the default `index.html` file from the admin panel build
const html = fs.readFileSync(path.resolve(__dirname, '..', 'public', 'build', 'index.html'));
// Convert the stream to a string
const htmlString = html.toString();
// Use `cheerio` to parse the HTML string
const parsedHTML = $.load(htmlString);
// Some plugins are ignored
const ignoredPlugins = ['admin', 'user'];
// Inject `js` files from plugins builds in the main admin panel
_.forEach(strapi.plugins, (value, pluginName) => {
if (!_.includes(ignoredPlugins, pluginName)) {
// Main plugin `js` file
const pluginMainScript = $('<script>').attr('src', '/' + pluginName + '/main.js');
parsedHTML('body').append(pluginMainScript);
}
});
2016-09-05 15:28:06 +02:00
2016-08-30 15:07:53 +02:00
// Finally, send the HTML file with injected scripts
this.body = parsedHTML.html();
2016-08-26 14:19:03 +02:00
},
file: function *() {
2016-11-22 11:35:10 +01:00
yield sendfile(this, path.resolve(__dirname, '..', 'public', 'build', this.params.file));
2016-08-26 14:19:03 +02:00
yield sendfile(this, path.resolve(__dirname, '..', 'public', 'build', this.params.file));
if (!this.status) this.throw(404);
}
};