@strapi/generators 0.0.0-next.e50ef5e2ea57ecf3da5bcf308508b51ee3c0deca → 0.0.0-next.e5b87a54008c9de2b3286a4774635dcf69895d9b

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 (77) hide show
  1. package/dist/index.d.ts +8 -4
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +73 -14
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +74 -15
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/plops/api.d.ts.map +1 -1
  8. package/dist/plops/api.js +79 -11
  9. package/dist/plops/api.js.map +1 -1
  10. package/dist/plops/api.mjs +79 -11
  11. package/dist/plops/api.mjs.map +1 -1
  12. package/dist/plops/content-type.d.ts.map +1 -1
  13. package/dist/plops/content-type.js +98 -3
  14. package/dist/plops/content-type.js.map +1 -1
  15. package/dist/plops/content-type.mjs +98 -3
  16. package/dist/plops/content-type.mjs.map +1 -1
  17. package/dist/plops/controller.d.ts.map +1 -1
  18. package/dist/plops/controller.js +35 -2
  19. package/dist/plops/controller.js.map +1 -1
  20. package/dist/plops/controller.mjs +35 -2
  21. package/dist/plops/controller.mjs.map +1 -1
  22. package/dist/plops/middleware.d.ts.map +1 -1
  23. package/dist/plops/middleware.js +35 -2
  24. package/dist/plops/middleware.js.map +1 -1
  25. package/dist/plops/middleware.mjs +35 -2
  26. package/dist/plops/middleware.mjs.map +1 -1
  27. package/dist/plops/migration.js.map +1 -1
  28. package/dist/plops/migration.mjs.map +1 -1
  29. package/dist/plops/policy.d.ts.map +1 -1
  30. package/dist/plops/policy.js +35 -2
  31. package/dist/plops/policy.js.map +1 -1
  32. package/dist/plops/policy.mjs +35 -2
  33. package/dist/plops/policy.mjs.map +1 -1
  34. package/dist/plops/prompts/bootstrap-api-prompts.d.ts +6 -2
  35. package/dist/plops/prompts/bootstrap-api-prompts.d.ts.map +1 -1
  36. package/dist/plops/prompts/bootstrap-api-prompts.js.map +1 -1
  37. package/dist/plops/prompts/bootstrap-api-prompts.mjs.map +1 -1
  38. package/dist/plops/prompts/ct-names-prompts.d.ts +18 -2
  39. package/dist/plops/prompts/ct-names-prompts.d.ts.map +1 -1
  40. package/dist/plops/prompts/ct-names-prompts.js.map +1 -1
  41. package/dist/plops/prompts/ct-names-prompts.mjs.map +1 -1
  42. package/dist/plops/prompts/get-attributes-prompts.d.ts +8 -2
  43. package/dist/plops/prompts/get-attributes-prompts.d.ts.map +1 -1
  44. package/dist/plops/prompts/get-attributes-prompts.js.map +1 -1
  45. package/dist/plops/prompts/get-attributes-prompts.mjs.map +1 -1
  46. package/dist/plops/prompts/get-destination-prompts.d.ts +24 -2
  47. package/dist/plops/prompts/get-destination-prompts.d.ts.map +1 -1
  48. package/dist/plops/prompts/get-destination-prompts.js.map +1 -1
  49. package/dist/plops/prompts/get-destination-prompts.mjs.map +1 -1
  50. package/dist/plops/prompts/kind-prompts.d.ts +11 -2
  51. package/dist/plops/prompts/kind-prompts.d.ts.map +1 -1
  52. package/dist/plops/prompts/kind-prompts.js.map +1 -1
  53. package/dist/plops/prompts/kind-prompts.mjs.map +1 -1
  54. package/dist/plops/service.d.ts.map +1 -1
  55. package/dist/plops/service.js +35 -2
  56. package/dist/plops/service.js.map +1 -1
  57. package/dist/plops/service.mjs +35 -2
  58. package/dist/plops/service.mjs.map +1 -1
  59. package/dist/plops/utils/extend-plugin-index-files.d.ts +8 -0
  60. package/dist/plops/utils/extend-plugin-index-files.d.ts.map +1 -0
  61. package/dist/plops/utils/extend-plugin-index-files.js +356 -0
  62. package/dist/plops/utils/extend-plugin-index-files.js.map +1 -0
  63. package/dist/plops/utils/extend-plugin-index-files.mjs +335 -0
  64. package/dist/plops/utils/extend-plugin-index-files.mjs.map +1 -0
  65. package/dist/plops/utils/get-file-path.d.ts +1 -1
  66. package/dist/plops/utils/get-file-path.js +2 -2
  67. package/dist/plops/utils/get-file-path.js.map +1 -1
  68. package/dist/plops/utils/get-file-path.mjs +2 -2
  69. package/dist/plops/utils/get-file-path.mjs.map +1 -1
  70. package/dist/templates/js/plugin/plugin.index.js.hbs +3 -0
  71. package/dist/templates/js/plugin/plugin.routes.index.js.hbs +12 -0
  72. package/dist/templates/js/plugin/plugin.routes.type.index.js.hbs +6 -0
  73. package/dist/templates/ts/core-controller.ts.hbs +1 -1
  74. package/dist/templates/ts/plugin/plugin.index.ts.hbs +1 -0
  75. package/dist/templates/ts/plugin/plugin.routes.index.ts.hbs +9 -0
  76. package/dist/templates/ts/plugin/plugin.routes.type.index.ts.hbs +4 -0
  77. package/package.json +11 -7
@@ -1 +1 @@
1
- {"version":3,"file":"api.mjs","sources":["../../src/plops/api.ts"],"sourcesContent":["import { join } from 'path';\nimport type { NodePlopAPI } from 'plop';\nimport fs from 'fs-extra';\nimport tsUtils from '@strapi/typescript-utils';\n\nimport validateInput from './utils/validate-input';\n\nexport default (plop: NodePlopAPI) => {\n // API generator\n plop.setGenerator('api', {\n description: 'Generate a basic API',\n prompts: [\n {\n type: 'input',\n name: 'id',\n message: 'API name',\n validate: (input) => validateInput(input),\n },\n {\n type: 'confirm',\n name: 'isPluginApi',\n message: 'Is this API for a plugin?',\n },\n {\n when: (answers) => answers.isPluginApi,\n type: 'list',\n name: 'plugin',\n message: 'Plugin name',\n async choices() {\n const pluginsPath = join(plop.getDestBasePath(), 'plugins');\n const exists = await fs.pathExists(pluginsPath);\n if (!exists) {\n throw Error('Couldn\\'t find a \"plugins\" directory');\n }\n\n const pluginsDir = await fs.readdir(pluginsPath, { withFileTypes: true });\n const pluginsDirContent = pluginsDir.filter((fd) => fd.isDirectory());\n\n if (pluginsDirContent.length === 0) {\n throw Error('The \"plugins\" directory is empty');\n }\n\n return pluginsDirContent;\n },\n },\n ],\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const filePath =\n answers.isPluginApi && answers.plugin ? 'plugins/{{ plugin }}/server' : 'api/{{ id }}';\n const currentDir = process.cwd();\n const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n const baseActions = [\n {\n type: 'add',\n path: `${filePath}/controllers/{{ id }}.${language}`,\n templateFile: `templates/${language}/controller.${language}.hbs`,\n },\n {\n type: 'add',\n path: `${filePath}/services/{{ id }}.${language}`,\n templateFile: `templates/${language}/service.${language}.hbs`,\n },\n ];\n\n if (answers.isPluginApi) {\n return baseActions;\n }\n\n return [\n {\n type: 'add',\n path: `${filePath}/routes/{{ id }}.${language}`,\n templateFile: `templates/${language}/single-route.${language}.hbs`,\n },\n ...baseActions,\n ];\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","type","name","message","validate","input","validateInput","when","answers","isPluginApi","choices","pluginsPath","join","getDestBasePath","exists","fs","pathExists","Error","pluginsDir","readdir","withFileTypes","pluginsDirContent","filter","fd","isDirectory","length","actions","filePath","plugin","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","baseActions","path","templateFile"],"mappings":";;;;;AAOA,kBAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,KAAO,EAAA;QACvBC,WAAa,EAAA,sBAAA;QACbC,OAAS,EAAA;AACP,YAAA;gBACEC,IAAM,EAAA,OAAA;gBACNC,IAAM,EAAA,IAAA;gBACNC,OAAS,EAAA,UAAA;gBACTC,QAAU,EAAA,CAACC,QAAUC,aAAcD,CAAAA,KAAAA;AACrC,aAAA;AACA,YAAA;gBACEJ,IAAM,EAAA,SAAA;gBACNC,IAAM,EAAA,aAAA;gBACNC,OAAS,EAAA;AACX,aAAA;AACA,YAAA;gBACEI,IAAM,EAAA,CAACC,OAAYA,GAAAA,OAAAA,CAAQC,WAAW;gBACtCR,IAAM,EAAA,MAAA;gBACNC,IAAM,EAAA,QAAA;gBACNC,OAAS,EAAA,aAAA;gBACT,MAAMO,OAAAA,CAAAA,GAAAA;AACJ,oBAAA,MAAMC,WAAcC,GAAAA,IAAAA,CAAKf,IAAKgB,CAAAA,eAAe,EAAI,EAAA,SAAA,CA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AS,mBAAmB,EAAEK,SAAS,CAAC;oBACjDK,YAAc,EAAA,CAAC,UAAU,EAAEL,QAAAA,CAAS,SAAS,EAAEA,QAAAA,CAAS,IAAI;AAC9D;AACD,aAAA;YAED,IAAIxB,OAAAA,CAAQC,WAAW,EAAE;gBACvB,OAAO0B,WAAAA;AACT;YAEA,OAAO;AACL,gBAAA;oBACElC,IAAM,EAAA,KAAA;AACNmC,oBAAAA,IAAAA,EAAM,CAAC,EAAET,QAAAA,CAAS,iBAAiB,EAAEK,SAAS,CAAC;oBAC/CK,YAAc,EAAA,CAAC,UAAU,EAAEL,QAAAA,CAAS,cAAc,EAAEA,QAAAA,CAAS,IAAI;AACnE,iBAAA;AACGG,gBAAAA,GAAAA;AACJ,aAAA;AACH;AACF,KAAA,CAAA;AACF,CAAA;;;;"}
1
+ {"version":3,"file":"api.mjs","sources":["../../src/plops/api.ts"],"sourcesContent":["import { join } from 'path';\nimport type { ActionType, NodePlopAPI } from 'plop';\nimport fs from 'fs-extra';\nimport tsUtils from '@strapi/typescript-utils';\n\nimport validateInput from './utils/validate-input';\nimport getFilePath from './utils/get-file-path';\nimport { appendToFile } from './utils/extend-plugin-index-files';\n\nexport default (plop: NodePlopAPI) => {\n // API generator\n plop.setGenerator('api', {\n description: 'Generate a basic API',\n prompts: [\n {\n type: 'input',\n name: 'id',\n message: 'API name',\n validate: (input) => validateInput(input),\n },\n {\n type: 'confirm',\n name: 'isPluginApi',\n message: 'Is this API for a plugin?',\n },\n {\n when: (answers) => answers.isPluginApi,\n type: 'list',\n name: 'plugin',\n message: 'Plugin name',\n async choices() {\n const pluginsPath = join(plop.getDestBasePath(), 'plugins');\n const exists = await fs.pathExists(pluginsPath);\n if (!exists) {\n throw Error('Couldn\\'t find a \"plugins\" directory');\n }\n\n const pluginsDir = await fs.readdir(pluginsPath, { withFileTypes: true });\n const pluginsDirContent = pluginsDir.filter((fd) => fd.isDirectory());\n\n if (pluginsDirContent.length === 0) {\n throw Error('The \"plugins\" directory is empty');\n }\n\n return pluginsDirContent;\n },\n },\n ],\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const filePath = getFilePath(\n answers.destination || (answers.isPluginApi && answers.plugin ? 'plugin' : 'api')\n );\n const currentDir = process.cwd();\n let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n if (answers.plugin) {\n // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.\n const pluginServerDir = join(\n currentDir,\n 'src',\n filePath.replace('{{ plugin }}', answers.plugin),\n '../'\n );\n language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';\n }\n\n const baseActions: Array<ActionType> = [\n {\n type: 'add',\n path: `${filePath}/controllers/{{ id }}.${language}`,\n templateFile: `templates/${language}/controller.${language}.hbs`,\n },\n {\n type: 'add',\n path: `${filePath}/services/{{ id }}.${language}`,\n templateFile: `templates/${language}/service.${language}.hbs`,\n },\n {\n type: 'add',\n path: `${filePath}/routes/${answers.plugin ? 'content-api/' : ''}{{ id }}.${language}`,\n templateFile: `templates/${language}/single-route.${language}.hbs`,\n },\n ];\n\n if (answers.isPluginApi) {\n const indexFiles = ['controllers', 'services', 'routes'];\n\n indexFiles.forEach((type) => {\n const indexPath = join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);\n const exists = fs.existsSync(indexPath);\n\n if (!exists && type !== 'routes') {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n if (type === 'routes') {\n const indexPath = join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);\n const exists = fs.existsSync(indexPath);\n\n if (!exists) {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.routes.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n const routeIndexFiles = ['content-api', 'admin'];\n\n routeIndexFiles.forEach((routeType) => {\n const routeTypeIndexPath = join(\n plop.getDestBasePath(),\n `${filePath}/${type}/${routeType}/index.${language}`\n );\n const routeTypeExists = fs.existsSync(routeTypeIndexPath);\n\n if (!routeTypeExists) {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/${routeType}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.routes.type.index.${language}.hbs`,\n data: { type: routeType },\n skipIfExists: true,\n });\n }\n });\n }\n\n baseActions.push({\n type: 'modify',\n path: `${filePath}/${type}/${type === 'routes' ? 'content-api/' : ''}index.${language}`,\n transform(template: string) {\n if (type === 'routes') {\n return appendToFile(template, { type: 'routes', singularName: answers.id });\n }\n\n return appendToFile(template, { type: 'index', singularName: answers.id });\n },\n });\n });\n }\n\n return baseActions;\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","type","name","message","validate","input","validateInput","when","answers","isPluginApi","choices","pluginsPath","join","getDestBasePath","exists","fs","pathExists","Error","pluginsDir","readdir","withFileTypes","pluginsDirContent","filter","fd","isDirectory","length","actions","filePath","getFilePath","destination","plugin","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","pluginServerDir","replace","baseActions","path","templateFile","indexFiles","forEach","indexPath","existsSync","push","skipIfExists","routeIndexFiles","routeType","routeTypeIndexPath","routeTypeExists","data","transform","template","appendToFile","singularName","id"],"mappings":";;;;;;;AASA,kBAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,KAAO,EAAA;QACvBC,WAAa,EAAA,sBAAA;QACbC,OAAS,EAAA;AACP,YAAA;gBACEC,IAAM,EAAA,OAAA;gBACNC,IAAM,EAAA,IAAA;gBACNC,OAAS,EAAA,UAAA;gBACTC,QAAU,EAAA,CAACC,QAAUC,aAAcD,CAAAA,KAAAA;AACrC,aAAA;AACA,YAAA;gBACEJ,IAAM,EAAA,SAAA;gBACNC,IAAM,EAAA,aAAA;gBACNC,OAAS,EAAA;AACX,aAAA;AACA,YAAA;gBACEI,IAAM,EAAA,CA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AIrD,SAAS,QAAU,EAAA;AACrB,gCAAA,OAAOsD,aAAaD,QAAU,EAAA;oCAAErD,IAAM,EAAA,QAAA;AAAUuD,oCAAAA,YAAAA,EAAchD,QAAQiD;AAAG,iCAAA,CAAA;AAC3E;AAEA,4BAAA,OAAOF,aAAaD,QAAU,EAAA;gCAAErD,IAAM,EAAA,OAAA;AAASuD,gCAAAA,YAAAA,EAAchD,QAAQiD;AAAG,6BAAA,CAAA;AAC1E;AACF,qBAAA,CAAA;AACF,iBAAA,CAAA;AACF;YAEA,OAAOlB,WAAAA;AACT;AACF,KAAA,CAAA;AACF,CAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"content-type.d.ts","sourceRoot":"","sources":["../../src/plops/content-type.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAc,MAAM,MAAM,CAAC;+BAa9B,WAAW;AAAjC,wBAoIE"}
1
+ {"version":3,"file":"content-type.d.ts","sourceRoot":"","sources":["../../src/plops/content-type.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAc,MAAM,MAAM,CAAC;+BAc9B,WAAW;AAAjC,wBAqPE"}
@@ -11,6 +11,7 @@ var ctNamesPrompts = require('./prompts/ct-names-prompts.js');
11
11
  var kindPrompts = require('./prompts/kind-prompts.js');
12
12
  var getAttributesPrompts = require('./prompts/get-attributes-prompts.js');
13
13
  var bootstrapApiPrompts = require('./prompts/bootstrap-api-prompts.js');
14
+ var extendPluginIndexFiles = require('./utils/extend-plugin-index-files.js');
14
15
 
15
16
  var generateContentType = ((plop)=>{
16
17
  // Model generator
@@ -21,7 +22,6 @@ var generateContentType = ((plop)=>{
21
22
  ...ctNamesPrompts,
22
23
  ...kindPrompts
23
24
  ]);
24
- // @ts-expect-error issue with deprecated inquirer.prompts attribute to fix with ugprade to inquirer
25
25
  const attributes = await getAttributesPrompts(inquirer);
26
26
  const api = await inquirer.prompt([
27
27
  ...getDestinationPrompts('model', plop.getDestBasePath()),
@@ -84,7 +84,12 @@ var generateContentType = ((plop)=>{
84
84
  }, {});
85
85
  const filePath = getFilePath(answers.destination);
86
86
  const currentDir = process.cwd();
87
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
87
+ let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
88
+ if (answers.plugin) {
89
+ // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.
90
+ const pluginServerDir = path.join(currentDir, 'src', filePath.replace('{{ plugin }}', answers.plugin), '../');
91
+ language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';
92
+ }
88
93
  const baseActions = [
89
94
  {
90
95
  type: 'add',
@@ -108,6 +113,30 @@ var generateContentType = ((plop)=>{
108
113
  }
109
114
  });
110
115
  }
116
+ if (answers.plugin) {
117
+ const indexPath = path.join(plop.getDestBasePath(), `${filePath}/content-types/index.${language}`);
118
+ const exists = fs.existsSync(indexPath);
119
+ if (!exists) {
120
+ // Create index file if it doesn't exist
121
+ baseActions.push({
122
+ type: 'add',
123
+ path: `${filePath}/content-types/index.${language}`,
124
+ templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,
125
+ skipIfExists: true
126
+ });
127
+ }
128
+ // Append the new content type to the index.ts file
129
+ baseActions.push({
130
+ type: 'modify',
131
+ path: `${filePath}/content-types/index.${language}`,
132
+ transform (template) {
133
+ return extendPluginIndexFiles.appendToFile(template, {
134
+ type: 'content-type',
135
+ singularName: answers.singularName
136
+ });
137
+ }
138
+ });
139
+ }
111
140
  if (answers.bootstrapApi) {
112
141
  const { singularName } = answers;
113
142
  let uid;
@@ -134,12 +163,78 @@ var generateContentType = ((plop)=>{
134
163
  }
135
164
  }, {
136
165
  type: 'add',
137
- path: `${filePath}/routes/{{ singularName }}.${language}`,
166
+ path: `${filePath}/routes/${answers.plugin ? 'content-api/' : ''}{{ singularName }}.${language}`,
138
167
  templateFile: `templates/${language}/core-router.${language}.hbs`,
139
168
  data: {
140
169
  uid
141
170
  }
142
171
  });
172
+ if (answers.plugin) {
173
+ const indexFiles = [
174
+ 'controllers',
175
+ 'services',
176
+ 'routes'
177
+ ];
178
+ indexFiles.forEach((type)=>{
179
+ const indexPath = path.join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);
180
+ const exists = fs.existsSync(indexPath);
181
+ if (!exists && type !== 'routes') {
182
+ baseActions.push({
183
+ type: 'add',
184
+ path: `${filePath}/${type}/index.${language}`,
185
+ templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,
186
+ skipIfExists: true
187
+ });
188
+ }
189
+ if (type === 'routes') {
190
+ const indexPath = path.join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);
191
+ const exists = fs.existsSync(indexPath);
192
+ if (!exists) {
193
+ baseActions.push({
194
+ type: 'add',
195
+ path: `${filePath}/${type}/index.${language}`,
196
+ templateFile: `templates/${language}/plugin/plugin.routes.index.${language}.hbs`,
197
+ skipIfExists: true
198
+ });
199
+ }
200
+ const routeIndexFiles = [
201
+ 'content-api',
202
+ 'admin'
203
+ ];
204
+ routeIndexFiles.forEach((routeType)=>{
205
+ const routeTypeIndexPath = path.join(plop.getDestBasePath(), `${filePath}/${type}/${routeType}/index.${language}`);
206
+ const routeTypeExists = fs.existsSync(routeTypeIndexPath);
207
+ if (!routeTypeExists) {
208
+ baseActions.push({
209
+ type: 'add',
210
+ path: `${filePath}/${type}/${routeType}/index.${language}`,
211
+ templateFile: `templates/${language}/plugin/plugin.routes.type.index.${language}.hbs`,
212
+ data: {
213
+ type: routeType
214
+ },
215
+ skipIfExists: true
216
+ });
217
+ }
218
+ });
219
+ }
220
+ baseActions.push({
221
+ type: 'modify',
222
+ path: `${filePath}/${type}/${type === 'routes' ? 'content-api/' : ''}index.${language}`,
223
+ transform (template) {
224
+ if (type === 'routes') {
225
+ return extendPluginIndexFiles.appendToFile(template, {
226
+ type: 'routes',
227
+ singularName: answers.singularName
228
+ });
229
+ }
230
+ return extendPluginIndexFiles.appendToFile(template, {
231
+ type: 'index',
232
+ singularName: answers.singularName
233
+ });
234
+ }
235
+ });
236
+ });
237
+ }
143
238
  }
144
239
  return baseActions;
145
240
  }
@@ -1 +1 @@
1
- {"version":3,"file":"content-type.js","sources":["../../src/plops/content-type.ts"],"sourcesContent":["import { join } from 'path';\nimport type { NodePlopAPI, ActionType } from 'plop';\nimport slugify from '@sindresorhus/slugify';\nimport fs from 'fs-extra';\nimport { strings } from '@strapi/utils';\nimport tsUtils from '@strapi/typescript-utils';\n\nimport getDestinationPrompts from './prompts/get-destination-prompts';\nimport getFilePath from './utils/get-file-path';\nimport ctNamesPrompts from './prompts/ct-names-prompts';\nimport kindPrompts from './prompts/kind-prompts';\nimport getAttributesPrompts from './prompts/get-attributes-prompts';\nimport bootstrapApiPrompts from './prompts/bootstrap-api-prompts';\n\nexport default (plop: NodePlopAPI) => {\n // Model generator\n plop.setGenerator('content-type', {\n description: 'Generate a content type for an API',\n async prompts(inquirer) {\n const config = await inquirer.prompt([...ctNamesPrompts, ...kindPrompts]);\n // @ts-expect-error issue with deprecated inquirer.prompts attribute to fix with ugprade to inquirer\n const attributes = await getAttributesPrompts(inquirer);\n\n const api = await inquirer.prompt([\n ...getDestinationPrompts('model', plop.getDestBasePath()),\n {\n when: (answers) => answers.destination === 'new',\n type: 'input',\n name: 'id',\n default: config.singularName,\n message: 'Name of the new API?',\n async validate(input) {\n if (!strings.isKebabCase(input)) {\n return 'Value must be in kebab-case';\n }\n\n const apiPath = join(plop.getDestBasePath(), 'api');\n const exists = await fs.pathExists(apiPath);\n\n if (!exists) {\n return true;\n }\n\n const apiDir = await fs.readdir(apiPath, { withFileTypes: true });\n const apiDirContent = apiDir.filter((fd) => fd.isDirectory());\n\n if (apiDirContent.findIndex((dir) => dir.name === input) !== -1) {\n throw new Error('This name is already taken.');\n }\n\n return true;\n },\n },\n ...bootstrapApiPrompts,\n ]);\n\n return {\n ...config,\n ...api,\n attributes,\n };\n },\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const attributes = answers.attributes.reduce((object: any, answer: any) => {\n const val: any = { type: answer.attributeType };\n\n if (answer.attributeType === 'enumeration') {\n val.enum = answer.enum.split(',').map((item: string) => item.trim());\n }\n\n if (answer.attributeType === 'media') {\n val.allowedTypes = ['images', 'files', 'videos', 'audios'];\n val.multiple = answer.multiple;\n }\n\n return Object.assign(object, { [answer.attributeName]: val }, {});\n }, {});\n\n const filePath = getFilePath(answers.destination);\n const currentDir = process.cwd();\n const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n const baseActions: Array<ActionType> = [\n {\n type: 'add',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n templateFile: `templates/${language}/content-type.schema.json.hbs`,\n data: {\n collectionName: slugify(answers.pluralName, { separator: '_' }),\n },\n },\n ];\n\n if (Object.entries(attributes).length > 0) {\n baseActions.push({\n type: 'modify',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n transform(template: string) {\n const parsedTemplate = JSON.parse(template);\n parsedTemplate.attributes = attributes;\n return JSON.stringify(parsedTemplate, null, 2);\n },\n });\n }\n\n if (answers.bootstrapApi) {\n const { singularName } = answers;\n\n let uid;\n if (answers.destination === 'new') {\n uid = `api::${answers.id}.${singularName}`;\n } else if (answers.api) {\n uid = `api::${answers.api}.${singularName}`;\n } else if (answers.plugin) {\n uid = `plugin::${answers.plugin}.${singularName}`;\n }\n\n baseActions.push(\n {\n type: 'add',\n path: `${filePath}/controllers/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-controller.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/services/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-service.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/routes/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-router.${language}.hbs`,\n data: { uid },\n }\n );\n }\n\n return baseActions;\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","inquirer","config","prompt","ctNamesPrompts","kindPrompts","attributes","getAttributesPrompts","api","getDestinationPrompts","getDestBasePath","when","answers","destination","type","name","default","singularName","message","validate","input","strings","isKebabCase","apiPath","join","exists","fs","pathExists","apiDir","readdir","withFileTypes","apiDirContent","filter","fd","isDirectory","findIndex","dir","Error","bootstrapApiPrompts","actions","reduce","object","answer","val","attributeType","enum","split","map","item","trim","allowedTypes","multiple","Object","assign","attributeName","filePath","getFilePath","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","baseActions","path","templateFile","data","collectionName","slugify","pluralName","separator","entries","length","push","transform","template","parsedTemplate","JSON","parse","stringify","bootstrapApi","uid","id","plugin"],"mappings":";;;;;;;;;;;;;;AAcA,0BAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,cAAgB,EAAA;QAChCC,WAAa,EAAA,oCAAA;AACb,QAAA,MAAMC,SAAQC,QAAQ,EAAA;AACpB,YAAA,MAAMC,MAAS,GAAA,MAAMD,QAASE,CAAAA,MAAM,CAAC;AAAIC,gBAAAA,GAAAA,cAAAA;AAAmBC,gBAAAA,GAAAA;AAAY,aAAA,CAAA;;YAExE,MAAMC,UAAAA,GAAa,MAAMC,oBAAqBN,CAAAA,QAAAA,CAAAA;AAE9C,YAAA,MAAMO,GAAM,GAAA,MAAMP,QAASE,CAAAA,MAAM,CAAC;mBAC7BM,qBAAsB,CAAA,OAAA,EAASZ,KAAKa,eAAe,EAAA,CAAA;AACtD,gBAAA;AACEC,oBAAAA,IAAAA,EAAM,CAACC,OAAAA,GAAYA,OAAQC,CAAAA,WAAW,KAAK,KAAA;oBAC3CC,IAAM,EAAA,OAAA;oBACNC,IAAM,EAAA,IAAA;AACNC,oBAAAA,OAAAA,EAASd,OAAOe,YAAY;oBAC5BC,OAAS,EAAA,sBAAA;AACT,oBAAA,MAAMC,UAASC,KAAK,EAAA;AAClB,wBAAA,IAAI,CAACC,aAAAA,CAAQC,WAAW,CAACF,KAAQ,CAAA,EAAA;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e,wBAAAA;AAAI;iBAEd,EAAA;oBACEnE,IAAM,EAAA,KAAA;AACNkD,oBAAAA,IAAAA,EAAM,CAAC,EAAET,QAAAA,CAAS,6BAA6B,EAAEK,SAAS,CAAC;oBAC3DK,YAAc,EAAA,CAAC,UAAU,EAAEL,QAAAA,CAAS,cAAc,EAAEA,QAAAA,CAAS,IAAI,CAAC;oBAClEM,IAAM,EAAA;AAAEe,wBAAAA;AAAI;iBAEd,EAAA;oBACEnE,IAAM,EAAA,KAAA;AACNkD,oBAAAA,IAAAA,EAAM,CAAC,EAAET,QAAAA,CAAS,2BAA2B,EAAEK,SAAS,CAAC;oBACzDK,YAAc,EAAA,CAAC,UAAU,EAAEL,QAAAA,CAAS,aAAa,EAAEA,QAAAA,CAAS,IAAI,CAAC;oBACjEM,IAAM,EAAA;AAAEe,wBAAAA;AAAI;AACd,iBAAA,CAAA;AAEJ;YAEA,OAAOlB,WAAAA;AACT;AACF,KAAA,CAAA;AACF,CAAA;;;;"}
1
+ {"version":3,"file":"content-type.js","sources":["../../src/plops/content-type.ts"],"sourcesContent":["import { join } from 'path';\nimport type { NodePlopAPI, ActionType } from 'plop';\nimport slugify from '@sindresorhus/slugify';\nimport fs from 'fs-extra';\nimport { strings } from '@strapi/utils';\nimport tsUtils from '@strapi/typescript-utils';\n\nimport getDestinationPrompts from './prompts/get-destination-prompts';\nimport getFilePath from './utils/get-file-path';\nimport ctNamesPrompts from './prompts/ct-names-prompts';\nimport kindPrompts from './prompts/kind-prompts';\nimport getAttributesPrompts from './prompts/get-attributes-prompts';\nimport bootstrapApiPrompts from './prompts/bootstrap-api-prompts';\nimport { appendToFile } from './utils/extend-plugin-index-files';\n\nexport default (plop: NodePlopAPI) => {\n // Model generator\n plop.setGenerator('content-type', {\n description: 'Generate a content type for an API',\n async prompts(inquirer) {\n const config = await inquirer.prompt([...ctNamesPrompts, ...kindPrompts]);\n const attributes = await getAttributesPrompts(inquirer);\n\n const api = await inquirer.prompt([\n ...getDestinationPrompts('model', plop.getDestBasePath()),\n {\n when: (answers) => answers.destination === 'new',\n type: 'input',\n name: 'id',\n default: config.singularName,\n message: 'Name of the new API?',\n async validate(input) {\n if (!strings.isKebabCase(input)) {\n return 'Value must be in kebab-case';\n }\n\n const apiPath = join(plop.getDestBasePath(), 'api');\n const exists = await fs.pathExists(apiPath);\n\n if (!exists) {\n return true;\n }\n\n const apiDir = await fs.readdir(apiPath, { withFileTypes: true });\n const apiDirContent = apiDir.filter((fd) => fd.isDirectory());\n\n if (apiDirContent.findIndex((dir) => dir.name === input) !== -1) {\n throw new Error('This name is already taken.');\n }\n\n return true;\n },\n },\n ...bootstrapApiPrompts,\n ]);\n\n return {\n ...config,\n ...api,\n attributes,\n };\n },\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const attributes = answers.attributes.reduce((object: any, answer: any) => {\n const val: any = { type: answer.attributeType };\n\n if (answer.attributeType === 'enumeration') {\n val.enum = answer.enum.split(',').map((item: string) => item.trim());\n }\n\n if (answer.attributeType === 'media') {\n val.allowedTypes = ['images', 'files', 'videos', 'audios'];\n val.multiple = answer.multiple;\n }\n\n return Object.assign(object, { [answer.attributeName]: val }, {});\n }, {});\n\n const filePath = getFilePath(answers.destination);\n const currentDir = process.cwd();\n let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n if (answers.plugin) {\n // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.\n const pluginServerDir = join(\n currentDir,\n 'src',\n filePath.replace('{{ plugin }}', answers.plugin),\n '../'\n );\n language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';\n }\n\n const baseActions: Array<ActionType> = [\n {\n type: 'add',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n templateFile: `templates/${language}/content-type.schema.json.hbs`,\n data: {\n collectionName: slugify(answers.pluralName, { separator: '_' }),\n },\n },\n ];\n\n if (Object.entries(attributes).length > 0) {\n baseActions.push({\n type: 'modify',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n transform(template: string) {\n const parsedTemplate = JSON.parse(template);\n parsedTemplate.attributes = attributes;\n return JSON.stringify(parsedTemplate, null, 2);\n },\n });\n }\n\n if (answers.plugin) {\n const indexPath = join(\n plop.getDestBasePath(),\n `${filePath}/content-types/index.${language}`\n );\n const exists = fs.existsSync(indexPath);\n\n if (!exists) {\n // Create index file if it doesn't exist\n baseActions.push({\n type: 'add',\n path: `${filePath}/content-types/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n // Append the new content type to the index.ts file\n baseActions.push({\n type: 'modify',\n path: `${filePath}/content-types/index.${language}`,\n transform(template: string) {\n return appendToFile(template, {\n type: 'content-type',\n singularName: answers.singularName,\n });\n },\n });\n }\n\n if (answers.bootstrapApi) {\n const { singularName } = answers;\n\n let uid;\n if (answers.destination === 'new') {\n uid = `api::${answers.id}.${singularName}`;\n } else if (answers.api) {\n uid = `api::${answers.api}.${singularName}`;\n } else if (answers.plugin) {\n uid = `plugin::${answers.plugin}.${singularName}`;\n }\n\n baseActions.push(\n {\n type: 'add',\n path: `${filePath}/controllers/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-controller.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/services/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-service.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/routes/${answers.plugin ? 'content-api/' : ''}{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-router.${language}.hbs`,\n data: { uid },\n }\n );\n\n if (answers.plugin) {\n const indexFiles = ['controllers', 'services', 'routes'];\n\n indexFiles.forEach((type) => {\n const indexPath = join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);\n const exists = fs.existsSync(indexPath);\n\n if (!exists && type !== 'routes') {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n if (type === 'routes') {\n const indexPath = join(\n plop.getDestBasePath(),\n `${filePath}/${type}/index.${language}`\n );\n const exists = fs.existsSync(indexPath);\n\n if (!exists) {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.routes.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n const routeIndexFiles = ['content-api', 'admin'];\n\n routeIndexFiles.forEach((routeType) => {\n const routeTypeIndexPath = join(\n plop.getDestBasePath(),\n `${filePath}/${type}/${routeType}/index.${language}`\n );\n const routeTypeExists = fs.existsSync(routeTypeIndexPath);\n\n if (!routeTypeExists) {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/${routeType}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.routes.type.index.${language}.hbs`,\n data: { type: routeType },\n skipIfExists: true,\n });\n }\n });\n }\n\n baseActions.push({\n type: 'modify',\n path: `${filePath}/${type}/${type === 'routes' ? 'content-api/' : ''}index.${language}`,\n transform(template: string) {\n if (type === 'routes') {\n return appendToFile(template, {\n type: 'routes',\n singularName: answers.singularName,\n });\n }\n\n return appendToFile(template, {\n type: 'index',\n singularName: answers.singularName,\n });\n },\n });\n });\n }\n }\n\n return baseActions;\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","inquirer","config","prompt","ctNamesPrompts","kindPrompts","attributes","getAttributesPrompts","api","getDestinationPrompts","getDestBasePath","when","answers","destination","type","name","default","singularName","message","validate","input","strings","isKebabCase","apiPath","join","exists","fs","pathExists","apiDir","readdir","withFileTypes","apiDirContent","filter","fd","isDirectory","findIndex","dir","Error","bootstrapApiPrompts","actions","reduce","object","answer","val","attributeType","enum","split","map","item","trim","allowedTypes","multiple","Object","assign","attributeName","filePath","getFilePath","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","plugin","pluginServerDir","replace","baseActions","path","templateFile","data","collectionName","slugify","pluralName","separator","entries","length","push","transform","template","parsedTemplate","JSON","parse","stringify","indexPath","existsSync","skipIfExists","appendToFile","bootstrapApi","uid","id","indexFiles","forEach","routeIndexFiles","routeType","routeTypeIndexPath","routeTypeExists"],"mappings":";;;;;;;;;;;;;;;AAeA,0BAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,cAAgB,EAAA;QAChCC,WAAa,EAAA,oCAAA;AACb,QAAA,MAAMC,SAAQC,QAAQ,EAAA;AACpB,YAAA,MAAMC,MAAS,GAAA,MAAMD,QAASE,CAAAA,MAAM,CAAC;AAAIC,gBAAAA,GAAAA,cAAAA;AAAmBC,gBAAAA,GAAAA;AAAY,aAAA,CAAA;YACxE,MAAMC,UAAAA,GAAa,MAAMC,oBAAqBN,CAAAA,QAAAA,CAAAA;AAE9C,YAAA,MAAMO,GAAM,GAAA,MAAMP,QAASE,CAAAA,MAAM,CAAC;mBAC7BM,qBAAsB,CAAA,OAAA,EAASZ,KAAKa,eAAe,EAAA,CAAA;AACtD,gBAAA;AACEC,oBAAAA,IAAAA,EAAM,CAACC,OAAAA,GAAYA,OAAQC,CAAAA,WAAW,KAAK,KAAA;oBAC3CC,IAAM,EAAA,OAAA;oBACNC,IAAM,EAAA,IAAA;AACNC,oBAAAA,OAAAA,EAASd,OAAOe,YAAY;oBAC5BC,OAAS,EAAA,sBAAA;AACT,oBAAA,MAAMC,UAASC,KAAK,EAAA;AAClB,wBAAA,IAAI,CAACC,aAAAA,CAAQC,WAAW,CAACF,KAAQ,CAAA,EAAA;4BAC/B,OAAO,6BAAA;AACT;AAEA,wBAAA,MAAMG,OAAUC,GAAAA,SAAAA,CAAK3B,IAAKa,CAAAA,eAAe,EAAI,EAAA,KAAA,CAAA;AAC7C,wBAAA,MAAMe,MAAS,GAAA,MAAMC,EAAGC,CAAAA,UAAU,CAACJ,OAAAA,CAAAA;AAEnC,wBAAA,IAAI,CAACE,MAAQ,EAAA;4BACX,OAAO,IAAA;AACT;AAEA,wBAAA,MAAM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AA,CAAU,OAAO,EAAEjC,QAAU,CAAA,CAAA;wCAC1DQ,YAAc,EAAA,CAAC,UAAU,EAAER,QAAAA,CAAS,iCAAiC,EAAEA,QAAAA,CAAS,IAAI,CAAC;wCACrFS,IAAM,EAAA;4CAAEvD,IAAM+E,EAAAA;AAAU,yCAAA;wCACxBR,YAAc,EAAA;AAChB,qCAAA,CAAA;AACF;AACF,6BAAA,CAAA;AACF;AAEAnB,wBAAAA,WAAAA,CAAYU,IAAI,CAAC;4BACf9D,IAAM,EAAA,QAAA;AACNqD,4BAAAA,IAAAA,EAAM,CAAGZ,EAAAA,QAAAA,CAAS,CAAC,EAAEzC,IAAK,CAAA,CAAC,EAAEA,IAAAA,KAAS,QAAW,GAAA,cAAA,GAAiB,EAAG,CAAA,MAAM,EAAE8C,QAAU,CAAA,CAAA;AACvFiB,4BAAAA,SAAAA,CAAAA,CAAUC,QAAgB,EAAA;AACxB,gCAAA,IAAIhE,SAAS,QAAU,EAAA;AACrB,oCAAA,OAAOwE,oCAAaR,QAAU,EAAA;wCAC5BhE,IAAM,EAAA,QAAA;AACNG,wCAAAA,YAAAA,EAAcL,QAAQK;AACxB,qCAAA,CAAA;AACF;AAEA,gCAAA,OAAOqE,oCAAaR,QAAU,EAAA;oCAC5BhE,IAAM,EAAA,OAAA;AACNG,oCAAAA,YAAAA,EAAcL,QAAQK;AACxB,iCAAA,CAAA;AACF;AACF,yBAAA,CAAA;AACF,qBAAA,CAAA;AACF;AACF;YAEA,OAAOiD,WAAAA;AACT;AACF,KAAA,CAAA;AACF,CAAA;;;;"}
@@ -9,6 +9,7 @@ import questions from './prompts/ct-names-prompts.mjs';
9
9
  import questions$1 from './prompts/kind-prompts.mjs';
10
10
  import getAttributesPrompts from './prompts/get-attributes-prompts.mjs';
11
11
  import questions$2 from './prompts/bootstrap-api-prompts.mjs';
12
+ import { appendToFile } from './utils/extend-plugin-index-files.mjs';
12
13
 
13
14
  var generateContentType = ((plop)=>{
14
15
  // Model generator
@@ -19,7 +20,6 @@ var generateContentType = ((plop)=>{
19
20
  ...questions,
20
21
  ...questions$1
21
22
  ]);
22
- // @ts-expect-error issue with deprecated inquirer.prompts attribute to fix with ugprade to inquirer
23
23
  const attributes = await getAttributesPrompts(inquirer);
24
24
  const api = await inquirer.prompt([
25
25
  ...getDestinationPrompts('model', plop.getDestBasePath()),
@@ -82,7 +82,12 @@ var generateContentType = ((plop)=>{
82
82
  }, {});
83
83
  const filePath = getFilePath(answers.destination);
84
84
  const currentDir = process.cwd();
85
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
85
+ let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
86
+ if (answers.plugin) {
87
+ // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.
88
+ const pluginServerDir = join(currentDir, 'src', filePath.replace('{{ plugin }}', answers.plugin), '../');
89
+ language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';
90
+ }
86
91
  const baseActions = [
87
92
  {
88
93
  type: 'add',
@@ -106,6 +111,30 @@ var generateContentType = ((plop)=>{
106
111
  }
107
112
  });
108
113
  }
114
+ if (answers.plugin) {
115
+ const indexPath = join(plop.getDestBasePath(), `${filePath}/content-types/index.${language}`);
116
+ const exists = fs.existsSync(indexPath);
117
+ if (!exists) {
118
+ // Create index file if it doesn't exist
119
+ baseActions.push({
120
+ type: 'add',
121
+ path: `${filePath}/content-types/index.${language}`,
122
+ templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,
123
+ skipIfExists: true
124
+ });
125
+ }
126
+ // Append the new content type to the index.ts file
127
+ baseActions.push({
128
+ type: 'modify',
129
+ path: `${filePath}/content-types/index.${language}`,
130
+ transform (template) {
131
+ return appendToFile(template, {
132
+ type: 'content-type',
133
+ singularName: answers.singularName
134
+ });
135
+ }
136
+ });
137
+ }
109
138
  if (answers.bootstrapApi) {
110
139
  const { singularName } = answers;
111
140
  let uid;
@@ -132,12 +161,78 @@ var generateContentType = ((plop)=>{
132
161
  }
133
162
  }, {
134
163
  type: 'add',
135
- path: `${filePath}/routes/{{ singularName }}.${language}`,
164
+ path: `${filePath}/routes/${answers.plugin ? 'content-api/' : ''}{{ singularName }}.${language}`,
136
165
  templateFile: `templates/${language}/core-router.${language}.hbs`,
137
166
  data: {
138
167
  uid
139
168
  }
140
169
  });
170
+ if (answers.plugin) {
171
+ const indexFiles = [
172
+ 'controllers',
173
+ 'services',
174
+ 'routes'
175
+ ];
176
+ indexFiles.forEach((type)=>{
177
+ const indexPath = join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);
178
+ const exists = fs.existsSync(indexPath);
179
+ if (!exists && type !== 'routes') {
180
+ baseActions.push({
181
+ type: 'add',
182
+ path: `${filePath}/${type}/index.${language}`,
183
+ templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,
184
+ skipIfExists: true
185
+ });
186
+ }
187
+ if (type === 'routes') {
188
+ const indexPath = join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);
189
+ const exists = fs.existsSync(indexPath);
190
+ if (!exists) {
191
+ baseActions.push({
192
+ type: 'add',
193
+ path: `${filePath}/${type}/index.${language}`,
194
+ templateFile: `templates/${language}/plugin/plugin.routes.index.${language}.hbs`,
195
+ skipIfExists: true
196
+ });
197
+ }
198
+ const routeIndexFiles = [
199
+ 'content-api',
200
+ 'admin'
201
+ ];
202
+ routeIndexFiles.forEach((routeType)=>{
203
+ const routeTypeIndexPath = join(plop.getDestBasePath(), `${filePath}/${type}/${routeType}/index.${language}`);
204
+ const routeTypeExists = fs.existsSync(routeTypeIndexPath);
205
+ if (!routeTypeExists) {
206
+ baseActions.push({
207
+ type: 'add',
208
+ path: `${filePath}/${type}/${routeType}/index.${language}`,
209
+ templateFile: `templates/${language}/plugin/plugin.routes.type.index.${language}.hbs`,
210
+ data: {
211
+ type: routeType
212
+ },
213
+ skipIfExists: true
214
+ });
215
+ }
216
+ });
217
+ }
218
+ baseActions.push({
219
+ type: 'modify',
220
+ path: `${filePath}/${type}/${type === 'routes' ? 'content-api/' : ''}index.${language}`,
221
+ transform (template) {
222
+ if (type === 'routes') {
223
+ return appendToFile(template, {
224
+ type: 'routes',
225
+ singularName: answers.singularName
226
+ });
227
+ }
228
+ return appendToFile(template, {
229
+ type: 'index',
230
+ singularName: answers.singularName
231
+ });
232
+ }
233
+ });
234
+ });
235
+ }
141
236
  }
142
237
  return baseActions;
143
238
  }
@@ -1 +1 @@
1
- {"version":3,"file":"content-type.mjs","sources":["../../src/plops/content-type.ts"],"sourcesContent":["import { join } from 'path';\nimport type { NodePlopAPI, ActionType } from 'plop';\nimport slugify from '@sindresorhus/slugify';\nimport fs from 'fs-extra';\nimport { strings } from '@strapi/utils';\nimport tsUtils from '@strapi/typescript-utils';\n\nimport getDestinationPrompts from './prompts/get-destination-prompts';\nimport getFilePath from './utils/get-file-path';\nimport ctNamesPrompts from './prompts/ct-names-prompts';\nimport kindPrompts from './prompts/kind-prompts';\nimport getAttributesPrompts from './prompts/get-attributes-prompts';\nimport bootstrapApiPrompts from './prompts/bootstrap-api-prompts';\n\nexport default (plop: NodePlopAPI) => {\n // Model generator\n plop.setGenerator('content-type', {\n description: 'Generate a content type for an API',\n async prompts(inquirer) {\n const config = await inquirer.prompt([...ctNamesPrompts, ...kindPrompts]);\n // @ts-expect-error issue with deprecated inquirer.prompts attribute to fix with ugprade to inquirer\n const attributes = await getAttributesPrompts(inquirer);\n\n const api = await inquirer.prompt([\n ...getDestinationPrompts('model', plop.getDestBasePath()),\n {\n when: (answers) => answers.destination === 'new',\n type: 'input',\n name: 'id',\n default: config.singularName,\n message: 'Name of the new API?',\n async validate(input) {\n if (!strings.isKebabCase(input)) {\n return 'Value must be in kebab-case';\n }\n\n const apiPath = join(plop.getDestBasePath(), 'api');\n const exists = await fs.pathExists(apiPath);\n\n if (!exists) {\n return true;\n }\n\n const apiDir = await fs.readdir(apiPath, { withFileTypes: true });\n const apiDirContent = apiDir.filter((fd) => fd.isDirectory());\n\n if (apiDirContent.findIndex((dir) => dir.name === input) !== -1) {\n throw new Error('This name is already taken.');\n }\n\n return true;\n },\n },\n ...bootstrapApiPrompts,\n ]);\n\n return {\n ...config,\n ...api,\n attributes,\n };\n },\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const attributes = answers.attributes.reduce((object: any, answer: any) => {\n const val: any = { type: answer.attributeType };\n\n if (answer.attributeType === 'enumeration') {\n val.enum = answer.enum.split(',').map((item: string) => item.trim());\n }\n\n if (answer.attributeType === 'media') {\n val.allowedTypes = ['images', 'files', 'videos', 'audios'];\n val.multiple = answer.multiple;\n }\n\n return Object.assign(object, { [answer.attributeName]: val }, {});\n }, {});\n\n const filePath = getFilePath(answers.destination);\n const currentDir = process.cwd();\n const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n const baseActions: Array<ActionType> = [\n {\n type: 'add',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n templateFile: `templates/${language}/content-type.schema.json.hbs`,\n data: {\n collectionName: slugify(answers.pluralName, { separator: '_' }),\n },\n },\n ];\n\n if (Object.entries(attributes).length > 0) {\n baseActions.push({\n type: 'modify',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n transform(template: string) {\n const parsedTemplate = JSON.parse(template);\n parsedTemplate.attributes = attributes;\n return JSON.stringify(parsedTemplate, null, 2);\n },\n });\n }\n\n if (answers.bootstrapApi) {\n const { singularName } = answers;\n\n let uid;\n if (answers.destination === 'new') {\n uid = `api::${answers.id}.${singularName}`;\n } else if (answers.api) {\n uid = `api::${answers.api}.${singularName}`;\n } else if (answers.plugin) {\n uid = `plugin::${answers.plugin}.${singularName}`;\n }\n\n baseActions.push(\n {\n type: 'add',\n path: `${filePath}/controllers/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-controller.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/services/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-service.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/routes/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-router.${language}.hbs`,\n data: { uid },\n }\n );\n }\n\n return baseActions;\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","inquirer","config","prompt","ctNamesPrompts","kindPrompts","attributes","getAttributesPrompts","api","getDestinationPrompts","getDestBasePath","when","answers","destination","type","name","default","singularName","message","validate","input","strings","isKebabCase","apiPath","join","exists","fs","pathExists","apiDir","readdir","withFileTypes","apiDirContent","filter","fd","isDirectory","findIndex","dir","Error","bootstrapApiPrompts","actions","reduce","object","answer","val","attributeType","enum","split","map","item","trim","allowedTypes","multiple","Object","assign","attributeName","filePath","getFilePath","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","baseActions","path","templateFile","data","collectionName","slugify","pluralName","separator","entries","length","push","transform","template","parsedTemplate","JSON","parse","stringify","bootstrapApi","uid","id","plugin"],"mappings":";;;;;;;;;;;;AAcA,0BAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,cAAgB,EAAA;QAChCC,WAAa,EAAA,oCAAA;AACb,QAAA,MAAMC,SAAQC,QAAQ,EAAA;AACpB,YAAA,MAAMC,MAAS,GAAA,MAAMD,QAASE,CAAAA,MAAM,CAAC;AAAIC,gBAAAA,GAAAA,SAAAA;AAAmBC,gBAAAA,GAAAA;AAAY,aAAA,CAAA;;YAExE,MAAMC,UAAAA,GAAa,MAAMC,oBAAqBN,CAAAA,QAAAA,CAAAA;AAE9C,YAAA,MAAMO,GAAM,GAAA,MAAMP,QAASE,CAAAA,MAAM,CAAC;mBAC7BM,qBAAsB,CAAA,OAAA,EAASZ,KAAKa,eAAe,EAAA,CAAA;AACtD,gBAAA;AACEC,oBAAAA,IAAAA,EAAM,CAACC,OAAAA,GAAYA,OAAQC,CAAAA,WAAW,KAAK,KAAA;oBAC3CC,IAAM,EAAA,OAAA;oBACNC,IAAM,EAAA,IAAA;AACNC,oBAAAA,O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,QAAAA,CAAS,gCAAgC,EAAEK,SAAS,CAAC;oBAC9DK,YAAc,EAAA,CAAC,UAAU,EAAEL,QAAAA,CAAS,iBAAiB,EAAEA,QAAAA,CAAS,IAAI,CAAC;oBACrEM,IAAM,EAAA;AAAEe,wBAAAA;AAAI;iBAEd,EAAA;oBACEnE,IAAM,EAAA,KAAA;AACNkD,oBAAAA,IAAAA,EAAM,CAAC,EAAET,QAAAA,CAAS,6BAA6B,EAAEK,SAAS,CAAC;oBAC3DK,YAAc,EAAA,CAAC,UAAU,EAAEL,QAAAA,CAAS,cAAc,EAAEA,QAAAA,CAAS,IAAI,CAAC;oBAClEM,IAAM,EAAA;AAAEe,wBAAAA;AAAI;iBAEd,EAAA;oBACEnE,IAAM,EAAA,KAAA;AACNkD,oBAAAA,IAAAA,EAAM,CAAC,EAAET,QAAAA,CAAS,2BAA2B,EAAEK,SAAS,CAAC;oBACzDK,YAAc,EAAA,CAAC,UAAU,EAAEL,QAAAA,CAAS,aAAa,EAAEA,QAAAA,CAAS,IAAI,CAAC;oBACjEM,IAAM,EAAA;AAAEe,wBAAAA;AAAI;AACd,iBAAA,CAAA;AAEJ;YAEA,OAAOlB,WAAAA;AACT;AACF,KAAA,CAAA;AACF,CAAA;;;;"}
1
+ {"version":3,"file":"content-type.mjs","sources":["../../src/plops/content-type.ts"],"sourcesContent":["import { join } from 'path';\nimport type { NodePlopAPI, ActionType } from 'plop';\nimport slugify from '@sindresorhus/slugify';\nimport fs from 'fs-extra';\nimport { strings } from '@strapi/utils';\nimport tsUtils from '@strapi/typescript-utils';\n\nimport getDestinationPrompts from './prompts/get-destination-prompts';\nimport getFilePath from './utils/get-file-path';\nimport ctNamesPrompts from './prompts/ct-names-prompts';\nimport kindPrompts from './prompts/kind-prompts';\nimport getAttributesPrompts from './prompts/get-attributes-prompts';\nimport bootstrapApiPrompts from './prompts/bootstrap-api-prompts';\nimport { appendToFile } from './utils/extend-plugin-index-files';\n\nexport default (plop: NodePlopAPI) => {\n // Model generator\n plop.setGenerator('content-type', {\n description: 'Generate a content type for an API',\n async prompts(inquirer) {\n const config = await inquirer.prompt([...ctNamesPrompts, ...kindPrompts]);\n const attributes = await getAttributesPrompts(inquirer);\n\n const api = await inquirer.prompt([\n ...getDestinationPrompts('model', plop.getDestBasePath()),\n {\n when: (answers) => answers.destination === 'new',\n type: 'input',\n name: 'id',\n default: config.singularName,\n message: 'Name of the new API?',\n async validate(input) {\n if (!strings.isKebabCase(input)) {\n return 'Value must be in kebab-case';\n }\n\n const apiPath = join(plop.getDestBasePath(), 'api');\n const exists = await fs.pathExists(apiPath);\n\n if (!exists) {\n return true;\n }\n\n const apiDir = await fs.readdir(apiPath, { withFileTypes: true });\n const apiDirContent = apiDir.filter((fd) => fd.isDirectory());\n\n if (apiDirContent.findIndex((dir) => dir.name === input) !== -1) {\n throw new Error('This name is already taken.');\n }\n\n return true;\n },\n },\n ...bootstrapApiPrompts,\n ]);\n\n return {\n ...config,\n ...api,\n attributes,\n };\n },\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const attributes = answers.attributes.reduce((object: any, answer: any) => {\n const val: any = { type: answer.attributeType };\n\n if (answer.attributeType === 'enumeration') {\n val.enum = answer.enum.split(',').map((item: string) => item.trim());\n }\n\n if (answer.attributeType === 'media') {\n val.allowedTypes = ['images', 'files', 'videos', 'audios'];\n val.multiple = answer.multiple;\n }\n\n return Object.assign(object, { [answer.attributeName]: val }, {});\n }, {});\n\n const filePath = getFilePath(answers.destination);\n const currentDir = process.cwd();\n let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n if (answers.plugin) {\n // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.\n const pluginServerDir = join(\n currentDir,\n 'src',\n filePath.replace('{{ plugin }}', answers.plugin),\n '../'\n );\n language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';\n }\n\n const baseActions: Array<ActionType> = [\n {\n type: 'add',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n templateFile: `templates/${language}/content-type.schema.json.hbs`,\n data: {\n collectionName: slugify(answers.pluralName, { separator: '_' }),\n },\n },\n ];\n\n if (Object.entries(attributes).length > 0) {\n baseActions.push({\n type: 'modify',\n path: `${filePath}/content-types/{{ singularName }}/schema.json`,\n transform(template: string) {\n const parsedTemplate = JSON.parse(template);\n parsedTemplate.attributes = attributes;\n return JSON.stringify(parsedTemplate, null, 2);\n },\n });\n }\n\n if (answers.plugin) {\n const indexPath = join(\n plop.getDestBasePath(),\n `${filePath}/content-types/index.${language}`\n );\n const exists = fs.existsSync(indexPath);\n\n if (!exists) {\n // Create index file if it doesn't exist\n baseActions.push({\n type: 'add',\n path: `${filePath}/content-types/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n // Append the new content type to the index.ts file\n baseActions.push({\n type: 'modify',\n path: `${filePath}/content-types/index.${language}`,\n transform(template: string) {\n return appendToFile(template, {\n type: 'content-type',\n singularName: answers.singularName,\n });\n },\n });\n }\n\n if (answers.bootstrapApi) {\n const { singularName } = answers;\n\n let uid;\n if (answers.destination === 'new') {\n uid = `api::${answers.id}.${singularName}`;\n } else if (answers.api) {\n uid = `api::${answers.api}.${singularName}`;\n } else if (answers.plugin) {\n uid = `plugin::${answers.plugin}.${singularName}`;\n }\n\n baseActions.push(\n {\n type: 'add',\n path: `${filePath}/controllers/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-controller.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/services/{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-service.${language}.hbs`,\n data: { uid },\n },\n {\n type: 'add',\n path: `${filePath}/routes/${answers.plugin ? 'content-api/' : ''}{{ singularName }}.${language}`,\n templateFile: `templates/${language}/core-router.${language}.hbs`,\n data: { uid },\n }\n );\n\n if (answers.plugin) {\n const indexFiles = ['controllers', 'services', 'routes'];\n\n indexFiles.forEach((type) => {\n const indexPath = join(plop.getDestBasePath(), `${filePath}/${type}/index.${language}`);\n const exists = fs.existsSync(indexPath);\n\n if (!exists && type !== 'routes') {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n if (type === 'routes') {\n const indexPath = join(\n plop.getDestBasePath(),\n `${filePath}/${type}/index.${language}`\n );\n const exists = fs.existsSync(indexPath);\n\n if (!exists) {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.routes.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n const routeIndexFiles = ['content-api', 'admin'];\n\n routeIndexFiles.forEach((routeType) => {\n const routeTypeIndexPath = join(\n plop.getDestBasePath(),\n `${filePath}/${type}/${routeType}/index.${language}`\n );\n const routeTypeExists = fs.existsSync(routeTypeIndexPath);\n\n if (!routeTypeExists) {\n baseActions.push({\n type: 'add',\n path: `${filePath}/${type}/${routeType}/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.routes.type.index.${language}.hbs`,\n data: { type: routeType },\n skipIfExists: true,\n });\n }\n });\n }\n\n baseActions.push({\n type: 'modify',\n path: `${filePath}/${type}/${type === 'routes' ? 'content-api/' : ''}index.${language}`,\n transform(template: string) {\n if (type === 'routes') {\n return appendToFile(template, {\n type: 'routes',\n singularName: answers.singularName,\n });\n }\n\n return appendToFile(template, {\n type: 'index',\n singularName: answers.singularName,\n });\n },\n });\n });\n }\n }\n\n return baseActions;\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","inquirer","config","prompt","ctNamesPrompts","kindPrompts","attributes","getAttributesPrompts","api","getDestinationPrompts","getDestBasePath","when","answers","destination","type","name","default","singularName","message","validate","input","strings","isKebabCase","apiPath","join","exists","fs","pathExists","apiDir","readdir","withFileTypes","apiDirContent","filter","fd","isDirectory","findIndex","dir","Error","bootstrapApiPrompts","actions","reduce","object","answer","val","attributeType","enum","split","map","item","trim","allowedTypes","multiple","Object","assign","attributeName","filePath","getFilePath","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","plugin","pluginServerDir","replace","baseActions","path","templateFile","data","collectionName","slugify","pluralName","separator","entries","length","push","transform","template","parsedTemplate","JSON","parse","stringify","indexPath","existsSync","skipIfExists","appendToFile","bootstrapApi","uid","id","indexFiles","forEach","routeIndexFiles","routeType","routeTypeIndexPath","routeTypeExists"],"mappings":";;;;;;;;;;;;;AAeA,0BAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,cAAgB,EAAA;QAChCC,WAAa,EAAA,oCAAA;AACb,QAAA,MAAMC,SAAQC,QAAQ,EAAA;AACpB,YAAA,MAAMC,MAAS,GAAA,MAAMD,QAASE,CAAAA,MAAM,CAAC;AAAIC,gBAAAA,GAAAA,SAAAA;AAAmBC,gBAAAA,GAAAA;AAAY,aAAA,CAAA;YACxE,MAAMC,UAAAA,GAAa,MAAMC,oBAAqBN,CAAAA,QAAAA,CAAAA;AAE9C,YAAA,MAAMO,GAAM,GAAA,MAAMP,QAASE,CAAAA,MAAM,CAAC;mBAC7BM,qBAAsB,CAAA,OAAA,EAASZ,KAAKa,eAAe,EAAA,CAAA;AACtD,gBAAA;AACEC,oBAAAA,IAAAA,EAAM,CAACC,OAAAA,GAAYA,OAAQC,CAAAA,WAAW,KAAK,KAAA;oBAC3CC,IAAM,EAAA,OAAA;oBACNC,IAAM,EAAA,IAAA;AACNC,oBAAAA,OAAAA,EAASd,OAAOe,YAAY;oBAC5BC,OAAS,EAAA,sBAAA;AACT,oBAAA,MAAMC,UAASC,KAAK,EAAA;AAClB,wBAAA,IAAI,CAACC,OAAAA,CAAQC,WAAW,CAACF,KAAQ,CAAA,EAAA;4BAC/B,OAAO,6BAAA;AACT;AAEA,wBAAA,MAAMG,OAAUC,GAAAA,IAAAA,CAAK3B,IAAKa,CAAAA,eAAe,EAAI,EAAA,KAAA,CAAA;A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AAK,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;+BAOlB,WAAW;AAAjC,wBA+BE"}
1
+ {"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/plops/controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,WAAW,EAAE,MAAM,MAAM,CAAC;+BAU9B,WAAW;AAAjC,wBAoEE"}
@@ -1,9 +1,12 @@
1
1
  'use strict';
2
2
 
3
3
  var tsUtils = require('@strapi/typescript-utils');
4
+ var path = require('path');
5
+ var fs = require('fs');
4
6
  var getDestinationPrompts = require('./prompts/get-destination-prompts.js');
5
7
  var getFilePath = require('./utils/get-file-path.js');
6
8
  var validateInput = require('./utils/validate-input.js');
9
+ var extendPluginIndexFiles = require('./utils/extend-plugin-index-files.js');
7
10
 
8
11
  var generateController = ((plop)=>{
9
12
  // Controller generator
@@ -24,14 +27,44 @@ var generateController = ((plop)=>{
24
27
  }
25
28
  const filePath = getFilePath(answers.destination);
26
29
  const currentDir = process.cwd();
27
- const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
28
- return [
30
+ let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';
31
+ if (answers.plugin) {
32
+ // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.
33
+ const pluginServerDir = path.join(currentDir, 'src', filePath.replace('{{ plugin }}', answers.plugin), '../');
34
+ language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';
35
+ }
36
+ const baseActions = [
29
37
  {
30
38
  type: 'add',
31
39
  path: `${filePath}/controllers/{{ id }}.${language}`,
32
40
  templateFile: `templates/${language}/controller.${language}.hbs`
33
41
  }
34
42
  ];
43
+ if (answers.plugin) {
44
+ const indexPath = path.join(plop.getDestBasePath(), `${filePath}/controllers/index.${language}`);
45
+ const exists = fs.existsSync(indexPath);
46
+ if (!exists) {
47
+ // Create index file if it doesn't exist
48
+ baseActions.push({
49
+ type: 'add',
50
+ path: `${filePath}/controllers/index.${language}`,
51
+ templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,
52
+ skipIfExists: true
53
+ });
54
+ }
55
+ // Append the new controller to the index.ts file
56
+ baseActions.push({
57
+ type: 'modify',
58
+ path: `${filePath}/controllers/index.${language}`,
59
+ transform (template) {
60
+ return extendPluginIndexFiles.appendToFile(template, {
61
+ type: 'index',
62
+ singularName: answers.id
63
+ });
64
+ }
65
+ });
66
+ }
67
+ return baseActions;
35
68
  }
36
69
  });
37
70
  });
@@ -1 +1 @@
1
- {"version":3,"file":"controller.js","sources":["../../src/plops/controller.ts"],"sourcesContent":["import type { NodePlopAPI } from 'plop';\nimport tsUtils from '@strapi/typescript-utils';\n\nimport getDestinationPrompts from './prompts/get-destination-prompts';\nimport getFilePath from './utils/get-file-path';\nimport validateInput from './utils/validate-input';\n\nexport default (plop: NodePlopAPI) => {\n // Controller generator\n plop.setGenerator('controller', {\n description: 'Generate a controller for an API',\n prompts: [\n {\n type: 'input',\n name: 'id',\n message: 'Controller name',\n validate: (input) => validateInput(input),\n },\n ...getDestinationPrompts('controller', plop.getDestBasePath()),\n ],\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const filePath = getFilePath(answers.destination);\n const currentDir = process.cwd();\n const language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n return [\n {\n type: 'add',\n path: `${filePath}/controllers/{{ id }}.${language}`,\n templateFile: `templates/${language}/controller.${language}.hbs`,\n },\n ];\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","type","name","message","validate","input","validateInput","getDestinationPrompts","getDestBasePath","actions","answers","filePath","getFilePath","destination","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","path","templateFile"],"mappings":";;;;;;;AAOA,yBAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,YAAc,EAAA;QAC9BC,WAAa,EAAA,kCAAA;QACbC,OAAS,EAAA;AACP,YAAA;gBACEC,IAAM,EAAA,OAAA;gBACNC,IAAM,EAAA,IAAA;gBACNC,OAAS,EAAA,iBAAA;gBACTC,QAAU,EAAA,CAACC,QAAUC,aAAcD,CAAAA,KAAAA;AACrC,aAAA;eACGE,qBAAsB,CAAA,YAAA,EAAcV,KAAKW,eAAe,EAAA;AAC5D,SAAA;AACDC,QAAAA,OAAAA,CAAAA,CAAQC,OAAO,EAAA;AACb,YAAA,IAAI,CAACA,OAAS,EAAA;AACZ,gBAAA,OAAO,EAAE;AACX;YAEA,MAAMC,QAAAA,GAAWC,WAAYF,CAAAA,OAAAA,CAAQG,WAAW,CAAA;YAChD,MAAMC,UAAAA,GAAaC,QAAQC,GAAG,EAAA;AAC9B,YAAA,MAAMC,QAAWC,GAAAA,OAAAA,CAAQC,qBAAqB,CAACL,cAAc,IAAO,GAAA,IAAA;YAEpE,OAAO;AACL,gBAAA;oBACEb,IAAM,EAAA,KAAA;AACNmB,oBAAAA,IAAAA,EAAM,CAAC,EAAET,QAAAA,CAAS,sBAAsB,EAAEM,SAAS,CAAC;oBACpDI,YAAc,EAAA,CAAC,UAAU,EAAEJ,QAAAA,CAAS,YAAY,EAAEA,QAAAA,CAAS,IAAI;AACjE;AACD,aAAA;AACH;AACF,KAAA,CAAA;AACF,CAAA;;;;"}
1
+ {"version":3,"file":"controller.js","sources":["../../src/plops/controller.ts"],"sourcesContent":["import type { ActionType, NodePlopAPI } from 'plop';\nimport tsUtils from '@strapi/typescript-utils';\nimport { join } from 'path';\nimport fs from 'fs';\n\nimport getDestinationPrompts from './prompts/get-destination-prompts';\nimport getFilePath from './utils/get-file-path';\nimport validateInput from './utils/validate-input';\nimport { appendToFile } from './utils/extend-plugin-index-files';\n\nexport default (plop: NodePlopAPI) => {\n // Controller generator\n plop.setGenerator('controller', {\n description: 'Generate a controller for an API',\n prompts: [\n {\n type: 'input',\n name: 'id',\n message: 'Controller name',\n validate: (input) => validateInput(input),\n },\n ...getDestinationPrompts('controller', plop.getDestBasePath()),\n ],\n actions(answers) {\n if (!answers) {\n return [];\n }\n\n const filePath = getFilePath(answers.destination);\n const currentDir = process.cwd();\n let language = tsUtils.isUsingTypeScriptSync(currentDir) ? 'ts' : 'js';\n\n if (answers.plugin) {\n // The tsconfig in plugins is located just outside the server src, not in the root of the plugin.\n const pluginServerDir = join(\n currentDir,\n 'src',\n filePath.replace('{{ plugin }}', answers.plugin),\n '../'\n );\n language = tsUtils.isUsingTypeScriptSync(pluginServerDir) ? 'ts' : 'js';\n }\n\n const baseActions: Array<ActionType> = [\n {\n type: 'add',\n path: `${filePath}/controllers/{{ id }}.${language}`,\n templateFile: `templates/${language}/controller.${language}.hbs`,\n },\n ];\n\n if (answers.plugin) {\n const indexPath = join(plop.getDestBasePath(), `${filePath}/controllers/index.${language}`);\n const exists = fs.existsSync(indexPath);\n\n if (!exists) {\n // Create index file if it doesn't exist\n baseActions.push({\n type: 'add',\n path: `${filePath}/controllers/index.${language}`,\n templateFile: `templates/${language}/plugin/plugin.index.${language}.hbs`,\n skipIfExists: true,\n });\n }\n\n // Append the new controller to the index.ts file\n baseActions.push({\n type: 'modify',\n path: `${filePath}/controllers/index.${language}`,\n transform(template: string) {\n return appendToFile(template, { type: 'index', singularName: answers.id });\n },\n });\n }\n\n return baseActions;\n },\n });\n};\n"],"names":["plop","setGenerator","description","prompts","type","name","message","validate","input","validateInput","getDestinationPrompts","getDestBasePath","actions","answers","filePath","getFilePath","destination","currentDir","process","cwd","language","tsUtils","isUsingTypeScriptSync","plugin","pluginServerDir","join","replace","baseActions","path","templateFile","indexPath","exists","fs","existsSync","push","skipIfExists","transform","template","appendToFile","singularName","id"],"mappings":";;;;;;;;;;AAUA,yBAAe,CAAA,CAACA,IAAAA,GAAAA;;IAEdA,IAAKC,CAAAA,YAAY,CAAC,YAAc,EAAA;QAC9BC,WAAa,EAAA,kCAAA;QACbC,OAAS,EAAA;AACP,YAAA;gBACEC,IAAM,EAAA,OAAA;gBACNC,IAAM,EAAA,IAAA;gBACNC,OAAS,EAAA,iBAAA;gB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