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