| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | const pluralize = require('pluralize'); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:00:48 +01:00
										 |  |  | const { nameToSlug } = require('strapi-utils'); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This `before` function is run before generating targets. | 
					
						
							|  |  |  |  * Validate, configure defaults, get extra dependencies, etc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param {Object} scope | 
					
						
							|  |  |  |  * @param {Function} cb | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-11 13:03:35 +02:00
										 |  |  | module.exports = (scope, cb) => { | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   if (!scope.rootPath || !scope.id) { | 
					
						
							| 
									
										
										
										
											2016-03-25 22:22:34 +01:00
										 |  |  |     return cb.invalid('Usage: `$ strapi generate:api apiName`'); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-09 14:26:47 +01:00
										 |  |  |   // Format `id`.
 | 
					
						
							| 
									
										
										
										
											2020-03-20 19:00:48 +01:00
										 |  |  |   const name = scope.name || nameToSlug(scope.id); | 
					
						
							| 
									
										
										
										
											2017-03-09 14:26:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-22 11:29:37 +01:00
										 |  |  |   scope.contentTypeKind = scope.args.kind || 'collectionType'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   // `scope.args` are the raw command line arguments.
 | 
					
						
							|  |  |  |   _.defaults(scope, { | 
					
						
							| 
									
										
										
										
											2019-10-31 14:34:30 +01:00
										 |  |  |     name, | 
					
						
							| 
									
										
										
										
											2020-01-22 11:29:37 +01:00
										 |  |  |     route: | 
					
						
							|  |  |  |       scope.contentTypeKind === 'singleType' | 
					
						
							|  |  |  |         ? _.kebabCase(scope.id) | 
					
						
							|  |  |  |         : _.kebabCase(pluralize(scope.id)), | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 14:34:30 +01:00
										 |  |  |   let filePath; | 
					
						
							|  |  |  |   if (scope.args.api) { | 
					
						
							|  |  |  |     filePath = `./api/${scope.args.api}`; | 
					
						
							|  |  |  |   } else if (scope.args.plugin) { | 
					
						
							|  |  |  |     filePath = `./plugins/${scope.args.plugin}`; | 
					
						
							|  |  |  |   } else if (scope.args.extend) { | 
					
						
							|  |  |  |     filePath = `./extensions/${scope.args.extend}`; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     filePath = `./api/${name}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   // Take another pass to take advantage of the defaults absorbed in previous passes.
 | 
					
						
							|  |  |  |   _.defaults(scope, { | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |     filename: `${name}.js`, | 
					
						
							|  |  |  |     filenameSettings: `${name}.settings.json`, | 
					
						
							| 
									
										
										
										
											2019-10-31 14:34:30 +01:00
										 |  |  |     filePath, | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   // Validate optional attribute arguments.
 | 
					
						
							|  |  |  |   const invalidAttributes = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |   if (_.isPlainObject(scope.args.attributes)) { | 
					
						
							|  |  |  |     scope.attributes = scope.args.attributes; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     // Map attributes and split them for CLI.
 | 
					
						
							|  |  |  |     scope.attributes = scope.args.attributes.map(attribute => { | 
					
						
							|  |  |  |       if (_.isString(attribute)) { | 
					
						
							|  |  |  |         const parts = attribute.split(':'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parts[1] = parts[1] || 'string'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Handle invalid attributes.
 | 
					
						
							|  |  |  |         if (!parts[1] || !parts[0]) { | 
					
						
							| 
									
										
										
										
											2020-03-19 18:35:08 +01:00
										 |  |  |           invalidAttributes.push('Error: Invalid attribute notation `' + attribute + '`.'); | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           name: _.trim(_.deburr(parts[0].toLowerCase())), | 
					
						
							|  |  |  |           params: { | 
					
						
							|  |  |  |             type: _.trim(_.deburr(parts[1].toLowerCase())), | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return _.has(attribute, 'params.type') ? attribute : undefined; | 
					
						
							| 
									
										
										
										
											2017-08-04 18:22:18 +02:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-04 18:22:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |     scope.attributes = _.compact(scope.attributes); | 
					
						
							| 
									
										
										
										
											2018-05-04 17:39:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |     // Handle invalid action arguments.
 | 
					
						
							|  |  |  |     // Send back invalidActions.
 | 
					
						
							|  |  |  |     if (invalidAttributes.length) { | 
					
						
							|  |  |  |       return cb.invalid(invalidAttributes); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |     // Make sure there aren't duplicates.
 | 
					
						
							|  |  |  |     if ( | 
					
						
							|  |  |  |       _(scope.attributes.map(attribute => attribute.name)) | 
					
						
							|  |  |  |         .uniq() | 
					
						
							|  |  |  |         .valueOf().length !== scope.attributes.length | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |       return cb.invalid('Duplicate attributes not allowed!'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |     // Render some stringified code from the action template
 | 
					
						
							|  |  |  |     // and make it available in our scope for use later on.
 | 
					
						
							|  |  |  |     scope.attributes = scope.attributes.reduce((acc, attribute) => { | 
					
						
							|  |  |  |       acc[attribute.name] = attribute.params; | 
					
						
							|  |  |  |       return acc; | 
					
						
							|  |  |  |     }, {}); | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-08-11 18:23:19 +02:00
										 |  |  |   // Set collectionName
 | 
					
						
							| 
									
										
										
										
											2019-07-09 23:51:21 +02:00
										 |  |  |   scope.collectionName = _.has(scope.args, 'collectionName') | 
					
						
							|  |  |  |     ? scope.args.collectionName | 
					
						
							| 
									
										
										
										
											2019-10-31 14:34:30 +01:00
										 |  |  |     : _.snakeCase(pluralize(name)); | 
					
						
							| 
									
										
										
										
											2017-08-11 18:23:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 17:19:37 +02:00
										 |  |  |   // Set description
 | 
					
						
							| 
									
										
										
										
											2020-03-19 18:35:08 +01:00
										 |  |  |   scope.description = _.has(scope.args, 'description') ? scope.args.description : ''; | 
					
						
							| 
									
										
										
										
											2017-08-14 17:19:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:31:44 +02:00
										 |  |  |   // Set connection
 | 
					
						
							|  |  |  |   scope.connection = _.get(scope.args, 'connection', undefined); | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |   scope.schema = JSON.stringify( | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       connection: scope.connection, | 
					
						
							|  |  |  |       collectionName: scope.collectionName, | 
					
						
							|  |  |  |       info: { | 
					
						
							| 
									
										
										
										
											2019-10-31 14:34:30 +01:00
										 |  |  |         name: scope.args.displayName || scope.id, | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |         description: scope.description, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       options: { | 
					
						
							| 
									
										
										
										
											2020-07-28 12:14:52 +02:00
										 |  |  |         draftAndPublish: scope.args.draftAndPublish === 'true', | 
					
						
							| 
									
										
										
										
											2019-10-29 18:33:38 +01:00
										 |  |  |         increments: true, | 
					
						
							|  |  |  |         timestamps: true, | 
					
						
							|  |  |  |         comment: '', | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       attributes: scope.attributes, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     null, | 
					
						
							|  |  |  |     2 | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 11:12:50 +01:00
										 |  |  |   // Trigger callback with no error to proceed.
 | 
					
						
							|  |  |  |   return cb.success(); | 
					
						
							|  |  |  | }; |