| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Module dependencies | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Node.js core.
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Public node modules.
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  | const pluralize = require('pluralize'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Fetch stub attribute template on initial load.
 | 
					
						
							|  |  |  | const attributeTemplate = fs.readFileSync(path.resolve(__dirname, '..', 'templates', 'attribute.template'), 'utf8'); | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02: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) { | 
					
						
							|  |  |  |     return cb.invalid('Usage: `$ strapi generate:model modelName --api apiName --plugin pluginName`'); | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // `scope.args` are the raw command line arguments.
 | 
					
						
							|  |  |  |   _.defaults(scope, { | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |     id: _.trim(_.deburr(scope.id)), | 
					
						
							|  |  |  |     idPluralized: pluralize.plural(_.trim(_.deburr(scope.id))), | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |     environment: process.NODE_ENV || 'development' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Determine default values based on the available scope.
 | 
					
						
							|  |  |  |   _.defaults(scope, { | 
					
						
							| 
									
										
										
										
											2016-08-12 12:04:00 +02:00
										 |  |  |     globalID: _.upperFirst(_.camelCase(scope.id)), | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |     ext: '.js' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   // Determine the destination path.
 | 
					
						
							|  |  |  |   let filePath; | 
					
						
							|  |  |  |   if (scope.args.api) { | 
					
						
							|  |  |  |     filePath = `./api/${scope.args.api}/models`; | 
					
						
							|  |  |  |   } else if (scope.args.plugin) { | 
					
						
							|  |  |  |     filePath = `./plugins/${scope.args.plugin}/models`; | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2017-07-31 12:52:08 +02:00
										 |  |  |     filePath = `./api/${scope.id}`; | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |   // Take another pass to take advantage of the defaults absorbed in previous passes.
 | 
					
						
							|  |  |  |   _.defaults(scope, { | 
					
						
							|  |  |  |     rootPath: scope.rootPath, | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |     filePath, | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |     filename: scope.globalID + scope.ext, | 
					
						
							|  |  |  |     filenameSettings: scope.globalID + '.settings.json' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Humanize output.
 | 
					
						
							|  |  |  |   _.defaults(scope, { | 
					
						
							| 
									
										
										
										
											2016-08-12 12:04:00 +02:00
										 |  |  |     humanizeId: _.camelCase(scope.id).toLowerCase(), | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |     humanizedPath: '`' + scope.filePath + '`' | 
					
						
							| 
									
										
										
										
											2016-08-10 11:00:56 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   // Validate optional attribute arguments.
 | 
					
						
							|  |  |  |   const invalidAttributes = []; | 
					
						
							| 
									
										
										
										
											2016-08-10 11:00:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   // Map attributes and split them.
 | 
					
						
							|  |  |  |   scope.attributes = scope.args.attributes.map((attribute) => { | 
					
						
							|  |  |  |     const parts = attribute.split(':'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     parts[1] = parts[1] ? parts[1] : 'string'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Handle invalid attributes.
 | 
					
						
							|  |  |  |     if (!parts[1] || !parts[0]) { | 
					
						
							|  |  |  |       invalidAttributes.push('Error: Invalid attribute notation `' + attribute + '`.'); | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2016-08-10 11:00:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       name: _.trim(_.deburr(_.camelCase(parts[0]).toLowerCase())), | 
					
						
							|  |  |  |       type: _.trim(_.deburr(_.camelCase(parts[1]).toLowerCase())) | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 01:10:37 +01:00
										 |  |  |   // Handle invalid action arguments.
 | 
					
						
							|  |  |  |   // Send back invalidActions.
 | 
					
						
							|  |  |  |   if (invalidAttributes.length) { | 
					
						
							|  |  |  |     return cb.invalid(invalidAttributes); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 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!'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Render some stringified code from the action template
 | 
					
						
							|  |  |  |   // and make it available in our scope for use later on.
 | 
					
						
							|  |  |  |   scope.attributes = scope.attributes.map((attribute) => { | 
					
						
							|  |  |  |     const compiled = _.template(attributeTemplate); | 
					
						
							|  |  |  |     return _.trimEnd(_.unescape(compiled({ | 
					
						
							|  |  |  |       name: attribute.name, | 
					
						
							|  |  |  |       type: attribute.type | 
					
						
							|  |  |  |     }))); | 
					
						
							|  |  |  |   }).join(',\n'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |   // Get default connection
 | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2017-07-31 12:52:08 +02:00
										 |  |  |     scope.connection = JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'database.json'))).defaultConnection || ''; | 
					
						
							| 
									
										
										
										
											2016-04-21 14:59:00 +02:00
										 |  |  |   } catch (err) { | 
					
						
							|  |  |  |     return cb.invalid(err); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Trigger callback with no error to proceed.
 | 
					
						
							|  |  |  |   return cb(); | 
					
						
							|  |  |  | }; |