@strapi/generators 4.2.3 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/lib/files/{plugin → js/plugin}/admin/src/components/Initializer/index.js +0 -0
  2. package/lib/files/{plugin → js/plugin}/admin/src/components/PluginIcon/index.js +0 -0
  3. package/lib/files/{plugin → js/plugin}/admin/src/index.js +3 -1
  4. package/lib/files/{plugin → js/plugin}/admin/src/pages/App/index.js +0 -0
  5. package/lib/files/{plugin → js/plugin}/admin/src/pages/HomePage/index.js +2 -2
  6. package/lib/files/{plugin → js/plugin}/admin/src/pluginId.js +2 -2
  7. package/lib/files/{plugin → js/plugin}/admin/src/translations/en.json +0 -0
  8. package/lib/files/{plugin → js/plugin}/admin/src/translations/fr.json +0 -0
  9. package/lib/files/{plugin → js/plugin}/admin/src/utils/axiosInstance.js +0 -0
  10. package/lib/files/{plugin → js/plugin}/admin/src/utils/getTrad.js +0 -0
  11. package/lib/files/{plugin → js/plugin}/server/bootstrap.js +0 -0
  12. package/lib/files/{plugin → js/plugin}/server/config/index.js +0 -0
  13. package/lib/files/{plugin → js/plugin}/server/content-types/index.js +0 -0
  14. package/lib/files/{plugin → js/plugin}/server/controllers/index.js +0 -0
  15. package/lib/files/{plugin → js/plugin}/server/controllers/my-controller.js.hbs +2 -2
  16. package/lib/files/{plugin → js/plugin}/server/destroy.js +0 -0
  17. package/lib/files/{plugin → js/plugin}/server/index.js +0 -0
  18. package/lib/files/{plugin → js/plugin}/server/middlewares/index.js +0 -0
  19. package/lib/files/{plugin → js/plugin}/server/policies/index.js +0 -0
  20. package/lib/files/{plugin → js/plugin}/server/register.js +0 -0
  21. package/lib/files/{plugin → js/plugin}/server/routes/index.js +0 -0
  22. package/lib/files/{plugin → js/plugin}/server/services/index.js +0 -0
  23. package/lib/files/{plugin → js/plugin}/server/services/my-service.js +0 -0
  24. package/lib/files/{plugin → js/plugin}/strapi-admin.js +0 -0
  25. package/lib/files/{plugin → js/plugin}/strapi-server.js +0 -0
  26. package/lib/files/ts/plugin/admin/src/components/Initializer/index.tsx +25 -0
  27. package/lib/files/ts/plugin/admin/src/components/PluginIcon/index.tsx +12 -0
  28. package/lib/files/ts/plugin/admin/src/index.tsx +66 -0
  29. package/lib/files/ts/plugin/admin/src/pages/App/index.tsx +25 -0
  30. package/lib/files/ts/plugin/admin/src/pages/HomePage/index.tsx +19 -0
  31. package/lib/files/ts/plugin/admin/src/pluginId.ts +5 -0
  32. package/lib/files/ts/plugin/admin/src/translations/en.json +1 -0
  33. package/lib/files/ts/plugin/admin/src/translations/fr.json +1 -0
  34. package/lib/files/ts/plugin/admin/src/utils/axiosInstance.ts +40 -0
  35. package/lib/files/ts/plugin/admin/src/utils/getTrad.ts +5 -0
  36. package/lib/files/ts/plugin/server/bootstrap.ts +5 -0
  37. package/lib/files/ts/plugin/server/config/index.ts +4 -0
  38. package/lib/files/ts/plugin/server/content-types/index.ts +1 -0
  39. package/lib/files/ts/plugin/server/controllers/index.ts +5 -0
  40. package/lib/files/ts/plugin/server/controllers/my-controller.ts.hbs +10 -0
  41. package/lib/files/ts/plugin/server/destroy.ts +5 -0
  42. package/lib/files/ts/plugin/server/index.ts +23 -0
  43. package/lib/files/ts/plugin/server/middlewares/index.ts +1 -0
  44. package/lib/files/ts/plugin/server/policies/index.ts +1 -0
  45. package/lib/files/ts/plugin/server/register.ts +5 -0
  46. package/lib/files/ts/plugin/server/routes/index.ts +10 -0
  47. package/lib/files/ts/plugin/server/services/index.ts +5 -0
  48. package/lib/files/ts/plugin/server/services/my-service.ts +7 -0
  49. package/lib/files/ts/plugin/strapi-admin.js +3 -0
  50. package/lib/files/ts/plugin/strapi-server.js +3 -0
  51. package/lib/files/ts/plugin/tsconfig.json +25 -0
  52. package/lib/plops/api.js +10 -6
  53. package/lib/plops/content-type.js +10 -7
  54. package/lib/plops/controller.js +6 -2
  55. package/lib/plops/middleware.js +7 -2
  56. package/lib/plops/plugin.js +30 -8
  57. package/lib/plops/policy.js +6 -2
  58. package/lib/plops/service.js +6 -2
  59. package/lib/plops/utils/__tests__/get-file-path.test.js +5 -5
  60. package/lib/templates/{README.md.hbs → js/README.md.hbs} +0 -0
  61. package/lib/templates/{collection-type-routes.js.hbs → js/collection-type-routes.js.hbs} +0 -0
  62. package/lib/templates/{content-type.schema.json.hbs → js/content-type.schema.json.hbs} +0 -0
  63. package/lib/templates/{controller.js.hbs → js/controller.js.hbs} +0 -0
  64. package/lib/templates/{core-controller.js.hbs → js/core-controller.js.hbs} +0 -0
  65. package/lib/templates/{core-router.js.hbs → js/core-router.js.hbs} +0 -0
  66. package/lib/templates/{core-service.js.hbs → js/core-service.js.hbs} +0 -0
  67. package/lib/templates/{middleware.js.hbs → js/middleware.js.hbs} +0 -0
  68. package/lib/templates/{plugin-package.json.hbs → js/plugin-package.json.hbs} +0 -0
  69. package/lib/templates/{policy.js.hbs → js/policy.js.hbs} +0 -0
  70. package/lib/templates/{service.js.hbs → js/service.js.hbs} +0 -0
  71. package/lib/templates/{single-route.js.hbs → js/single-route.js.hbs} +0 -0
  72. package/lib/templates/{single-type-routes.js.hbs → js/single-type-routes.js.hbs} +2 -0
  73. package/lib/templates/ts/README.md.hbs +3 -0
  74. package/lib/templates/ts/collection-type-routes.ts.hbs +49 -0
  75. package/lib/templates/ts/content-type.schema.json.hbs +15 -0
  76. package/lib/templates/ts/controller.ts.hbs +13 -0
  77. package/lib/templates/ts/core-controller.ts.hbs +7 -0
  78. package/lib/templates/ts/core-router.ts.hbs +7 -0
  79. package/lib/templates/ts/core-service.ts.hbs +7 -0
  80. package/lib/templates/ts/middleware.ts.hbs +14 -0
  81. package/lib/templates/ts/plugin-package.json.hbs +32 -0
  82. package/lib/templates/ts/policy.ts.hbs +16 -0
  83. package/lib/templates/ts/service.ts.hbs +5 -0
  84. package/lib/templates/ts/single-route.ts.hbs +13 -0
  85. package/lib/templates/ts/single-type-routes.ts.hbs +31 -0
  86. package/package.json +4 -3
@@ -40,7 +40,9 @@ export default {
40
40
  async registerTrads({ locales }) {
41
41
  const importedTrads = await Promise.all(
42
42
  locales.map(locale => {
43
- return import(`./translations/${locale}.json`)
43
+ return import(
44
+ /* webpackChunkName: "translation-[request]" */ `./translations/${locale}.json`
45
+ )
44
46
  .then(({ default: data }) => {
45
47
  return {
46
48
  data: prefixPluginTranslations(data, pluginId),
@@ -4,7 +4,7 @@
4
4
  *
5
5
  */
6
6
 
7
- import React, { memo } from 'react';
7
+ import React from 'react';
8
8
  // import PropTypes from 'prop-types';
9
9
  import pluginId from '../../pluginId';
10
10
 
@@ -17,4 +17,4 @@ const HomePage = () => {
17
17
  );
18
18
  };
19
19
 
20
- export default memo(HomePage);
20
+ export default HomePage;
@@ -1,5 +1,5 @@
1
- const pluginPkg = require('../../package.json');
1
+ import pluginPkg from '../../package.json';
2
2
 
3
3
  const pluginId = pluginPkg.name.replace(/^(@[^-,.][\w,-]+\/|strapi-)plugin-/i, '');
4
4
 
5
- module.exports = pluginId;
5
+ export default pluginId;
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- module.exports = {
3
+ module.exports = ({ strapi }) => ({
4
4
  index(ctx) {
5
5
  ctx.body = strapi
6
6
  .plugin('{{ pluginName }}')
7
7
  .service('myService')
8
8
  .getWelcomeMessage();
9
9
  },
10
- };
10
+ });
File without changes
File without changes
File without changes
@@ -0,0 +1,25 @@
1
+ /**
2
+ *
3
+ * Initializer
4
+ *
5
+ */
6
+
7
+ import React, { useEffect, useRef } from 'react';
8
+ import pluginId from '../../pluginId';
9
+
10
+ type InitializerProps = {
11
+ setPlugin: (id: string) => void;
12
+ };
13
+
14
+ const Initializer: React.FC<InitializerProps> = ({ setPlugin }) => {
15
+ const ref = useRef<(id: string) => void | null>(null);
16
+ ref.current = setPlugin;
17
+
18
+ useEffect(() => {
19
+ ref.current(pluginId);
20
+ }, []);
21
+
22
+ return null;
23
+ };
24
+
25
+ export default Initializer;
@@ -0,0 +1,12 @@
1
+ /**
2
+ *
3
+ * PluginIcon
4
+ *
5
+ */
6
+
7
+ import React from 'react';
8
+ import Puzzle from '@strapi/icons/Puzzle';
9
+
10
+ const PluginIcon: React.VoidFunctionComponent = () => <Puzzle />;
11
+
12
+ export default PluginIcon;
@@ -0,0 +1,66 @@
1
+ import React from 'react';
2
+ import { prefixPluginTranslations } from '@strapi/helper-plugin';
3
+ import pluginPkg from '../../package.json';
4
+ import pluginId from './pluginId';
5
+ import Initializer from './components/Initializer';
6
+ import PluginIcon from './components/PluginIcon';
7
+
8
+ const name = pluginPkg.strapi.name;
9
+
10
+ export default {
11
+ register(app) {
12
+ app.addMenuLink({
13
+ to: `/plugins/${pluginId}`,
14
+ icon: PluginIcon,
15
+ intlLabel: {
16
+ id: `${pluginId}.plugin.name`,
17
+ defaultMessage: name,
18
+ },
19
+ Component: async () => {
20
+ const component = await import(/* webpackChunkName: "[request]" */ './pages/App');
21
+
22
+ return component;
23
+ },
24
+ permissions: [
25
+ // Uncomment to set the permissions of the plugin here
26
+ // {
27
+ // action: '', // the action name should be plugin::plugin-name.actionType
28
+ // subject: null,
29
+ // },
30
+ ],
31
+ });
32
+ const plugin = {
33
+ id: pluginId,
34
+ initializer: Initializer,
35
+ isReady: false,
36
+ name,
37
+ };
38
+
39
+ app.registerPlugin(plugin);
40
+ },
41
+
42
+ bootstrap(app) {},
43
+ async registerTrads(app) {
44
+ const { locales } = app;
45
+
46
+ const importedTrads = await Promise.all(
47
+ locales.map(locale => {
48
+ return import(`./translations/${locale}.json`)
49
+ .then(({ default: data }) => {
50
+ return {
51
+ data: prefixPluginTranslations(data, pluginId),
52
+ locale,
53
+ };
54
+ })
55
+ .catch(() => {
56
+ return {
57
+ data: {},
58
+ locale,
59
+ };
60
+ });
61
+ })
62
+ );
63
+
64
+ return Promise.resolve(importedTrads);
65
+ },
66
+ };
@@ -0,0 +1,25 @@
1
+ /**
2
+ *
3
+ * This component is the skeleton around the actual pages, and should only
4
+ * contain code that should be seen on all pages. (e.g. navigation bar)
5
+ *
6
+ */
7
+
8
+ import React from 'react';
9
+ import { Switch, Route } from 'react-router-dom';
10
+ import { NotFound } from '@strapi/helper-plugin';
11
+ import pluginId from '../../pluginId';
12
+ import HomePage from '../HomePage';
13
+
14
+ const App: React.VoidFunctionComponent = () => {
15
+ return (
16
+ <div>
17
+ <Switch>
18
+ <Route path={`/plugins/${pluginId}`} component={HomePage} exact />
19
+ <Route component={NotFound} />
20
+ </Switch>
21
+ </div>
22
+ );
23
+ };
24
+
25
+ export default App;
@@ -0,0 +1,19 @@
1
+ /*
2
+ *
3
+ * HomePage
4
+ *
5
+ */
6
+
7
+ import React from 'react';
8
+ import pluginId from '../../pluginId';
9
+
10
+ const HomePage: React.VoidFunctionComponent = () => {
11
+ return (
12
+ <div>
13
+ <h1>{pluginId}&apos;s HomePage</h1>
14
+ <p>Happy coding</p>
15
+ </div>
16
+ );
17
+ };
18
+
19
+ export default HomePage;
@@ -0,0 +1,5 @@
1
+ import pluginPkg from '../../package.json';
2
+
3
+ const pluginId = pluginPkg.name.replace(/^(@[^-,.][\w,-]+\/|strapi-)plugin-/i, '');
4
+
5
+ export default pluginId;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * axios with a custom config.
3
+ */
4
+
5
+ import axios from 'axios';
6
+ import { auth } from '@strapi/helper-plugin';
7
+
8
+ const instance = axios.create({
9
+ baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
10
+ });
11
+
12
+ instance.interceptors.request.use(
13
+ async (config) => {
14
+ config.headers = {
15
+ Authorization: `Bearer ${auth.getToken()}`,
16
+ Accept: 'application/json',
17
+ 'Content-Type': 'application/json',
18
+ };
19
+
20
+ return config;
21
+ },
22
+ (error) => {
23
+ Promise.reject(error);
24
+ }
25
+ );
26
+
27
+ instance.interceptors.response.use(
28
+ (response) => response,
29
+ (error) => {
30
+ // whatever you want to do with the error
31
+ if (error.response?.status === 401) {
32
+ auth.clearAppStorage();
33
+ window.location.reload();
34
+ }
35
+
36
+ throw error;
37
+ }
38
+ );
39
+
40
+ export default instance;
@@ -0,0 +1,5 @@
1
+ import pluginId from '../pluginId';
2
+
3
+ const getTrad = (id: string) => `${pluginId}.${id}`;
4
+
5
+ export default getTrad;
@@ -0,0 +1,5 @@
1
+ import '@strapi/strapi';
2
+
3
+ export default ({ strapi }: { strapi: Strapi }) => {
4
+ // bootstrap phase
5
+ };
@@ -0,0 +1,4 @@
1
+ export default {
2
+ default: {},
3
+ validator() {},
4
+ };
@@ -0,0 +1 @@
1
+ export default {};
@@ -0,0 +1,5 @@
1
+ import myController from './my-controller';
2
+
3
+ export default {
4
+ myController,
5
+ };
@@ -0,0 +1,10 @@
1
+ import '@strapi/strapi';
2
+
3
+ export default ({ strapi }: { strapi: Strapi }) => ({
4
+ index(ctx) {
5
+ ctx.body = strapi
6
+ .plugin('{{ pluginName }}')
7
+ .service('myService')
8
+ .getWelcomeMessage();
9
+ },
10
+ });
@@ -0,0 +1,5 @@
1
+ import '@strapi/strapi';
2
+
3
+ export default ({ strapi }: { strapi: Strapi }) => {
4
+ // destroy phase
5
+ };
@@ -0,0 +1,23 @@
1
+ import register from './register';
2
+ import bootstrap from './bootstrap';
3
+ import destroy from './destroy';
4
+ import config from './config';
5
+ import contentTypes from './content-types';
6
+ import controllers from './controllers';
7
+ import routes from './routes';
8
+ import middlewares from './middlewares';
9
+ import policies from './policies';
10
+ import services from './services';
11
+
12
+ export default {
13
+ register,
14
+ bootstrap,
15
+ destroy,
16
+ config,
17
+ controllers,
18
+ routes,
19
+ services,
20
+ contentTypes,
21
+ policies,
22
+ middlewares,
23
+ };
@@ -0,0 +1 @@
1
+ export default {};
@@ -0,0 +1 @@
1
+ export default {};
@@ -0,0 +1,5 @@
1
+ import '@strapi/strapi';
2
+
3
+ export default ({ strapi }: { strapi: Strapi }) => {
4
+ // registeration phase
5
+ };
@@ -0,0 +1,10 @@
1
+ export default [
2
+ {
3
+ method: 'GET',
4
+ path: '/',
5
+ handler: 'myController.index',
6
+ config: {
7
+ policies: [],
8
+ },
9
+ },
10
+ ];
@@ -0,0 +1,5 @@
1
+ import myService from './my-service';
2
+
3
+ export default {
4
+ myService,
5
+ };
@@ -0,0 +1,7 @@
1
+ import '@strapi/strapi';
2
+
3
+ export default ({ strapi }: { strapi: Strapi }) => ({
4
+ getWelcomeMessage() {
5
+ return 'Welcome to Strapi 🚀';
6
+ },
7
+ });
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ module.exports = require('./admin/src').default;
@@ -0,0 +1,3 @@
1
+ 'use strict';
2
+
3
+ module.exports = require('./dist/server');
@@ -0,0 +1,25 @@
1
+ {
2
+ "extends": "@strapi/typescript-utils/tsconfigs/server",
3
+
4
+ "compilerOptions": {
5
+ "outDir": "dist",
6
+ "rootDir": "."
7
+ },
8
+
9
+ "include": [
10
+ // Include the root directory
11
+ "server",
12
+ // Force the JSON files in the src folder to be included
13
+ "server/**/*.json"
14
+ ],
15
+
16
+ "exclude": [
17
+ "node_modules/",
18
+ "dist/",
19
+
20
+ // Do not include admin files in the server compilation
21
+ "admin/",
22
+ // Do not include test files
23
+ "**/*.test.ts"
24
+ ]
25
+ }
package/lib/plops/api.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  const { join } = require('path');
4
4
  const fs = require('fs-extra');
5
+ const tsUtils = require('@strapi/typescript-utils');
6
+
5
7
  const validateInput = require('./utils/validate-input');
6
8
 
7
9
  module.exports = plop => {
@@ -46,17 +48,19 @@ module.exports = plop => {
46
48
  ],
47
49
  actions(answers) {
48
50
  const filePath = answers.isPluginApi && answers.plugin ? 'plugins/{{plugin}}' : 'api/{{id}}';
51
+ const currentDir = process.cwd();
52
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
49
53
 
50
54
  const baseActions = [
51
55
  {
52
56
  type: 'add',
53
- path: `${filePath}/controllers/{{id}}.js`,
54
- templateFile: 'templates/controller.js.hbs',
57
+ path: `${filePath}/controllers/{{id}}.${language}`,
58
+ templateFile: `templates/${language}/controller.${language}.hbs`,
55
59
  },
56
60
  {
57
61
  type: 'add',
58
- path: `${filePath}/services/{{id}}.js`,
59
- templateFile: 'templates/service.js.hbs',
62
+ path: `${filePath}/services/{{id}}.${language}`,
63
+ templateFile: `templates/${language}/service.${language}.hbs`,
60
64
  },
61
65
  ];
62
66
 
@@ -67,8 +71,8 @@ module.exports = plop => {
67
71
  return [
68
72
  {
69
73
  type: 'add',
70
- path: `${filePath}/routes/{{id}}.js`,
71
- templateFile: `templates/single-route.js.hbs`,
74
+ path: `${filePath}/routes/{{id}}.${language}`,
75
+ templateFile: `templates/${language}/single-route.${language}.hbs`,
72
76
  },
73
77
  ...baseActions,
74
78
  ];
@@ -4,6 +4,7 @@ const { join } = require('path');
4
4
  const slugify = require('@sindresorhus/slugify');
5
5
  const fs = require('fs-extra');
6
6
  const { isKebabCase } = require('@strapi/utils');
7
+ const tsUtils = require('@strapi/typescript-utils');
7
8
 
8
9
  const getDestinationPrompts = require('./prompts/get-destination-prompts');
9
10
  const getFilePath = require('./utils/get-file-path');
@@ -81,12 +82,14 @@ module.exports = plop => {
81
82
  }, {});
82
83
 
83
84
  const filePath = getFilePath(answers.destination);
85
+ const currentDir = process.cwd();
86
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
84
87
 
85
88
  const baseActions = [
86
89
  {
87
90
  type: 'add',
88
91
  path: `${filePath}/content-types/{{ singularName }}/schema.json`,
89
- templateFile: 'templates/content-type.schema.json.hbs',
92
+ templateFile: `templates/${language}/content-type.schema.json.hbs`,
90
93
  data: {
91
94
  collectionName: slugify(answers.pluralName, { separator: '_' }),
92
95
  },
@@ -120,20 +123,20 @@ module.exports = plop => {
120
123
  baseActions.push(
121
124
  {
122
125
  type: 'add',
123
- path: `${filePath}/controllers/{{singularName}}.js`,
124
- templateFile: 'templates/core-controller.js.hbs',
126
+ path: `${filePath}/controllers/{{singularName}}.${language}`,
127
+ templateFile: `templates/${language}/core-controller.${language}.hbs`,
125
128
  data: { uid },
126
129
  },
127
130
  {
128
131
  type: 'add',
129
- path: `${filePath}/services/{{singularName}}.js`,
130
- templateFile: 'templates/core-service.js.hbs',
132
+ path: `${filePath}/services/{{singularName}}.${language}`,
133
+ templateFile: `templates/${language}/core-service.${language}.hbs`,
131
134
  data: { uid },
132
135
  },
133
136
  {
134
137
  type: 'add',
135
- path: `${filePath}/routes/{{singularName}}.js`,
136
- templateFile: `templates/core-router.js.hbs`,
138
+ path: `${filePath}/routes/{{singularName}}.${language}`,
139
+ templateFile: `templates/${language}/core-router.${language}.hbs`,
137
140
  data: { uid },
138
141
  }
139
142
  );
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const tsUtils = require('@strapi/typescript-utils');
4
+
3
5
  const getDestinationPrompts = require('./prompts/get-destination-prompts');
4
6
  const getFilePath = require('./utils/get-file-path');
5
7
  const validateInput = require('./utils/validate-input');
@@ -19,12 +21,14 @@ module.exports = plop => {
19
21
  ],
20
22
  actions(answers) {
21
23
  const filePath = getFilePath(answers.destination);
24
+ const currentDir = process.cwd();
25
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
22
26
 
23
27
  return [
24
28
  {
25
29
  type: 'add',
26
- path: `${filePath}/controllers/{{ id }}.js`,
27
- templateFile: 'templates/controller.js.hbs',
30
+ path: `${filePath}/controllers/{{ id }}.${language}`,
31
+ templateFile: `templates/${language}/controller.${language}.hbs`,
28
32
  },
29
33
  ];
30
34
  },
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const tsUtils = require('@strapi/typescript-utils');
4
+
3
5
  const getDestinationPrompts = require('./prompts/get-destination-prompts');
4
6
  const validateInput = require('./utils/validate-input');
5
7
  const getFilePath = require('./utils/get-file-path');
@@ -19,11 +21,14 @@ module.exports = plop => {
19
21
  ],
20
22
  actions(answers) {
21
23
  const filePath = getFilePath(answers.destination);
24
+ const currentDir = process.cwd();
25
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
26
+
22
27
  return [
23
28
  {
24
29
  type: 'add',
25
- path: `${filePath}/middlewares/{{ name }}.js`,
26
- templateFile: 'templates/middleware.js.hbs',
30
+ path: `${filePath}/middlewares/{{ name }}.${language}`,
31
+ templateFile: `templates/${language}/middleware.${language}.hbs`,
27
32
  },
28
33
  ];
29
34
  },
@@ -1,19 +1,30 @@
1
1
  'use strict';
2
2
 
3
3
  const chalk = require('chalk');
4
+ const { isUsingTypeScriptSync } = require('@strapi/typescript-utils');
4
5
  const { isKebabCase, toKebabCase } = require('@strapi/utils');
6
+
5
7
  const validateInput = require('./utils/validate-input');
6
8
 
7
- const logInstructions = pluginName => {
9
+ const LANGUAGES = {
10
+ javascript: 'JavaScript',
11
+ typescript: 'TypeScript',
12
+ };
13
+
14
+ const logInstructions = (pluginName, { language }) => {
8
15
  const maxLength = ` resolve: './src/plugins/${pluginName}'`.length;
9
16
  const separator = Array(maxLength)
10
17
  .fill('─')
11
18
  .join('');
12
19
 
20
+ const exportInstruction = language === 'js' ? 'module.exports =' : 'export default';
21
+
13
22
  return `
14
- You can now enable your plugin by adding the following in ${chalk.yellow('./config/plugins.js')}.
23
+ You can now enable your plugin by adding the following in ${chalk.yellow(
24
+ `./config/plugins.${language}`
25
+ )}
15
26
  ${separator}
16
- module.exports = {
27
+ ${exportInstruction} {
17
28
  ${chalk.gray('// ...')}
18
29
  ${chalk.green(`'${pluginName}'`)}: {
19
30
  enabled: ${chalk.yellow(true)},
@@ -36,8 +47,19 @@ module.exports = plop => {
36
47
  message: 'Plugin name',
37
48
  validate: input => validateInput(input),
38
49
  },
50
+ {
51
+ type: 'list',
52
+ name: 'language',
53
+ message: 'Choose your preferred language',
54
+ choices: Object.values(LANGUAGES),
55
+ default: LANGUAGES.javascript,
56
+ },
39
57
  ],
40
58
  actions(answers) {
59
+ const isTypescript = answers.language === LANGUAGES.typescript;
60
+ const language = isTypescript ? 'ts' : 'js';
61
+ const projectLanguage = isUsingTypeScriptSync(process.cwd()) ? 'ts' : 'js';
62
+
41
63
  if (!isKebabCase(answers.pluginName)) {
42
64
  answers.pluginName = toKebabCase(answers.pluginName);
43
65
  console.log(
@@ -51,20 +73,20 @@ module.exports = plop => {
51
73
  {
52
74
  type: 'addMany',
53
75
  destination: 'plugins/{{ pluginName }}',
54
- base: 'files/plugin',
55
- templateFiles: 'files/plugin/**',
76
+ base: `files/${language}/plugin`,
77
+ templateFiles: `files/${language}/plugin/**`,
56
78
  },
57
79
  {
58
80
  type: 'add',
59
81
  path: 'plugins/{{ pluginName }}/README.md',
60
- templateFile: 'templates/README.md.hbs',
82
+ templateFile: `templates/${language}/README.md.hbs`,
61
83
  },
62
84
  {
63
85
  type: 'add',
64
86
  path: 'plugins/{{ pluginName }}/package.json',
65
- templateFile: 'templates/plugin-package.json.hbs',
87
+ templateFile: `templates/${language}/plugin-package.json.hbs`,
66
88
  },
67
- () => plop.renderString(logInstructions(answers.pluginName)),
89
+ () => plop.renderString(logInstructions(answers.pluginName, { language: projectLanguage })),
68
90
  ];
69
91
  },
70
92
  });
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const tsUtils = require('@strapi/typescript-utils');
4
+
3
5
  const getDestinationPrompts = require('./prompts/get-destination-prompts');
4
6
  const validateInput = require('./utils/validate-input');
5
7
  const getFilePath = require('./utils/get-file-path');
@@ -19,12 +21,14 @@ module.exports = plop => {
19
21
  ],
20
22
  actions(answers) {
21
23
  const filePath = getFilePath(answers.destination);
24
+ const currentDir = process.cwd();
25
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
22
26
 
23
27
  return [
24
28
  {
25
29
  type: 'add',
26
- path: `${filePath}/policies/{{id}}.js`,
27
- templateFile: 'templates/policy.js.hbs',
30
+ path: `${filePath}/policies/{{id}}.${language}`,
31
+ templateFile: `templates/${language}/policy.${language}.hbs`,
28
32
  },
29
33
  ];
30
34
  },
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const tsUtils = require('@strapi/typescript-utils');
4
+
3
5
  const getDestinationPrompts = require('./prompts/get-destination-prompts');
4
6
  const getFilePath = require('./utils/get-file-path');
5
7
 
@@ -17,12 +19,14 @@ module.exports = plop => {
17
19
  ],
18
20
  actions(answers) {
19
21
  const filePath = getFilePath(answers.destination);
22
+ const currentDir = process.cwd();
23
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
20
24
 
21
25
  return [
22
26
  {
23
27
  type: 'add',
24
- path: `${filePath}/services/{{ id }}.js`,
25
- templateFile: 'templates/service.js.hbs',
28
+ path: `${filePath}/services/{{ id }}.${language}`,
29
+ templateFile: `templates/${language}/service.${language}.hbs`,
26
30
  },
27
31
  ];
28
32
  },
@@ -6,20 +6,20 @@ describe('Get-File-Path util', () => {
6
6
  test('with destination set as api', () => {
7
7
  const filePath = getFilePath('api');
8
8
  expect(filePath).toBe(`api/{{ api }}`);
9
- })
9
+ });
10
10
 
11
11
  test('with destination set as plugin', () => {
12
12
  const filePath = getFilePath('plugin');
13
13
  expect(filePath).toBe(`plugins/{{ plugin }}/server`);
14
- })
14
+ });
15
15
 
16
16
  test('with destination set as root', () => {
17
17
  const filePath = getFilePath('root');
18
18
  expect(filePath).toBe(`./`);
19
- })
19
+ });
20
20
 
21
21
  test('with empty destination string', () => {
22
22
  const filePath = getFilePath('');
23
23
  expect(filePath).toBe(`api/{{ id }}`);
24
- })
25
- })
24
+ });
25
+ });
@@ -1,3 +1,5 @@
1
+ 'use strict';
2
+
1
3
  module.exports = {
2
4
  routes: [
3
5
  {
@@ -0,0 +1,3 @@
1
+ # Strapi plugin {{ pluginName }}
2
+
3
+ A quick description of {{ pluginName }}.
@@ -0,0 +1,49 @@
1
+ export default {
2
+ routes: [
3
+ {
4
+ method: 'GET',
5
+ path: '/{{pluralize id}}',
6
+ handler: '{{id}}.find',
7
+ config: {
8
+ policies: [],
9
+ middlewares: [],
10
+ },
11
+ },
12
+ {
13
+ method: 'GET',
14
+ path: '/{{pluralize id}}/:id',
15
+ handler: '{{id}}.findOne',
16
+ config: {
17
+ policies: [],
18
+ middlewares: [],
19
+ },
20
+ },
21
+ {
22
+ method: 'POST',
23
+ path: '/{{pluralize id}}',
24
+ handler: '{{id}}.create',
25
+ config: {
26
+ policies: [],
27
+ middlewares: [],
28
+ },
29
+ },
30
+ {
31
+ method: 'PUT',
32
+ path: '/{{pluralize id}}/:id',
33
+ handler: '{{id}}.update',
34
+ config: {
35
+ policies: [],
36
+ middlewares: [],
37
+ },
38
+ },
39
+ {
40
+ method: 'DELETE',
41
+ path: '/{{pluralize id}}/:id',
42
+ handler: '{{id}}.delete',
43
+ config: {
44
+ policies: [],
45
+ middlewares: [],
46
+ },
47
+ },
48
+ ],
49
+ };
@@ -0,0 +1,15 @@
1
+
2
+ {
3
+ "kind": "{{kind}}",
4
+ "collectionName": "{{ collectionName }}",
5
+ "info": {
6
+ "singularName": "{{ singularName }}",
7
+ "pluralName": "{{ pluralName }}",
8
+ "displayName": "{{ displayName }}"
9
+ },
10
+ "options": {
11
+ "draftAndPublish": {{ useDraftAndPublish }},
12
+ "comment": ""
13
+ },
14
+ "attributes": {}
15
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A set of functions called "actions" for `{{id}}`
3
+ */
4
+
5
+ export default {
6
+ // exampleAction: async (ctx, next) => {
7
+ // try {
8
+ // ctx.body = 'ok';
9
+ // } catch (err) {
10
+ // ctx.body = err;
11
+ // }
12
+ // }
13
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * {{ id }} controller
3
+ */
4
+
5
+ import { factories } from '@strapi/strapi'
6
+
7
+ export default factories.createCoreController('{{ uid }}');
@@ -0,0 +1,7 @@
1
+ /**
2
+ * {{ id }} router.
3
+ */
4
+
5
+ import { factories } from '@strapi/strapi';
6
+
7
+ export default factories.createCoreRouter('{{ uid }}');
@@ -0,0 +1,7 @@
1
+ /**
2
+ * {{ id }} service.
3
+ */
4
+
5
+ import { factories } from '@strapi/strapi';
6
+
7
+ export default factories.createCoreService('{{ uid }}');
@@ -0,0 +1,14 @@
1
+ /**
2
+ * `{{ name }}` middleware.
3
+ */
4
+
5
+ import '@strapi/strapi';
6
+
7
+ export default (config, { strapi }: { strapi: Strapi }) => {
8
+ // Add your own logic here.
9
+ return async (ctx, next) => {
10
+ strapi.log.info('In {{ name }} middleware.');
11
+
12
+ await next();
13
+ };
14
+ };
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "{{ pluginName }}",
3
+ "version": "0.0.0",
4
+ "description": "This is the description of the plugin.",
5
+ "strapi": {
6
+ "name": "{{ pluginName }}",
7
+ "description": "Description of {{ pluginName }} plugin",
8
+ "kind": "plugin"
9
+ },
10
+ "dependencies": {},
11
+ "devDependencies": {
12
+ "typescript": "4.6.3",
13
+ "@strapi/strapi": "4.1.8"
14
+ },
15
+ "author": {
16
+ "name": "A Strapi developer"
17
+ },
18
+ "maintainers": [
19
+ {
20
+ "name": "A Strapi developer"
21
+ }
22
+ ],
23
+ "engines": {
24
+ "node": ">=12.x.x <=16.x.x",
25
+ "npm": ">=6.0.0"
26
+ },
27
+ "scripts": {
28
+ "develop": "tsc -w",
29
+ "build": "tsc"
30
+ },
31
+ "license": "MIT"
32
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `{{id}}` policy.
3
+ */
4
+
5
+ export default (policyContext, config, { strapi }) => {
6
+ // Add your own logic here.
7
+ strapi.log.info('In {{id}} policy.');
8
+
9
+ const canDoSomething = true;
10
+
11
+ if (canDoSomething) {
12
+ return true;
13
+ }
14
+
15
+ return false;
16
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * {{id}} service.
3
+ */
4
+
5
+ export default () => ({});
@@ -0,0 +1,13 @@
1
+ export default {
2
+ routes: [
3
+ // {
4
+ // method: 'GET',
5
+ // path: '/{{id}}',
6
+ // handler: '{{id}}.exampleAction',
7
+ // config: {
8
+ // policies: [],
9
+ // middlewares: [],
10
+ // },
11
+ // },
12
+ ],
13
+ };
@@ -0,0 +1,31 @@
1
+ export default {
2
+ routes: [
3
+ {
4
+ method: 'GET',
5
+ path: '/{{id}}',
6
+ handler: '{{id}}.find',
7
+ config: {
8
+ policies: [],
9
+ middlewares: [],
10
+ },
11
+ },
12
+ {
13
+ method: 'PUT',
14
+ path: '/{{id}}',
15
+ handler: '{{id}}.update',
16
+ config: {
17
+ policies: [],
18
+ middlewares: [],
19
+ },
20
+ },
21
+ {
22
+ method: 'DELETE',
23
+ path: '/{{id}}',
24
+ handler: '{{id}}.delete',
25
+ config: {
26
+ policies: [],
27
+ middlewares: [],
28
+ },
29
+ },
30
+ ],
31
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/generators",
3
- "version": "4.2.3",
3
+ "version": "4.3.0",
4
4
  "description": "Interactive API generator.",
5
5
  "keywords": [
6
6
  "strapi",
@@ -30,7 +30,8 @@
30
30
  "main": "lib/index.js",
31
31
  "dependencies": {
32
32
  "@sindresorhus/slugify": "1.1.0",
33
- "@strapi/utils": "4.2.3",
33
+ "@strapi/typescript-utils": "4.3.0",
34
+ "@strapi/utils": "4.3.0",
34
35
  "chalk": "4.1.2",
35
36
  "fs-extra": "10.0.0",
36
37
  "node-plop": "0.26.3",
@@ -41,5 +42,5 @@
41
42
  "node": ">=14.19.1 <=16.x.x",
42
43
  "npm": ">=6.0.0"
43
44
  },
44
- "gitHead": "fe296baf71cb932d45183d5335285eaf30a6fad6"
45
+ "gitHead": "74a2b908df75bc8001d72f9dc8571c4b7a2da337"
45
46
  }