@strapi/generators 4.0.0 → 4.0.1

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.
@@ -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
+ });
@@ -93,7 +93,7 @@ module.exports = plop => {
93
93
  },
94
94
  ];
95
95
 
96
- if (attributes.lenght > 0) {
96
+ if (Object.entries(attributes).length > 0) {
97
97
  baseActions.push({
98
98
  type: 'modify',
99
99
  path: `${filePath}/content-types/{{ singularName }}/schema.json`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/generators",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Interactive API generator.",
5
5
  "keywords": [
6
6
  "strapi",
@@ -30,7 +30,7 @@
30
30
  "main": "lib/index.js",
31
31
  "dependencies": {
32
32
  "@sindresorhus/slugify": "1.1.0",
33
- "@strapi/utils": "4.0.0",
33
+ "@strapi/utils": "4.0.1",
34
34
  "chalk": "4.1.2",
35
35
  "fs-extra": "10.0.0",
36
36
  "node-plop": "0.26.3",
@@ -41,5 +41,5 @@
41
41
  "node": ">=12.x.x <=16.x.x",
42
42
  "npm": ">=6.0.0"
43
43
  },
44
- "gitHead": "b181702f0202b2c6d645d42b195a831f25cd0b03"
44
+ "gitHead": "e2cd01e8c6cbfeba15ad7787e38b6eebcbb92221"
45
45
  }