Migrate plugin generator and fix some internal issues to make everything compatible

This commit is contained in:
Alexandre Bodin 2021-09-30 11:40:54 +02:00
parent 4dcdd9a488
commit 6ef3e1deb6
48 changed files with 455 additions and 57 deletions

View File

@ -0,0 +1,3 @@
# Strapi plugin super-plugin
A quick description of super-plugin.

View File

@ -0,0 +1,26 @@
/**
*
* Initializer
*
*/
import { useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import pluginId from '../../pluginId';
const Initializer = ({ setPlugin }) => {
const ref = useRef();
ref.current = setPlugin;
useEffect(() => {
ref.current(pluginId);
}, []);
return null;
};
Initializer.propTypes = {
setPlugin: PropTypes.func.isRequired,
};
export default Initializer;

View File

@ -0,0 +1,25 @@
/**
*
* This component is the skeleton around the actual pages, and should only
* contain code that should be seen on all pages. (e.g. navigation bar)
*
*/
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import { NotFound } from '@strapi/helper-plugin';
import pluginId from '../../pluginId';
import HomePage from '../HomePage';
const App = () => {
return (
<div>
<Switch>
<Route path={`/plugins/${pluginId}`} component={HomePage} exact />
<Route component={NotFound} />
</Switch>
</div>
);
};
export default App;

View File

@ -0,0 +1,20 @@
/*
*
* HomePage
*
*/
import React, { memo } from 'react';
// import PropTypes from 'prop-types';
import pluginId from '../../pluginId';
const HomePage = () => {
return (
<div>
<h1>{pluginId}&apos;s HomePage</h1>
<p>Happy coding</p>
</div>
);
};
export default memo(HomePage);

View File

@ -0,0 +1,26 @@
/**
*
* Initializer
*
*/
import { useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import pluginId from '../../pluginId';
const Initializer = ({ setPlugin }) => {
const ref = useRef();
ref.current = setPlugin;
useEffect(() => {
ref.current(pluginId);
}, []);
return null;
};
Initializer.propTypes = {
setPlugin: PropTypes.func.isRequired,
};
export default Initializer;

View File

@ -0,0 +1,65 @@
import { prefixPluginTranslations } from '@strapi/helper-plugin';
import pluginPkg from '../../package.json';
import pluginId from './pluginId';
import Initializer from './components/Initializer';
const pluginDescription = pluginPkg.strapi.description || pluginPkg.description;
const icon = pluginPkg.strapi.icon;
const name = pluginPkg.strapi.name;
export default {
register(app) {
app.addMenuLink({
to: `/plugins/${pluginId}`,
icon,
intlLabel: {
id: `${pluginId}.plugin.name`,
defaultMessage: name,
},
Component: async () => {
const component = await import(/* webpackChunkName: "[request]" */ './pages/App');
return component;
},
permissions: [
// Uncomment to set the permissions of the plugin here
// {
// action: '', // the action name should be plugin::plugin-name.actionType
// subject: null,
// },
],
});
app.registerPlugin({
description: pluginDescription,
icon,
id: pluginId,
initializer: Initializer,
isReady: false,
isRequired: pluginPkg.strapi.required || false,
name,
});
},
bootstrap(app) {},
async registerTrads({ locales }) {
const importedTrads = await Promise.all(
locales.map(locale => {
return import(`./translations/${locale}.json`)
.then(({ default: data }) => {
return {
data: prefixPluginTranslations(data, pluginId),
locale,
};
})
.catch(() => {
return {
data: {},
locale,
};
});
})
);
return Promise.resolve(importedTrads);
},
};

View File

@ -0,0 +1,25 @@
/**
*
* This component is the skeleton around the actual pages, and should only
* contain code that should be seen on all pages. (e.g. navigation bar)
*
*/
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import { NotFound } from '@strapi/helper-plugin';
import pluginId from '../../pluginId';
import HomePage from '../HomePage';
const App = () => {
return (
<div>
<Switch>
<Route path={`/plugins/${pluginId}`} component={HomePage} exact />
<Route component={NotFound} />
</Switch>
</div>
);
};
export default App;

View File

@ -0,0 +1,20 @@
/*
*
* HomePage
*
*/
import React, { memo } from 'react';
// import PropTypes from 'prop-types';
import pluginId from '../../pluginId';
const HomePage = () => {
return (
<div>
<h1>{pluginId}&apos;s HomePage</h1>
<p>Happy coding</p>
</div>
);
};
export default memo(HomePage);

View File

@ -0,0 +1,5 @@
const pluginPkg = require('../../package.json');
const pluginId = pluginPkg.name.replace(/^@strapi\/plugin-/i, '');
module.exports = pluginId;

View File

@ -0,0 +1,5 @@
import pluginId from '../pluginId';
const getTrad = id => `${pluginId}.${id}`;
export default getTrad;

View File

@ -0,0 +1,25 @@
{
"name": "super-plugin",
"version": "0.0.0",
"description": "This is the description of the plugin.",
"strapi": {
"name": "super-plugin",
"icon": "plug",
"description": "Description of super-plugin plugin",
"kind": "plugin"
},
"dependencies": {},
"author": {
"name": "A Strapi developer"
},
"maintainers": [
{
"name": "A Strapi developer"
}
],
"engines": {
"node": ">=12.x. <=16.x.x",
"npm": ">=6.0.0"
},
"license": "MIT"
}

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = ({ strapi }) => {
// bootstrap phase
};

View File

@ -0,0 +1,6 @@
'use strict';
module.exports = {
default: {},
validator() {},
};

View File

@ -0,0 +1,7 @@
'use strict';
const myController = require('./my-controller');
module.exports = {
myController,
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
index(ctx) {
ctx.body = strapi
.plugin('super-plugin')
.service('myService')
.getWelcomeMessage();
},
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = ({ strapi }) => {
// destroy phase
};

View File

@ -0,0 +1,22 @@
'use strict';
const register = require('./register');
const bootstrap = require('./bootstrap');
const destroy = require('./destroy');
const config = require('./config');
const controllers = require('./controllers');
const routes = require('./routes');
const services = require('./services');
module.exports = {
register,
bootstrap,
destroy,
config,
controllers,
routes,
services,
contentTypes: {},
policies: {},
middlewares: {},
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = ({ strapi }) => {
// registeration phase
};

View File

@ -0,0 +1,10 @@
module.exports = [
{
method: 'GET',
path: '/',
handler: 'myController.index',
config: {
policies: [],
},
},
];

View File

@ -0,0 +1,7 @@
'use strict';
const myService = require('./my-service');
module.exports = {
myService,
};

View File

@ -0,0 +1,7 @@
'use strict';
module.exports = ({ strapi }) => ({
getWelcomeMessage() {
return 'Welcome to Strapi 🚀';
},
});

View File

@ -0,0 +1,3 @@
'use strict';
module.exports = require('./admin/src').default;

View File

@ -0,0 +1,3 @@
'use strict';
module.exports = require('./server');

View File

@ -8,5 +8,5 @@ module.exports = {
permission: require('./permission'),
role: require('./role'),
user: require('./user'),
webhooks: require('./Webhooks'),
webhooks: require('./webhooks'),
};

View File

@ -4,7 +4,7 @@ module.exports = [
{
method: 'GET',
path: '/webhooks',
handler: 'Webhooks.listWebhooks',
handler: 'webhooks.listWebhooks',
config: {
policies: [
'admin::isAuthenticatedAdmin',
@ -15,7 +15,7 @@ module.exports = [
{
method: 'POST',
path: '/webhooks',
handler: 'Webhooks.createWebhook',
handler: 'webhooks.createWebhook',
config: {
policies: [
'admin::isAuthenticatedAdmin',
@ -26,7 +26,7 @@ module.exports = [
{
method: 'GET',
path: '/webhooks/:id',
handler: 'Webhooks.getWebhook',
handler: 'webhooks.getWebhook',
config: {
policies: [
'admin::isAuthenticatedAdmin',
@ -37,7 +37,7 @@ module.exports = [
{
method: 'PUT',
path: '/webhooks/:id',
handler: 'Webhooks.updateWebhook',
handler: 'webhooks.updateWebhook',
config: {
policies: [
'admin::isAuthenticatedAdmin',
@ -48,7 +48,7 @@ module.exports = [
{
method: 'DELETE',
path: '/webhooks/:id',
handler: 'Webhooks.deleteWebhook',
handler: 'webhooks.deleteWebhook',
config: {
policies: [
'admin::isAuthenticatedAdmin',
@ -59,7 +59,7 @@ module.exports = [
{
method: 'POST',
path: '/webhooks/batch-delete',
handler: 'Webhooks.deleteWebhooks',
handler: 'webhooks.deleteWebhooks',
config: {
policies: [
'admin::isAuthenticatedAdmin',
@ -70,7 +70,7 @@ module.exports = [
{
method: 'POST',
path: '/webhooks/:id/trigger',
handler: 'Webhooks.triggerWebhook',
handler: 'webhooks.triggerWebhook',
config: {
policies: [],
},

View File

@ -84,6 +84,7 @@ const getController = (name, { pluginName, apiName }, strapi) => {
if (pluginName === 'admin') {
return strapi.controller(`admin::${name}`);
}
return strapi.plugin(pluginName).controller(name);
} else if (apiName) {
return strapi.controller(`api::${apiName}.${name}`);
@ -102,7 +103,7 @@ const getAction = (route, strapi) => {
const [controllerName, actionName] = trim(handler).split('.');
const controller = getController(toLower(controllerName), { pluginName, apiName }, strapi);
const controller = getController(controllerName, { pluginName, apiName }, strapi);
if (typeof controller[actionName] !== 'function') {
throw new Error(`Handler not found "${handler}"`);

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = ({ strapi }) => {
// bootstrap phase
};

View File

@ -0,0 +1,6 @@
'use strict';
module.exports = {
default: {},
validator() {},
};

View File

@ -0,0 +1,7 @@
'use strict';
const myController = require('./my-controller');
module.exports = {
myController,
};

View File

@ -0,0 +1,10 @@
'use strict';
module.exports = {
index(ctx) {
ctx.body = strapi
.plugin('{{ id }}')
.service('myService')
.getWelcomeMessage();
},
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = ({ strapi }) => {
// destroy phase
};

View File

@ -0,0 +1,22 @@
'use strict';
const register = require('./register');
const bootstrap = require('./bootstrap');
const destroy = require('./destroy');
const config = require('./config');
const controllers = require('./controllers');
const routes = require('./routes');
const services = require('./services');
module.exports = {
register,
bootstrap,
destroy,
config,
controllers,
routes,
services,
contentTypes: {},
policies: {},
middlewares: {},
};

View File

@ -0,0 +1,5 @@
'use strict';
module.exports = ({ strapi }) => {
// registeration phase
};

View File

@ -0,0 +1,10 @@
module.exports = [
{
method: 'GET',
path: '/',
handler: 'myController.index',
config: {
policies: [],
},
},
];

View File

@ -0,0 +1,7 @@
'use strict';
const myService = require('./my-service');
module.exports = {
myService,
};

View File

@ -0,0 +1,7 @@
'use strict';
module.exports = ({ strapi }) => ({
getWelcomeMessage() {
return 'Welcome to Strapi 🚀';
},
});

View File

@ -0,0 +1,3 @@
'use strict';
module.exports = require('./admin/src').default;

View File

@ -0,0 +1,3 @@
'use strict';
module.exports = require('./server');

View File

@ -77,7 +77,7 @@ module.exports = plop => {
{
type: 'add',
path: `${filePath}/content-types/{{id}}/schema.json`,
templateFile: 'templates/model.schema.json.hbs',
templateFile: 'templates/content-type.schema.json.hbs',
},
{
type: 'add',

View File

@ -157,7 +157,7 @@ module.exports = plop => {
{
type: 'add',
path: `${filePath}/content-types/{{id}}/schema.json`,
templateFile: 'templates/model.schema.json.hbs',
templateFile: 'templates/content-type.schema.json.hbs',
},
{
type: 'modify',

View File

@ -15,24 +15,9 @@ module.exports = plop => {
return [
{
type: 'addMany',
destination: 'plugins/{{id}}/admin',
base: 'files/plugin/admin',
templateFiles: 'files/plugin/admin/**',
},
{
type: 'add',
path: 'plugins/{{id}}/services/{{id}}.js',
templateFile: 'templates/service.js.hbs',
},
{
type: 'add',
path: 'plugins/{{id}}/controllers/{{id}}.js',
templateFile: 'templates/controller.js.hbs',
},
{
type: 'add',
path: 'plugins/{{id}}/config/routes.json',
templateFile: 'templates/plugin-routes.json.hbs',
destination: 'plugins/{{id}}',
base: 'files/plugin',
templateFiles: 'files/plugin/**',
},
{
type: 'add',

View File

@ -1,23 +1,20 @@
{
"name": "strapi-plugin-{{id}}",
"name": "{{id}}",
"version": "0.0.0",
"description": "This is the description of the plugin.",
"strapi": {
"name": "{{id}}",
"icon": "plug",
"description": "Description of {{id}} plugin."
"description": "Description of {{id}} plugin",
"kind": "plugin"
},
"dependencies": {},
"author": {
"name": "A Strapi developer",
"email": "",
"url": ""
"name": "A Strapi developer"
},
"maintainers": [
{
"name": "A Strapi developer",
"email": "",
"url": ""
"name": "A Strapi developer"
}
],
"engines": {

View File

@ -1,12 +0,0 @@
{
"routes": [
{
"method": "GET",
"path": "/",
"handler": "{{id}}.index",
"config": {
"policies": []
}
}
]
}

View File

@ -4,7 +4,7 @@ module.exports = [
{
method: 'GET',
path: '/',
handler: 'Documentation.index',
handler: 'documentation.index',
config: {
policies: [
'plugin::documentation.index',
@ -15,7 +15,7 @@ module.exports = [
{
method: 'GET',
path: '/v:major(\\d+).:minor(\\d+).:patch(\\d+)',
handler: 'Documentation.index',
handler: 'documentation.index',
config: {
policies: [
'plugin::documentation.index',
@ -26,7 +26,7 @@ module.exports = [
{
method: 'GET',
path: '/login',
handler: 'Documentation.loginView',
handler: 'documentation.loginView',
config: {
policies: [
{ name: 'admin::hasPermissions', options: { actions: ['plugin::documentation.read'] } },
@ -36,7 +36,7 @@ module.exports = [
{
method: 'POST',
path: '/login',
handler: 'Documentation.login',
handler: 'documentation.login',
config: {
policies: [
{ name: 'admin::hasPermissions', options: { actions: ['plugin::documentation.read'] } },
@ -46,7 +46,7 @@ module.exports = [
{
method: 'GET',
path: '/getInfos',
handler: 'Documentation.getInfos',
handler: 'documentation.getInfos',
config: {
policies: [
{ name: 'admin::hasPermissions', options: { actions: ['plugin::documentation.read'] } },
@ -56,7 +56,7 @@ module.exports = [
{
method: 'POST',
path: '/regenerateDoc',
handler: 'Documentation.regenerateDoc',
handler: 'documentation.regenerateDoc',
config: {
policies: [
{
@ -69,7 +69,7 @@ module.exports = [
{
method: 'PUT',
path: '/updateSettings',
handler: 'Documentation.updateSettings',
handler: 'documentation.updateSettings',
config: {
policies: [
{
@ -82,7 +82,7 @@ module.exports = [
{
method: 'DELETE',
path: '/deleteDoc/:version',
handler: 'Documentation.deleteDoc',
handler: 'documentation.deleteDoc',
config: {
policies: [],
},