@strapi/generators 4.0.0-next.20 → 4.0.2

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 (45) hide show
  1. package/lib/files/plugin/admin/src/components/PluginIcon/index.js +12 -0
  2. package/lib/files/plugin/admin/src/index.js +2 -6
  3. package/lib/files/plugin/server/bootstrap.js +5 -0
  4. package/lib/files/plugin/server/config/index.js +6 -0
  5. package/lib/files/plugin/server/controllers/index.js +7 -0
  6. package/lib/files/plugin/server/controllers/my-controller.js.hbs +10 -0
  7. package/lib/files/plugin/server/destroy.js +5 -0
  8. package/lib/files/plugin/server/index.js +22 -0
  9. package/lib/files/plugin/server/register.js +5 -0
  10. package/lib/files/plugin/server/routes/index.js +10 -0
  11. package/lib/files/plugin/server/services/index.js +7 -0
  12. package/lib/files/plugin/server/services/my-service.js +7 -0
  13. package/lib/files/plugin/strapi-admin.js +3 -0
  14. package/lib/files/plugin/strapi-server.js +3 -0
  15. package/lib/plopfile.js +2 -0
  16. package/lib/plops/__tests__/content-type.test.js +179 -0
  17. package/lib/plops/api.js +2 -33
  18. package/lib/plops/content-type.js +100 -129
  19. package/lib/plops/controller.js +2 -2
  20. package/lib/plops/middleware.js +36 -0
  21. package/lib/plops/plugin.js +31 -22
  22. package/lib/plops/policy.js +2 -2
  23. package/lib/plops/prompts/bootstrap-api-prompts.js +10 -0
  24. package/lib/plops/prompts/ct-names-prompts.js +44 -0
  25. package/lib/plops/prompts/draft-and-publish-prompts.js +10 -0
  26. package/lib/plops/prompts/get-attributes-prompts.js +102 -0
  27. package/lib/plops/{utils → prompts}/get-destination-prompts.js +2 -2
  28. package/lib/plops/prompts/kind-prompts.js +17 -0
  29. package/lib/plops/service.js +2 -2
  30. package/lib/plops/utils/get-file-path.js +3 -3
  31. package/lib/plops/utils/validate-input.js +1 -1
  32. package/lib/templates/README.md.hbs +2 -2
  33. package/lib/templates/collection-type-routes.js.hbs +5 -0
  34. package/lib/templates/content-type.schema.json.hbs +15 -0
  35. package/lib/templates/core-controller.js.hbs +9 -0
  36. package/lib/templates/core-router.js.hbs +9 -0
  37. package/lib/templates/core-service.js.hbs +9 -0
  38. package/lib/templates/middleware.js.hbs +14 -0
  39. package/lib/templates/plugin-package.json.hbs +6 -10
  40. package/lib/templates/policy.js.hbs +8 -8
  41. package/lib/templates/single-route.js.hbs +13 -0
  42. package/lib/templates/single-type-routes.js.hbs +3 -0
  43. package/package.json +9 -6
  44. package/lib/templates/model.schema.json.hbs +0 -16
  45. package/lib/templates/plugin-routes.json.hbs +0 -12
@@ -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 = () => <Puzzle />;
11
+
12
+ export default PluginIcon;
@@ -2,16 +2,15 @@ import { prefixPluginTranslations } from '@strapi/helper-plugin';
2
2
  import pluginPkg from '../../package.json';
3
3
  import pluginId from './pluginId';
4
4
  import Initializer from './components/Initializer';
5
+ import PluginIcon from './components/PluginIcon';
5
6
 
6
- const pluginDescription = pluginPkg.strapi.description || pluginPkg.description;
7
- const icon = pluginPkg.strapi.icon;
8
7
  const name = pluginPkg.strapi.name;
9
8
 
10
9
  export default {
11
10
  register(app) {
12
11
  app.addMenuLink({
13
12
  to: `/plugins/${pluginId}`,
14
- icon,
13
+ icon: PluginIcon,
15
14
  intlLabel: {
16
15
  id: `${pluginId}.plugin.name`,
17
16
  defaultMessage: name,
@@ -30,12 +29,9 @@ export default {
30
29
  ],
31
30
  });
32
31
  app.registerPlugin({
33
- description: pluginDescription,
34
- icon,
35
32
  id: pluginId,
36
33
  initializer: Initializer,
37
34
  isReady: false,
38
- isRequired: pluginPkg.strapi.required || false,
39
35
  name,
40
36
  });
41
37
  },
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ strapi }) => {
4
+ // bootstrap phase
5
+ };
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ default: {},
5
+ validator() {},
6
+ };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const myController = require('./my-controller');
4
+
5
+ module.exports = {
6
+ myController,
7
+ };
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ index(ctx) {
5
+ ctx.body = strapi
6
+ .plugin('{{ pluginName }}')
7
+ .service('myService')
8
+ .getWelcomeMessage();
9
+ },
10
+ };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ strapi }) => {
4
+ // destroy phase
5
+ };
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const register = require('./register');
4
+ const bootstrap = require('./bootstrap');
5
+ const destroy = require('./destroy');
6
+ const config = require('./config');
7
+ const controllers = require('./controllers');
8
+ const routes = require('./routes');
9
+ const services = require('./services');
10
+
11
+ module.exports = {
12
+ register,
13
+ bootstrap,
14
+ destroy,
15
+ config,
16
+ controllers,
17
+ routes,
18
+ services,
19
+ contentTypes: {},
20
+ policies: {},
21
+ middlewares: {},
22
+ };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ strapi }) => {
4
+ // registeration phase
5
+ };
@@ -0,0 +1,10 @@
1
+ module.exports = [
2
+ {
3
+ method: 'GET',
4
+ path: '/',
5
+ handler: 'myController.index',
6
+ config: {
7
+ policies: [],
8
+ },
9
+ },
10
+ ];
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const myService = require('./my-service');
4
+
5
+ module.exports = {
6
+ myService,
7
+ };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ 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('./server');
package/lib/plopfile.js CHANGED
@@ -7,6 +7,7 @@ const generateController = require('./plops/controller');
7
7
  const generateContentType = require('./plops/content-type');
8
8
  const generatePlugin = require('./plops/plugin');
9
9
  const generatePolicy = require('./plops/policy');
10
+ const generateMiddleware = require('./plops/middleware');
10
11
  const generateService = require('./plops/service');
11
12
 
12
13
  module.exports = plop => {
@@ -20,5 +21,6 @@ module.exports = plop => {
20
21
  generateContentType(plop);
21
22
  generatePlugin(plop);
22
23
  generatePolicy(plop);
24
+ generateMiddleware(plop);
23
25
  generateService(plop);
24
26
  };
@@ -0,0 +1,179 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const { readFile, rm, stat } = require('fs-extra');
5
+ const strapiGenerators = require('../../index');
6
+
7
+ describe('Content Type Generator', () => {
8
+ const outputDirectory = path.join(__dirname, 'output');
9
+
10
+ afterEach(async () => {
11
+ await rm(outputDirectory, { recursive: true, force: true });
12
+ });
13
+
14
+ test('it generates the schema', async () => {
15
+ await strapiGenerators.generate(
16
+ 'content-type',
17
+ {
18
+ displayName: 'testContentType',
19
+ singularName: 'testContentType',
20
+ pluralName: 'testContentTypes',
21
+ kind: 'singleType',
22
+ id: 'testContentType',
23
+ useDraftAndPublish: false,
24
+ destination: 'new',
25
+ bootstrapApi: false,
26
+ attributes: [],
27
+ },
28
+ { dir: outputDirectory }
29
+ );
30
+
31
+ const generatedSchemaPath = path.join(
32
+ outputDirectory,
33
+ 'src/api/testContentType',
34
+ 'content-types/testContentType/schema.json'
35
+ );
36
+
37
+ expect((await stat(generatedSchemaPath)).isFile()).toBeTruthy();
38
+
39
+ const fileContent = await readFile(generatedSchemaPath, 'utf-8');
40
+
41
+ expect(fileContent).not.toBeNull();
42
+
43
+ const schema = JSON.parse(fileContent.toString());
44
+
45
+ expect(schema).toStrictEqual({
46
+ kind: 'singleType',
47
+ collectionName: 'test_content_types',
48
+ info: {
49
+ singularName: 'testContentType',
50
+ pluralName: 'testContentTypes',
51
+ displayName: 'testContentType',
52
+ },
53
+ options: {
54
+ draftAndPublish: false,
55
+ comment: '',
56
+ },
57
+ attributes: {},
58
+ });
59
+ });
60
+
61
+ test('it scaffolds a new API', async () => {
62
+ await strapiGenerators.generate(
63
+ 'content-type',
64
+ {
65
+ displayName: 'testContentType',
66
+ singularName: 'testContentType',
67
+ pluralName: 'testContentTypes',
68
+ kind: 'singleType',
69
+ id: 'testContentType',
70
+ useDraftAndPublish: false,
71
+ destination: 'new',
72
+ bootstrapApi: true,
73
+ attributes: [],
74
+ },
75
+ { dir: outputDirectory }
76
+ );
77
+ const generatedApiPath = path.join(outputDirectory, 'src/api/testContentType');
78
+
79
+ expect((await stat(generatedApiPath)).isDirectory()).toBeTruthy();
80
+ expect(
81
+ (await stat(path.join(generatedApiPath, 'controllers/testContentType.js'))).isFile()
82
+ ).toBeTruthy();
83
+ expect(
84
+ (await stat(path.join(generatedApiPath, 'services/testContentType.js'))).isFile()
85
+ ).toBeTruthy();
86
+ expect(
87
+ (await stat(path.join(generatedApiPath, 'routes/testContentType.js'))).isFile()
88
+ ).toBeTruthy();
89
+
90
+ const controller = await readFile(
91
+ path.join(generatedApiPath, 'controllers/testContentType.js')
92
+ );
93
+ const router = await readFile(path.join(generatedApiPath, 'routes/testContentType.js'));
94
+ const service = await readFile(path.join(generatedApiPath, 'services/testContentType.js'));
95
+
96
+ expect(controller.toString()).toMatchInlineSnapshot(`
97
+ "'use strict';
98
+
99
+ /**
100
+ * testContentType controller
101
+ */
102
+
103
+ const { createCoreController } = require('@strapi/strapi').factories;
104
+
105
+ module.exports = createCoreController('api::testContentType.testContentType');
106
+ "
107
+ `);
108
+ expect(router.toString()).toMatchInlineSnapshot(`
109
+ "'use strict';
110
+
111
+ /**
112
+ * testContentType router.
113
+ */
114
+
115
+ const { createCoreRouter } = require('@strapi/strapi').factories;
116
+
117
+ module.exports = createCoreRouter('api::testContentType.testContentType');
118
+ "
119
+ `);
120
+ expect(service.toString()).toMatchInlineSnapshot(`
121
+ "'use strict';
122
+
123
+ /**
124
+ * testContentType service.
125
+ */
126
+
127
+ const { createCoreService } = require('@strapi/strapi').factories;
128
+
129
+ module.exports = createCoreService('api::testContentType.testContentType');
130
+ "
131
+ `);
132
+ });
133
+
134
+ test('it generates the schema, then adds the attributes', async () => {
135
+ await strapiGenerators.generate(
136
+ 'content-type',
137
+ {
138
+ displayName: 'testContentType',
139
+ singularName: 'testContentType',
140
+ pluralName: 'testContentTypes',
141
+ kind: 'singleType',
142
+ id: 'testContentType',
143
+ useDraftAndPublish: false,
144
+ destination: 'new',
145
+ bootstrapApi: false,
146
+ attributes: [
147
+ {
148
+ attributeName: 'name',
149
+ attributeType: 'string',
150
+ },
151
+ {
152
+ attributeName: 'email',
153
+ attributeType: 'string',
154
+ },
155
+ ],
156
+ },
157
+ { dir: outputDirectory }
158
+ );
159
+
160
+ const generatedSchemaPath = path.join(
161
+ outputDirectory,
162
+ 'src/api/testContentType',
163
+ 'content-types/testContentType/schema.json'
164
+ );
165
+
166
+ expect((await stat(generatedSchemaPath)).isFile()).toBeTruthy();
167
+
168
+ const fileContent = await readFile(generatedSchemaPath, 'utf-8');
169
+
170
+ expect(fileContent).not.toBeNull();
171
+
172
+ const schema = JSON.parse(fileContent.toString());
173
+
174
+ expect(schema.attributes).toStrictEqual({
175
+ email: { type: 'string' },
176
+ name: { type: 'string' },
177
+ });
178
+ });
179
+ });
package/lib/plops/api.js CHANGED
@@ -43,30 +43,9 @@ module.exports = plop => {
43
43
  return pluginsDirContent;
44
44
  },
45
45
  },
46
- {
47
- type: 'list',
48
- name: 'kind',
49
- message: 'Please choose the model type',
50
- default: 'collectionType',
51
- choices: [
52
- { name: 'Collection Type', value: 'collectionType' },
53
- { name: 'Singe Type', value: 'singleType' },
54
- ],
55
- },
56
- {
57
- type: 'confirm',
58
- name: 'useDraftAndPublish',
59
- default: false,
60
- message: 'Use draft and publish?',
61
- },
62
46
  ],
63
47
  actions(answers) {
64
- let filePath;
65
- if (answers.isPluginApi && answers.plugin) {
66
- filePath = `plugins/{{plugin}}`;
67
- } else {
68
- filePath = `api/{{id}}`;
69
- }
48
+ const filePath = answers.isPluginApi && answers.plugin ? 'plugins/{{plugin}}' : 'api/{{id}}';
70
49
 
71
50
  const baseActions = [
72
51
  {
@@ -74,11 +53,6 @@ module.exports = plop => {
74
53
  path: `${filePath}/controllers/{{id}}.js`,
75
54
  templateFile: 'templates/controller.js.hbs',
76
55
  },
77
- {
78
- type: 'add',
79
- path: `${filePath}/content-types/{{id}}/schema.json`,
80
- templateFile: 'templates/model.schema.json.hbs',
81
- },
82
56
  {
83
57
  type: 'add',
84
58
  path: `${filePath}/services/{{id}}.js`,
@@ -90,16 +64,11 @@ module.exports = plop => {
90
64
  return baseActions;
91
65
  }
92
66
 
93
- const routeType =
94
- answers.kind === 'singleType'
95
- ? 'single-type-routes.js.hbs'
96
- : 'collection-type-routes.js.hbs';
97
-
98
67
  return [
99
68
  {
100
69
  type: 'add',
101
70
  path: `${filePath}/routes/{{id}}.js`,
102
- templateFile: `templates/${routeType}`,
71
+ templateFile: `templates/single-route.js.hbs`,
103
72
  },
104
73
  ...baseActions,
105
74
  ];
@@ -1,137 +1,66 @@
1
1
  'use strict';
2
2
 
3
- const getDestinationPrompts = require('./utils/get-destination-prompts');
4
- const getFilePath = require('./utils/get-file-path');
5
- const validateInput = require('./utils/validate-input');
6
-
7
- const DEFAULT_TYPES = [
8
- // advanced types
9
- 'media',
10
-
11
- // scalar types
12
- 'string',
13
- 'text',
14
- 'richtext',
15
- 'json',
16
- 'enumeration',
17
- 'password',
18
- 'email',
19
- 'integer',
20
- 'biginteger',
21
- 'float',
22
- 'decimal',
23
- 'date',
24
- 'time',
25
- 'datetime',
26
- 'timestamp',
27
- 'boolean',
28
- ];
29
-
30
- const promptConfigQuestions = (plop, inquirer) => {
31
- return inquirer.prompt([
32
- {
33
- type: 'input',
34
- name: 'id',
35
- message: 'Content type name',
36
- validate: input => validateInput(input),
37
- },
38
- {
39
- type: 'list',
40
- name: 'kind',
41
- message: 'Please choose the model type',
42
- default: 'collectionType',
43
- choices: [
44
- { name: 'Collection Type', value: 'collectionType' },
45
- { name: 'Singe Type', value: 'singleType' },
46
- ],
47
- validate: input => validateInput(input),
48
- },
49
- ...getDestinationPrompts('model', plop.getDestBasePath()),
50
- {
51
- type: 'confirm',
52
- name: 'useDraftAndPublish',
53
- default: false,
54
- message: 'Use draft and publish?',
55
- },
56
- {
57
- type: 'confirm',
58
- name: 'addAttributes',
59
- message: 'Do you want to add attributes?',
60
- },
61
- ]);
62
- };
3
+ const { join } = require('path');
4
+ const slugify = require('@sindresorhus/slugify');
5
+ const fs = require('fs-extra');
6
+ const { isKebabCase } = require('@strapi/utils');
63
7
 
64
- const promptAttributeQuestions = inquirer => {
65
- return inquirer.prompt([
66
- {
67
- type: 'input',
68
- name: 'attributeName',
69
- message: 'Name of attribute',
70
- validate: input => validateInput(input),
71
- },
72
- {
73
- type: 'list',
74
- name: 'attributeType',
75
- message: 'What type of attribute',
76
- pageSize: DEFAULT_TYPES.length,
77
- choices: DEFAULT_TYPES.map(type => {
78
- return { name: type, value: type };
79
- }),
80
- },
81
- {
82
- when: answers => answers.attributeType === 'enumeration',
83
- type: 'input',
84
- name: 'enum',
85
- message: 'Add values separated by a comma',
86
- },
87
- {
88
- when: answers => answers.attributeType === 'media',
89
- type: 'list',
90
- name: 'multiple',
91
- message: 'Choose media type',
92
- choices: [
93
- { name: 'Multiple', value: true },
94
- { name: 'Single', value: false },
95
- ],
96
- },
97
- {
98
- type: 'confirm',
99
- name: 'addAttributes',
100
- message: 'Do you want to add another attribute?',
101
- },
102
- ]);
103
- };
8
+ const getDestinationPrompts = require('./prompts/get-destination-prompts');
9
+ const getFilePath = require('./utils/get-file-path');
10
+ const ctNamesPrompts = require('./prompts/ct-names-prompts');
11
+ const kindPrompts = require('./prompts/kind-prompts');
12
+ const draftAndPublishPrompts = require('./prompts/draft-and-publish-prompts');
13
+ const getAttributesPrompts = require('./prompts/get-attributes-prompts');
14
+ const bootstrapApiPrompts = require('./prompts/bootstrap-api-prompts');
104
15
 
105
16
  module.exports = plop => {
106
17
  // Model generator
107
18
  plop.setGenerator('content-type', {
108
19
  description: 'Generate a content type for an API',
109
20
  async prompts(inquirer) {
110
- const config = await promptConfigQuestions(plop, inquirer);
111
-
112
- if (!config.addAttributes) {
113
- return {
114
- ...config,
115
- attributes: [],
116
- };
117
- }
118
-
119
- const attributes = [];
120
-
121
- const genAttribute = async () => {
122
- const answers = await promptAttributeQuestions(inquirer);
123
-
124
- attributes.push(answers);
125
-
126
- if (answers.addAttributes) {
127
- return genAttribute();
128
- }
129
- };
130
-
131
- await genAttribute();
21
+ const config = await inquirer.prompt([
22
+ ...ctNamesPrompts,
23
+ ...kindPrompts,
24
+ ...draftAndPublishPrompts,
25
+ ]);
26
+ const attributes = await getAttributesPrompts(inquirer);
27
+
28
+ const api = await inquirer.prompt([
29
+ ...getDestinationPrompts('model', plop.getDestBasePath()),
30
+ {
31
+ when: answers => answers.destination === 'new',
32
+ type: 'input',
33
+ name: 'id',
34
+ default: config.singularName,
35
+ message: 'Name of the new API?',
36
+ async validate(input) {
37
+ if (!isKebabCase(input)) {
38
+ return 'Value must be in kebab-case';
39
+ }
40
+
41
+ const apiPath = join(plop.getDestBasePath(), 'api');
42
+ const exists = await fs.pathExists(apiPath);
43
+
44
+ if (!exists) {
45
+ return true;
46
+ }
47
+
48
+ const apiDir = await fs.readdir(apiPath, { withFileTypes: true });
49
+ const apiDirContent = apiDir.filter(fd => fd.isDirectory());
50
+
51
+ if (apiDirContent.findIndex(api => api.name === input) !== -1) {
52
+ throw new Error('This name is already taken.');
53
+ }
54
+
55
+ return true;
56
+ },
57
+ },
58
+ ...bootstrapApiPrompts,
59
+ ]);
132
60
 
133
61
  return {
134
62
  ...config,
63
+ ...api,
135
64
  attributes,
136
65
  };
137
66
  },
@@ -153,22 +82,64 @@ module.exports = plop => {
153
82
 
154
83
  const filePath = getFilePath(answers.destination);
155
84
 
156
- return [
85
+ const baseActions = [
157
86
  {
158
87
  type: 'add',
159
- path: `${filePath}/content-types/{{id}}/schema.json`,
160
- templateFile: 'templates/model.schema.json.hbs',
88
+ path: `${filePath}/content-types/{{ singularName }}/schema.json`,
89
+ templateFile: 'templates/content-type.schema.json.hbs',
90
+ data: {
91
+ collectionName: slugify(answers.pluralName, { separator: '_' }),
92
+ },
161
93
  },
162
- {
94
+ ];
95
+
96
+ if (Object.entries(attributes).length > 0) {
97
+ baseActions.push({
163
98
  type: 'modify',
164
- path: `${filePath}/content-types/{{id}}/schema.json`,
99
+ path: `${filePath}/content-types/{{ singularName }}/schema.json`,
165
100
  transform(template) {
166
101
  const parsedTemplate = JSON.parse(template);
167
102
  parsedTemplate.attributes = attributes;
168
103
  return JSON.stringify(parsedTemplate, null, 2);
169
104
  },
170
- },
171
- ];
105
+ });
106
+ }
107
+
108
+ if (answers.bootstrapApi) {
109
+ const { singularName } = answers;
110
+
111
+ let uid;
112
+ if (answers.destination === 'new') {
113
+ uid = `api::${answers.id}.${singularName}`;
114
+ } else if (answers.api) {
115
+ uid = `api::${answers.api}.${singularName}`;
116
+ } else if (answers.plugin) {
117
+ uid = `plugin::${answers.plugin}.${singularName}`;
118
+ }
119
+
120
+ baseActions.push(
121
+ {
122
+ type: 'add',
123
+ path: `${filePath}/controllers/{{singularName}}.js`,
124
+ templateFile: 'templates/core-controller.js.hbs',
125
+ data: { uid },
126
+ },
127
+ {
128
+ type: 'add',
129
+ path: `${filePath}/services/{{singularName}}.js`,
130
+ templateFile: 'templates/core-service.js.hbs',
131
+ data: { uid },
132
+ },
133
+ {
134
+ type: 'add',
135
+ path: `${filePath}/routes/{{singularName}}.js`,
136
+ templateFile: `templates/core-router.js.hbs`,
137
+ data: { uid },
138
+ }
139
+ );
140
+ }
141
+
142
+ return baseActions;
172
143
  },
173
144
  });
174
145
  };