@strapi/generators 5.8.1 → 5.10.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.
package/dist/plopfile.mjs CHANGED
@@ -1,582 +1,655 @@
1
- import pluralize from "pluralize";
2
- import { join } from "path";
3
- import fs from "fs-extra";
4
- import tsUtils from "@strapi/typescript-utils";
5
- import slugify from "@sindresorhus/slugify";
6
- import { strings } from "@strapi/utils";
7
- const validateInput = (input) => {
8
- const regex = /^[A-Za-z-]+$/g;
9
- if (!input) {
10
- return "You must provide an input";
11
- }
12
- return regex.test(input) || "Please use only letters, '-' and no spaces";
13
- };
14
- const generateApi = (plop) => {
15
- plop.setGenerator("api", {
16
- description: "Generate a basic API",
17
- prompts: [
18
- {
19
- type: "input",
20
- name: "id",
21
- message: "API name",
22
- validate: (input) => validateInput(input)
23
- },
24
- {
25
- type: "confirm",
26
- name: "isPluginApi",
27
- message: "Is this API for a plugin?"
28
- },
29
- {
30
- when: (answers) => answers.isPluginApi,
31
- type: "list",
32
- name: "plugin",
33
- message: "Plugin name",
34
- async choices() {
35
- const pluginsPath = join(plop.getDestBasePath(), "plugins");
36
- const exists = await fs.pathExists(pluginsPath);
37
- if (!exists) {
38
- throw Error(`Couldn't find a "plugins" directory`);
39
- }
40
- const pluginsDir = await fs.readdir(pluginsPath, { withFileTypes: true });
41
- const pluginsDirContent = pluginsDir.filter((fd) => fd.isDirectory());
42
- if (pluginsDirContent.length === 0) {
43
- throw Error('The "plugins" directory is empty');
44
- }
45
- return pluginsDirContent;
1
+ import pluralize from 'pluralize';
2
+ import { join } from 'path';
3
+ import fs from 'fs-extra';
4
+ import tsUtils from '@strapi/typescript-utils';
5
+ import slugify from '@sindresorhus/slugify';
6
+ import { strings } from '@strapi/utils';
7
+
8
+ var validateInput = ((input)=>{
9
+ const regex = /^[A-Za-z-]+$/g;
10
+ if (!input) {
11
+ return 'You must provide an input';
12
+ }
13
+ return regex.test(input) || "Please use only letters, '-' and no spaces";
14
+ });
15
+
16
+ var generateApi = ((plop)=>{
17
+ // API generator
18
+ plop.setGenerator('api', {
19
+ description: 'Generate a basic API',
20
+ prompts: [
21
+ {
22
+ type: 'input',
23
+ name: 'id',
24
+ message: 'API name',
25
+ validate: (input)=>validateInput(input)
26
+ },
27
+ {
28
+ type: 'confirm',
29
+ name: 'isPluginApi',
30
+ message: 'Is this API for a plugin?'
31
+ },
32
+ {
33
+ when: (answers)=>answers.isPluginApi,
34
+ type: 'list',
35
+ name: 'plugin',
36
+ message: 'Plugin name',
37
+ async choices () {
38
+ const pluginsPath = join(plop.getDestBasePath(), 'plugins');
39
+ const exists = await fs.pathExists(pluginsPath);
40
+ if (!exists) {
41
+ throw Error('Couldn\'t find a "plugins" directory');
42
+ }
43
+ const pluginsDir = await fs.readdir(pluginsPath, {
44
+ withFileTypes: true
45
+ });
46
+ const pluginsDirContent = pluginsDir.filter((fd)=>fd.isDirectory());
47
+ if (pluginsDirContent.length === 0) {
48
+ throw Error('The "plugins" directory is empty');
49
+ }
50
+ return pluginsDirContent;
51
+ }
52
+ }
53
+ ],
54
+ actions (answers) {
55
+ if (!answers) {
56
+ return [];
57
+ }
58
+ const filePath = answers.isPluginApi && answers.plugin ? 'plugins/{{ plugin }}/server' : 'api/{{ id }}';
59
+ const currentDir = process.cwd();
60
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
61
+ const baseActions = [
62
+ {
63
+ type: 'add',
64
+ path: `${filePath}/controllers/{{ id }}.${language}`,
65
+ templateFile: `templates/${language}/controller.${language}.hbs`
66
+ },
67
+ {
68
+ type: 'add',
69
+ path: `${filePath}/services/{{ id }}.${language}`,
70
+ templateFile: `templates/${language}/service.${language}.hbs`
71
+ }
72
+ ];
73
+ if (answers.isPluginApi) {
74
+ return baseActions;
75
+ }
76
+ return [
77
+ {
78
+ type: 'add',
79
+ path: `${filePath}/routes/{{ id }}.${language}`,
80
+ templateFile: `templates/${language}/single-route.${language}.hbs`
81
+ },
82
+ ...baseActions
83
+ ];
46
84
  }
47
- }
48
- ],
49
- actions(answers) {
50
- if (!answers) {
51
- return [];
52
- }
53
- const filePath = answers.isPluginApi && answers.plugin ? "plugins/{{ plugin }}/server" : "api/{{ id }}";
54
- const currentDir = process.cwd();
55
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? "ts" : "js";
56
- const baseActions = [
85
+ });
86
+ });
87
+
88
+ var getDestinationPrompts = ((action, basePath, { rootFolder = false } = {})=>{
89
+ return [
57
90
  {
58
- type: "add",
59
- path: `${filePath}/controllers/{{ id }}.${language}`,
60
- templateFile: `templates/${language}/controller.${language}.hbs`
91
+ type: 'list',
92
+ name: 'destination',
93
+ message: `Where do you want to add this ${action}?`,
94
+ choices: [
95
+ ...rootFolder ? [
96
+ {
97
+ name: `Add ${action} to root of project`,
98
+ value: 'root'
99
+ }
100
+ ] : [
101
+ {
102
+ name: `Add ${action} to new API`,
103
+ value: 'new'
104
+ }
105
+ ],
106
+ {
107
+ name: `Add ${action} to an existing API`,
108
+ value: 'api'
109
+ },
110
+ {
111
+ name: `Add ${action} to an existing plugin`,
112
+ value: 'plugin'
113
+ }
114
+ ]
61
115
  },
62
116
  {
63
- type: "add",
64
- path: `${filePath}/services/{{ id }}.${language}`,
65
- templateFile: `templates/${language}/service.${language}.hbs`
66
- }
67
- ];
68
- if (answers.isPluginApi) {
69
- return baseActions;
70
- }
71
- return [
72
- {
73
- type: "add",
74
- path: `${filePath}/routes/{{ id }}.${language}`,
75
- templateFile: `templates/${language}/single-route.${language}.hbs`
117
+ when: (answers)=>answers.destination === 'api',
118
+ type: 'list',
119
+ message: 'Which API is this for?',
120
+ name: 'api',
121
+ async choices () {
122
+ const apiPath = join(basePath, 'api');
123
+ const exists = await fs.pathExists(apiPath);
124
+ if (!exists) {
125
+ throw Error('Couldn\'t find an "api" directory');
126
+ }
127
+ const apiDir = await fs.readdir(apiPath, {
128
+ withFileTypes: true
129
+ });
130
+ const apiDirContent = apiDir.filter((fd)=>fd.isDirectory());
131
+ if (apiDirContent.length === 0) {
132
+ throw Error('The "api" directory is empty');
133
+ }
134
+ return apiDirContent;
135
+ }
76
136
  },
77
- ...baseActions
78
- ];
137
+ {
138
+ when: (answers)=>answers.destination === 'plugin',
139
+ type: 'list',
140
+ message: 'Which plugin is this for?',
141
+ name: 'plugin',
142
+ async choices () {
143
+ const pluginsPath = join(basePath, 'plugins');
144
+ const exists = await fs.pathExists(pluginsPath);
145
+ if (!exists) {
146
+ throw Error('Couldn\'t find a "plugins" directory');
147
+ }
148
+ const pluginsDir = await fs.readdir(pluginsPath);
149
+ const pluginsDirContent = pluginsDir.filter((api)=>fs.lstatSync(join(pluginsPath, api)).isDirectory());
150
+ if (pluginsDirContent.length === 0) {
151
+ throw Error('The "plugins" directory is empty');
152
+ }
153
+ return pluginsDirContent;
154
+ }
155
+ }
156
+ ];
157
+ });
158
+
159
+ var getFilePath = ((destination)=>{
160
+ if (destination === 'api') {
161
+ return `api/{{ api }}`;
79
162
  }
80
- });
81
- };
82
- const getDestinationPrompts = (action, basePath, { rootFolder = false } = {}) => {
83
- return [
84
- {
85
- type: "list",
86
- name: "destination",
87
- message: `Where do you want to add this ${action}?`,
88
- choices: [
89
- ...rootFolder ? [
90
- {
91
- name: `Add ${action} to root of project`,
92
- value: "root"
93
- }
94
- ] : [
95
- {
96
- name: `Add ${action} to new API`,
97
- value: "new"
98
- }
163
+ if (destination === 'plugin') {
164
+ return `plugins/{{ plugin }}/server`;
165
+ }
166
+ if (destination === 'root') {
167
+ return './';
168
+ }
169
+ return `api/{{ id }}`;
170
+ });
171
+
172
+ var generateController = ((plop)=>{
173
+ // Controller generator
174
+ plop.setGenerator('controller', {
175
+ description: 'Generate a controller for an API',
176
+ prompts: [
177
+ {
178
+ type: 'input',
179
+ name: 'id',
180
+ message: 'Controller name',
181
+ validate: (input)=>validateInput(input)
182
+ },
183
+ ...getDestinationPrompts('controller', plop.getDestBasePath())
99
184
  ],
100
- { name: `Add ${action} to an existing API`, value: "api" },
101
- { name: `Add ${action} to an existing plugin`, value: "plugin" }
102
- ]
185
+ actions (answers) {
186
+ if (!answers) {
187
+ return [];
188
+ }
189
+ const filePath = getFilePath(answers.destination);
190
+ const currentDir = process.cwd();
191
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
192
+ return [
193
+ {
194
+ type: 'add',
195
+ path: `${filePath}/controllers/{{ id }}.${language}`,
196
+ templateFile: `templates/${language}/controller.${language}.hbs`
197
+ }
198
+ ];
199
+ }
200
+ });
201
+ });
202
+
203
+ const questions$2 = [
204
+ {
205
+ type: 'input',
206
+ name: 'displayName',
207
+ message: 'Content type display name',
208
+ validate: (input)=>!!input
103
209
  },
104
210
  {
105
- when: (answers) => answers.destination === "api",
106
- type: "list",
107
- message: "Which API is this for?",
108
- name: "api",
109
- async choices() {
110
- const apiPath = join(basePath, "api");
111
- const exists = await fs.pathExists(apiPath);
112
- if (!exists) {
113
- throw Error(`Couldn't find an "api" directory`);
114
- }
115
- const apiDir = await fs.readdir(apiPath, { withFileTypes: true });
116
- const apiDirContent = apiDir.filter((fd) => fd.isDirectory());
117
- if (apiDirContent.length === 0) {
118
- throw Error('The "api" directory is empty');
211
+ type: 'input',
212
+ name: 'singularName',
213
+ message: 'Content type singular name',
214
+ default: (answers)=>slugify(answers.displayName),
215
+ validate (input) {
216
+ if (!strings.isKebabCase(input)) {
217
+ return 'Value must be in kebab-case';
218
+ }
219
+ return true;
119
220
  }
120
- return apiDirContent;
121
- }
122
221
  },
123
222
  {
124
- when: (answers) => answers.destination === "plugin",
125
- type: "list",
126
- message: "Which plugin is this for?",
127
- name: "plugin",
128
- async choices() {
129
- const pluginsPath = join(basePath, "plugins");
130
- const exists = await fs.pathExists(pluginsPath);
131
- if (!exists) {
132
- throw Error(`Couldn't find a "plugins" directory`);
133
- }
134
- const pluginsDir = await fs.readdir(pluginsPath);
135
- const pluginsDirContent = pluginsDir.filter(
136
- (api) => fs.lstatSync(join(pluginsPath, api)).isDirectory()
137
- );
138
- if (pluginsDirContent.length === 0) {
139
- throw Error('The "plugins" directory is empty');
140
- }
141
- return pluginsDirContent;
142
- }
143
- }
144
- ];
145
- };
146
- const getFilePath = (destination) => {
147
- if (destination === "api") {
148
- return `api/{{ api }}`;
149
- }
150
- if (destination === "plugin") {
151
- return `plugins/{{ plugin }}/server`;
152
- }
153
- if (destination === "root") {
154
- return "./";
155
- }
156
- return `api/{{ id }}`;
157
- };
158
- const generateController = (plop) => {
159
- plop.setGenerator("controller", {
160
- description: "Generate a controller for an API",
161
- prompts: [
162
- {
163
- type: "input",
164
- name: "id",
165
- message: "Controller name",
166
- validate: (input) => validateInput(input)
167
- },
168
- ...getDestinationPrompts("controller", plop.getDestBasePath())
169
- ],
170
- actions(answers) {
171
- if (!answers) {
172
- return [];
173
- }
174
- const filePath = getFilePath(answers.destination);
175
- const currentDir = process.cwd();
176
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? "ts" : "js";
177
- return [
178
- {
179
- type: "add",
180
- path: `${filePath}/controllers/{{ id }}.${language}`,
181
- templateFile: `templates/${language}/controller.${language}.hbs`
223
+ type: 'input',
224
+ name: 'pluralName',
225
+ message: 'Content type plural name',
226
+ default: (answers)=>pluralize(answers.singularName),
227
+ validate (input, answers) {
228
+ if (answers.singularName === input) {
229
+ return 'Singular and plural names cannot be the same';
230
+ }
231
+ if (!strings.isKebabCase(input)) {
232
+ return 'Value must be in kebab-case';
233
+ }
234
+ return true;
182
235
  }
183
- ];
184
236
  }
185
- });
186
- };
187
- const questions$2 = [
188
- {
189
- type: "input",
190
- name: "displayName",
191
- message: "Content type display name",
192
- validate: (input) => !!input
193
- },
194
- {
195
- type: "input",
196
- name: "singularName",
197
- message: "Content type singular name",
198
- default: (answers) => slugify(answers.displayName),
199
- validate(input) {
200
- if (!strings.isKebabCase(input)) {
201
- return "Value must be in kebab-case";
202
- }
203
- return true;
204
- }
205
- },
206
- {
207
- type: "input",
208
- name: "pluralName",
209
- message: "Content type plural name",
210
- default: (answers) => pluralize(answers.singularName),
211
- validate(input, answers) {
212
- if (answers.singularName === input) {
213
- return "Singular and plural names cannot be the same";
214
- }
215
- if (!strings.isKebabCase(input)) {
216
- return "Value must be in kebab-case";
217
- }
218
- return true;
219
- }
220
- }
221
237
  ];
238
+
222
239
  const questions$1 = [
223
- {
224
- type: "list",
225
- name: "kind",
226
- message: "Please choose the model type",
227
- default: "collectionType",
228
- choices: [
229
- { name: "Collection Type", value: "collectionType" },
230
- { name: "Single Type", value: "singleType" }
231
- ],
232
- validate: (input) => validateInput(input)
233
- }
240
+ {
241
+ type: 'list',
242
+ name: 'kind',
243
+ message: 'Please choose the model type',
244
+ default: 'collectionType',
245
+ choices: [
246
+ {
247
+ name: 'Collection Type',
248
+ value: 'collectionType'
249
+ },
250
+ {
251
+ name: 'Single Type',
252
+ value: 'singleType'
253
+ }
254
+ ],
255
+ validate: (input)=>validateInput(input)
256
+ }
234
257
  ];
235
- const validateAttributeInput = (input) => {
236
- const regex = /^[A-Za-z-|_]+$/g;
237
- if (!input) {
238
- return "You must provide an input";
239
- }
240
- return regex.test(input) || "Please use only letters, '-', '_', and no spaces";
241
- };
258
+
259
+ var validateAttributeInput = ((input)=>{
260
+ const regex = /^[A-Za-z-|_]+$/g;
261
+ if (!input) {
262
+ return 'You must provide an input';
263
+ }
264
+ return regex.test(input) || "Please use only letters, '-', '_', and no spaces";
265
+ });
266
+
242
267
  const DEFAULT_TYPES = [
243
- // advanced types
244
- "media",
245
- // scalar types
246
- "string",
247
- "text",
248
- "richtext",
249
- "json",
250
- "enumeration",
251
- "password",
252
- "email",
253
- "integer",
254
- "biginteger",
255
- "float",
256
- "decimal",
257
- "date",
258
- "time",
259
- "datetime",
260
- "timestamp",
261
- "boolean"
268
+ // advanced types
269
+ 'media',
270
+ // scalar types
271
+ 'string',
272
+ 'text',
273
+ 'richtext',
274
+ 'json',
275
+ 'enumeration',
276
+ 'password',
277
+ 'email',
278
+ 'integer',
279
+ 'biginteger',
280
+ 'float',
281
+ 'decimal',
282
+ 'date',
283
+ 'time',
284
+ 'datetime',
285
+ 'timestamp',
286
+ 'boolean'
262
287
  ];
263
- const getAttributesPrompts = async (inquirer) => {
264
- const { addAttributes } = await inquirer.prompt([
265
- {
266
- type: "confirm",
267
- name: "addAttributes",
268
- message: "Do you want to add attributes?"
269
- }
270
- ]);
271
- const attributes = [];
272
- const createNewAttributes = async (inquirer2) => {
273
- const answers = await inquirer2.prompt([
274
- {
275
- type: "input",
276
- name: "attributeName",
277
- message: "Name of attribute",
278
- validate: (input) => validateAttributeInput(input)
279
- },
280
- {
281
- type: "list",
282
- name: "attributeType",
283
- message: "What type of attribute",
284
- pageSize: DEFAULT_TYPES.length,
285
- choices: DEFAULT_TYPES.map((type) => {
286
- return { name: type, value: type };
287
- })
288
- },
289
- {
290
- when: (answers2) => answers2.attributeType === "enumeration",
291
- type: "input",
292
- name: "enum",
293
- message: "Add values separated by a comma"
294
- },
295
- {
296
- when: (answers2) => answers2.attributeType === "media",
297
- type: "list",
298
- name: "multiple",
299
- message: "Choose media type",
300
- choices: [
301
- { name: "Multiple", value: true },
302
- { name: "Single", value: false }
303
- ]
304
- },
305
- {
306
- type: "confirm",
307
- name: "addAttributes",
308
- message: "Do you want to add another attribute?"
309
- }
288
+ const getAttributesPrompts = async (inquirer)=>{
289
+ const { addAttributes } = await inquirer.prompt([
290
+ {
291
+ type: 'confirm',
292
+ name: 'addAttributes',
293
+ message: 'Do you want to add attributes?'
294
+ }
310
295
  ]);
311
- attributes.push(answers);
312
- if (!answers.addAttributes) {
313
- return;
296
+ const attributes = [];
297
+ /**
298
+ * @param {import('inquirer').Inquirer} inquirer
299
+ * @returns {Promise<void>}
300
+ */ const createNewAttributes = async (inquirer)=>{
301
+ const answers = await inquirer.prompt([
302
+ {
303
+ type: 'input',
304
+ name: 'attributeName',
305
+ message: 'Name of attribute',
306
+ validate: (input)=>validateAttributeInput(input)
307
+ },
308
+ {
309
+ type: 'list',
310
+ name: 'attributeType',
311
+ message: 'What type of attribute',
312
+ pageSize: DEFAULT_TYPES.length,
313
+ choices: DEFAULT_TYPES.map((type)=>{
314
+ return {
315
+ name: type,
316
+ value: type
317
+ };
318
+ })
319
+ },
320
+ {
321
+ when: (answers)=>answers.attributeType === 'enumeration',
322
+ type: 'input',
323
+ name: 'enum',
324
+ message: 'Add values separated by a comma'
325
+ },
326
+ {
327
+ when: (answers)=>answers.attributeType === 'media',
328
+ type: 'list',
329
+ name: 'multiple',
330
+ message: 'Choose media type',
331
+ choices: [
332
+ {
333
+ name: 'Multiple',
334
+ value: true
335
+ },
336
+ {
337
+ name: 'Single',
338
+ value: false
339
+ }
340
+ ]
341
+ },
342
+ {
343
+ type: 'confirm',
344
+ name: 'addAttributes',
345
+ message: 'Do you want to add another attribute?'
346
+ }
347
+ ]);
348
+ attributes.push(answers);
349
+ if (!answers.addAttributes) {
350
+ return;
351
+ }
352
+ await createNewAttributes(inquirer);
353
+ };
354
+ if (addAttributes) {
355
+ await createNewAttributes(inquirer);
356
+ } else {
357
+ console.warn(`You won't be able to manage entries from the admin, you can still add attributes later from the content type builder.`);
314
358
  }
315
- await createNewAttributes(inquirer2);
316
- };
317
- if (addAttributes) {
318
- await createNewAttributes(inquirer);
319
- } else {
320
- console.warn(
321
- `You won't be able to manage entries from the admin, you can still add attributes later from the content type builder.`
322
- );
323
- }
324
- return attributes;
359
+ return attributes;
325
360
  };
361
+
326
362
  const questions = [
327
- {
328
- type: "confirm",
329
- name: "bootstrapApi",
330
- default: true,
331
- message: "Bootstrap API related files?"
332
- }
363
+ {
364
+ type: 'confirm',
365
+ name: 'bootstrapApi',
366
+ default: true,
367
+ message: 'Bootstrap API related files?'
368
+ }
333
369
  ];
334
- const generateContentType = (plop) => {
335
- plop.setGenerator("content-type", {
336
- description: "Generate a content type for an API",
337
- async prompts(inquirer) {
338
- const config = await inquirer.prompt([...questions$2, ...questions$1]);
339
- const attributes = await getAttributesPrompts(inquirer);
340
- const api = await inquirer.prompt([
341
- ...getDestinationPrompts("model", plop.getDestBasePath()),
342
- {
343
- when: (answers) => answers.destination === "new",
344
- type: "input",
345
- name: "id",
346
- default: config.singularName,
347
- message: "Name of the new API?",
348
- async validate(input) {
349
- if (!strings.isKebabCase(input)) {
350
- return "Value must be in kebab-case";
370
+
371
+ var generateContentType = ((plop)=>{
372
+ // Model generator
373
+ plop.setGenerator('content-type', {
374
+ description: 'Generate a content type for an API',
375
+ async prompts (inquirer) {
376
+ const config = await inquirer.prompt([
377
+ ...questions$2,
378
+ ...questions$1
379
+ ]);
380
+ // @ts-expect-error issue with deprecated inquirer.prompts attribute to fix with ugprade to inquirer
381
+ const attributes = await getAttributesPrompts(inquirer);
382
+ const api = await inquirer.prompt([
383
+ ...getDestinationPrompts('model', plop.getDestBasePath()),
384
+ {
385
+ when: (answers)=>answers.destination === 'new',
386
+ type: 'input',
387
+ name: 'id',
388
+ default: config.singularName,
389
+ message: 'Name of the new API?',
390
+ async validate (input) {
391
+ if (!strings.isKebabCase(input)) {
392
+ return 'Value must be in kebab-case';
393
+ }
394
+ const apiPath = join(plop.getDestBasePath(), 'api');
395
+ const exists = await fs.pathExists(apiPath);
396
+ if (!exists) {
397
+ return true;
398
+ }
399
+ const apiDir = await fs.readdir(apiPath, {
400
+ withFileTypes: true
401
+ });
402
+ const apiDirContent = apiDir.filter((fd)=>fd.isDirectory());
403
+ if (apiDirContent.findIndex((dir)=>dir.name === input) !== -1) {
404
+ throw new Error('This name is already taken.');
405
+ }
406
+ return true;
407
+ }
408
+ },
409
+ ...questions
410
+ ]);
411
+ return {
412
+ ...config,
413
+ ...api,
414
+ attributes
415
+ };
416
+ },
417
+ actions (answers) {
418
+ if (!answers) {
419
+ return [];
351
420
  }
352
- const apiPath = join(plop.getDestBasePath(), "api");
353
- const exists = await fs.pathExists(apiPath);
354
- if (!exists) {
355
- return true;
421
+ const attributes = answers.attributes.reduce((object, answer)=>{
422
+ const val = {
423
+ type: answer.attributeType
424
+ };
425
+ if (answer.attributeType === 'enumeration') {
426
+ val.enum = answer.enum.split(',').map((item)=>item.trim());
427
+ }
428
+ if (answer.attributeType === 'media') {
429
+ val.allowedTypes = [
430
+ 'images',
431
+ 'files',
432
+ 'videos',
433
+ 'audios'
434
+ ];
435
+ val.multiple = answer.multiple;
436
+ }
437
+ return Object.assign(object, {
438
+ [answer.attributeName]: val
439
+ }, {});
440
+ }, {});
441
+ const filePath = getFilePath(answers.destination);
442
+ const currentDir = process.cwd();
443
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
444
+ const baseActions = [
445
+ {
446
+ type: 'add',
447
+ path: `${filePath}/content-types/{{ singularName }}/schema.json`,
448
+ templateFile: `templates/${language}/content-type.schema.json.hbs`,
449
+ data: {
450
+ collectionName: slugify(answers.pluralName, {
451
+ separator: '_'
452
+ })
453
+ }
454
+ }
455
+ ];
456
+ if (Object.entries(attributes).length > 0) {
457
+ baseActions.push({
458
+ type: 'modify',
459
+ path: `${filePath}/content-types/{{ singularName }}/schema.json`,
460
+ transform (template) {
461
+ const parsedTemplate = JSON.parse(template);
462
+ parsedTemplate.attributes = attributes;
463
+ return JSON.stringify(parsedTemplate, null, 2);
464
+ }
465
+ });
356
466
  }
357
- const apiDir = await fs.readdir(apiPath, { withFileTypes: true });
358
- const apiDirContent = apiDir.filter((fd) => fd.isDirectory());
359
- if (apiDirContent.findIndex((dir) => dir.name === input) !== -1) {
360
- throw new Error("This name is already taken.");
467
+ if (answers.bootstrapApi) {
468
+ const { singularName } = answers;
469
+ let uid;
470
+ if (answers.destination === 'new') {
471
+ uid = `api::${answers.id}.${singularName}`;
472
+ } else if (answers.api) {
473
+ uid = `api::${answers.api}.${singularName}`;
474
+ } else if (answers.plugin) {
475
+ uid = `plugin::${answers.plugin}.${singularName}`;
476
+ }
477
+ baseActions.push({
478
+ type: 'add',
479
+ path: `${filePath}/controllers/{{ singularName }}.${language}`,
480
+ templateFile: `templates/${language}/core-controller.${language}.hbs`,
481
+ data: {
482
+ uid
483
+ }
484
+ }, {
485
+ type: 'add',
486
+ path: `${filePath}/services/{{ singularName }}.${language}`,
487
+ templateFile: `templates/${language}/core-service.${language}.hbs`,
488
+ data: {
489
+ uid
490
+ }
491
+ }, {
492
+ type: 'add',
493
+ path: `${filePath}/routes/{{ singularName }}.${language}`,
494
+ templateFile: `templates/${language}/core-router.${language}.hbs`,
495
+ data: {
496
+ uid
497
+ }
498
+ });
361
499
  }
362
- return true;
363
- }
364
- },
365
- ...questions
366
- ]);
367
- return {
368
- ...config,
369
- ...api,
370
- attributes
371
- };
372
- },
373
- actions(answers) {
374
- if (!answers) {
375
- return [];
376
- }
377
- const attributes = answers.attributes.reduce((object, answer) => {
378
- const val = { type: answer.attributeType };
379
- if (answer.attributeType === "enumeration") {
380
- val.enum = answer.enum.split(",").map((item) => item.trim());
381
- }
382
- if (answer.attributeType === "media") {
383
- val.allowedTypes = ["images", "files", "videos", "audios"];
384
- val.multiple = answer.multiple;
500
+ return baseActions;
385
501
  }
386
- return Object.assign(object, { [answer.attributeName]: val }, {});
387
- }, {});
388
- const filePath = getFilePath(answers.destination);
389
- const currentDir = process.cwd();
390
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? "ts" : "js";
391
- const baseActions = [
392
- {
393
- type: "add",
394
- path: `${filePath}/content-types/{{ singularName }}/schema.json`,
395
- templateFile: `templates/${language}/content-type.schema.json.hbs`,
396
- data: {
397
- collectionName: slugify(answers.pluralName, { separator: "_" })
398
- }
399
- }
400
- ];
401
- if (Object.entries(attributes).length > 0) {
402
- baseActions.push({
403
- type: "modify",
404
- path: `${filePath}/content-types/{{ singularName }}/schema.json`,
405
- transform(template) {
406
- const parsedTemplate = JSON.parse(template);
407
- parsedTemplate.attributes = attributes;
408
- return JSON.stringify(parsedTemplate, null, 2);
409
- }
410
- });
411
- }
412
- if (answers.bootstrapApi) {
413
- const { singularName } = answers;
414
- let uid;
415
- if (answers.destination === "new") {
416
- uid = `api::${answers.id}.${singularName}`;
417
- } else if (answers.api) {
418
- uid = `api::${answers.api}.${singularName}`;
419
- } else if (answers.plugin) {
420
- uid = `plugin::${answers.plugin}.${singularName}`;
421
- }
422
- baseActions.push(
423
- {
424
- type: "add",
425
- path: `${filePath}/controllers/{{ singularName }}.${language}`,
426
- templateFile: `templates/${language}/core-controller.${language}.hbs`,
427
- data: { uid }
428
- },
429
- {
430
- type: "add",
431
- path: `${filePath}/services/{{ singularName }}.${language}`,
432
- templateFile: `templates/${language}/core-service.${language}.hbs`,
433
- data: { uid }
434
- },
435
- {
436
- type: "add",
437
- path: `${filePath}/routes/{{ singularName }}.${language}`,
438
- templateFile: `templates/${language}/core-router.${language}.hbs`,
439
- data: { uid }
440
- }
441
- );
442
- }
443
- return baseActions;
444
- }
445
- });
446
- };
447
- const generatePolicy = (plop) => {
448
- plop.setGenerator("policy", {
449
- description: "Generate a policy for an API",
450
- prompts: [
451
- {
452
- type: "input",
453
- name: "id",
454
- message: "Policy name",
455
- validate: (input) => validateInput(input)
456
- },
457
- ...getDestinationPrompts("policy", plop.getDestBasePath(), { rootFolder: true })
458
- ],
459
- actions(answers) {
460
- if (!answers) {
461
- return [];
462
- }
463
- const currentDir = process.cwd();
464
- const filePath = getFilePath(answers.destination);
465
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? "ts" : "js";
466
- return [
467
- {
468
- type: "add",
469
- path: `${filePath}/policies/{{ id }}.${language}`,
470
- templateFile: `templates/${language}/policy.${language}.hbs`
502
+ });
503
+ });
504
+
505
+ var generatePolicy = ((plop)=>{
506
+ // Policy generator
507
+ plop.setGenerator('policy', {
508
+ description: 'Generate a policy for an API',
509
+ prompts: [
510
+ {
511
+ type: 'input',
512
+ name: 'id',
513
+ message: 'Policy name',
514
+ validate: (input)=>validateInput(input)
515
+ },
516
+ ...getDestinationPrompts('policy', plop.getDestBasePath(), {
517
+ rootFolder: true
518
+ })
519
+ ],
520
+ actions (answers) {
521
+ if (!answers) {
522
+ return [];
523
+ }
524
+ const currentDir = process.cwd();
525
+ const filePath = getFilePath(answers.destination);
526
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
527
+ return [
528
+ {
529
+ type: 'add',
530
+ path: `${filePath}/policies/{{ id }}.${language}`,
531
+ templateFile: `templates/${language}/policy.${language}.hbs`
532
+ }
533
+ ];
471
534
  }
472
- ];
473
- }
474
- });
475
- };
476
- const generateMiddleware = (plop) => {
477
- plop.setGenerator("middleware", {
478
- description: "Generate a middleware for an API",
479
- prompts: [
480
- {
481
- type: "input",
482
- name: "name",
483
- message: "Middleware name",
484
- validate: (input) => validateInput(input)
485
- },
486
- ...getDestinationPrompts("middleware", plop.getDestBasePath(), { rootFolder: true })
487
- ],
488
- actions(answers) {
489
- if (!answers) {
490
- return [];
491
- }
492
- const filePath = getFilePath(answers.destination);
493
- const currentDir = process.cwd();
494
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? "ts" : "js";
495
- return [
496
- {
497
- type: "add",
498
- path: `${filePath}/middlewares/{{ name }}.${language}`,
499
- templateFile: `templates/${language}/middleware.${language}.hbs`
535
+ });
536
+ });
537
+
538
+ var generateMiddleware = ((plop)=>{
539
+ // middleware generator
540
+ plop.setGenerator('middleware', {
541
+ description: 'Generate a middleware for an API',
542
+ prompts: [
543
+ {
544
+ type: 'input',
545
+ name: 'name',
546
+ message: 'Middleware name',
547
+ validate: (input)=>validateInput(input)
548
+ },
549
+ ...getDestinationPrompts('middleware', plop.getDestBasePath(), {
550
+ rootFolder: true
551
+ })
552
+ ],
553
+ actions (answers) {
554
+ if (!answers) {
555
+ return [];
556
+ }
557
+ const filePath = getFilePath(answers.destination);
558
+ const currentDir = process.cwd();
559
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
560
+ return [
561
+ {
562
+ type: 'add',
563
+ path: `${filePath}/middlewares/{{ name }}.${language}`,
564
+ templateFile: `templates/${language}/middleware.${language}.hbs`
565
+ }
566
+ ];
500
567
  }
501
- ];
568
+ });
569
+ });
570
+
571
+ var validateFileNameInput = ((input)=>{
572
+ const regex = /^[A-Za-z-_0-9]+$/g;
573
+ if (!input) {
574
+ return 'You must provide an input';
502
575
  }
503
- });
504
- };
505
- const validateFileNameInput = (input) => {
506
- const regex = /^[A-Za-z-_0-9]+$/g;
507
- if (!input) {
508
- return "You must provide an input";
509
- }
510
- return regex.test(input) || "Please use only letters and number, '-' or '_' and no spaces";
511
- };
512
- const getFormattedDate = (date = /* @__PURE__ */ new Date()) => {
513
- return new Date(date.getTime() - date.getTimezoneOffset() * 6e4).toJSON().replace(/[-:]/g, ".").replace(/\....Z/, "");
514
- };
515
- const generateMigration = (plop) => {
516
- plop.setGenerator("migration", {
517
- description: "Generate a migration",
518
- prompts: [
519
- {
520
- type: "input",
521
- name: "name",
522
- message: "Migration name",
523
- validate: (input) => validateFileNameInput(input)
524
- }
525
- ],
526
- actions() {
527
- const currentDir = process.cwd();
528
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? "ts" : "js";
529
- const timestamp = getFormattedDate();
530
- return [
531
- {
532
- type: "add",
533
- path: `${currentDir}/database/migrations/${timestamp}.{{ name }}.${language}`,
534
- templateFile: `templates/${language}/migration.${language}.hbs`
576
+ return regex.test(input) || "Please use only letters and number, '-' or '_' and no spaces";
577
+ });
578
+
579
+ var getFormattedDate = ((date = new Date())=>{
580
+ return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toJSON().replace(/[-:]/g, '.').replace(/\....Z/, '');
581
+ });
582
+
583
+ var generateMigration = ((plop)=>{
584
+ // Migration generator
585
+ plop.setGenerator('migration', {
586
+ description: 'Generate a migration',
587
+ prompts: [
588
+ {
589
+ type: 'input',
590
+ name: 'name',
591
+ message: 'Migration name',
592
+ validate: (input)=>validateFileNameInput(input)
593
+ }
594
+ ],
595
+ actions () {
596
+ const currentDir = process.cwd();
597
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
598
+ const timestamp = getFormattedDate();
599
+ return [
600
+ {
601
+ type: 'add',
602
+ path: `${currentDir}/database/migrations/${timestamp}.{{ name }}.${language}`,
603
+ templateFile: `templates/${language}/migration.${language}.hbs`
604
+ }
605
+ ];
535
606
  }
536
- ];
537
- }
538
- });
539
- };
540
- const generateService = (plop) => {
541
- plop.setGenerator("service", {
542
- description: "Generate a service for an API",
543
- prompts: [
544
- {
545
- type: "input",
546
- name: "id",
547
- message: "Service name"
548
- },
549
- ...getDestinationPrompts("service", plop.getDestBasePath())
550
- ],
551
- actions(answers) {
552
- if (!answers) {
553
- return [];
554
- }
555
- const filePath = getFilePath(answers?.destination);
556
- const currentDir = process.cwd();
557
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? "ts" : "js";
558
- return [
559
- {
560
- type: "add",
561
- path: `${filePath}/services/{{ id }}.${language}`,
562
- templateFile: `templates/${language}/service.${language}.hbs`
607
+ });
608
+ });
609
+
610
+ var generateService = ((plop)=>{
611
+ // Service generator
612
+ plop.setGenerator('service', {
613
+ description: 'Generate a service for an API',
614
+ prompts: [
615
+ {
616
+ type: 'input',
617
+ name: 'id',
618
+ message: 'Service name'
619
+ },
620
+ ...getDestinationPrompts('service', plop.getDestBasePath())
621
+ ],
622
+ actions (answers) {
623
+ if (!answers) {
624
+ return [];
625
+ }
626
+ const filePath = getFilePath(answers?.destination);
627
+ const currentDir = process.cwd();
628
+ const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
629
+ return [
630
+ {
631
+ type: 'add',
632
+ path: `${filePath}/services/{{ id }}.${language}`,
633
+ templateFile: `templates/${language}/service.${language}.hbs`
634
+ }
635
+ ];
563
636
  }
564
- ];
565
- }
566
- });
567
- };
568
- const plopfile = (plop) => {
569
- plop.setWelcomeMessage("Strapi Generators");
570
- plop.setHelper("pluralize", (text) => pluralize(text));
571
- generateApi(plop);
572
- generateController(plop);
573
- generateContentType(plop);
574
- generatePolicy(plop);
575
- generateMiddleware(plop);
576
- generateMigration(plop);
577
- generateService(plop);
578
- };
579
- export {
580
- plopfile as default
581
- };
637
+ });
638
+ });
639
+
640
+ var plopfile = ((plop)=>{
641
+ // Plop config
642
+ plop.setWelcomeMessage('Strapi Generators');
643
+ plop.setHelper('pluralize', (text)=>pluralize(text));
644
+ // Generators
645
+ generateApi(plop);
646
+ generateController(plop);
647
+ generateContentType(plop);
648
+ generatePolicy(plop);
649
+ generateMiddleware(plop);
650
+ generateMigration(plop);
651
+ generateService(plop);
652
+ });
653
+
654
+ export { plopfile as default };
582
655
  //# sourceMappingURL=plopfile.mjs.map