146 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-04-21 14:59:00 +02:00
'use strict';
/**
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
const pluralize = require('pluralize');
const { nameToSlug, nameToCollectionName } = require('strapi-utils');
2018-05-04 17:44:42 +02:00
/* eslint-disable prefer-template */
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
*/
module.exports = (scope, cb) => {
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
}
// Format `id`.
const name = scope.name || nameToSlug(scope.id);
2016-04-21 14:59:00 +02:00
// `scope.args` are the raw command line arguments.
_.defaults(scope, {
name,
environment: process.env.NODE_ENV || 'development',
2016-04-21 14:59:00 +02: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 {
filePath = `./api/${name}/models`;
}
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,
filePath,
filename: `${name}.js`,
filenameSettings: `${name}.settings.json`,
2016-04-21 14:59:00 +02:00
});
// Validate optional attribute arguments.
const invalidAttributes = [];
2016-08-10 11:00:56 +02:00
Fix/#3184/fix server crashs on database change (#5703) * Don't set connection field on create/edit operation on content-types & components Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Make sure that every component has a valid connection attribute Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection check on components load Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove default connections from core & webhook stores but make sure it's defined in the application lifecycle Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix component's associations that can be undefined instead of empty in populateBareAssociations Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove "default" connection from plugins' models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection attribute from generated models (cli) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Mutate each component instead of reassign Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Build core_store and webhook model based on the current config Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add connection to templates conditionally (based on args) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Set default value for description to undefined instead of empty string Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove unnecessary complexity Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update getStarted models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix attributes parsing for generate:model Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removed tpl option from generate:model/api Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove tpl option from cli Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
2020-04-07 16:31:44 +02: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]) {
invalidAttributes.push('Error: Invalid attribute notation `' + attribute + '`.');
return;
}
return {
name: _.trim(_.deburr(parts[0].toLowerCase())),
params: {
type: _.trim(_.deburr(parts[1].toLowerCase())),
},
};
} else {
return _.has(attribute, 'params.type') ? attribute : undefined;
}
});
scope.attributes = _.compact(scope.attributes);
// Handle invalid action arguments.
// Send back invalidActions.
if (invalidAttributes.length) {
return cb.invalid(invalidAttributes);
}
Fix/#3184/fix server crashs on database change (#5703) * Don't set connection field on create/edit operation on content-types & components Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Make sure that every component has a valid connection attribute Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection check on components load Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove default connections from core & webhook stores but make sure it's defined in the application lifecycle Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix component's associations that can be undefined instead of empty in populateBareAssociations Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove "default" connection from plugins' models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection attribute from generated models (cli) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Mutate each component instead of reassign Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Build core_store and webhook model based on the current config Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add connection to templates conditionally (based on args) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Set default value for description to undefined instead of empty string Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove unnecessary complexity Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update getStarted models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix attributes parsing for generate:model Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removed tpl option from generate:model/api Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove tpl option from cli Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
2020-04-07 16:31:44 +02: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!');
2016-08-10 11:00:56 +02:00
}
Fix/#3184/fix server crashs on database change (#5703) * Don't set connection field on create/edit operation on content-types & components Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Make sure that every component has a valid connection attribute Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection check on components load Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove default connections from core & webhook stores but make sure it's defined in the application lifecycle Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix component's associations that can be undefined instead of empty in populateBareAssociations Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove "default" connection from plugins' models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection attribute from generated models (cli) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Mutate each component instead of reassign Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Build core_store and webhook model based on the current config Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add connection to templates conditionally (based on args) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Set default value for description to undefined instead of empty string Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove unnecessary complexity Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update getStarted models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix attributes parsing for generate:model Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removed tpl option from generate:model/api Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove tpl option from cli Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
2020-04-07 16:31:44 +02: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;
}, {});
}
2016-04-21 14:59:00 +02:00
2018-05-14 12:05:09 +02:00
// Set collectionName
scope.collectionName = _.has(scope.args, 'collectionName')
? scope.args.collectionName
: nameToCollectionName(pluralize(scope.id));
2018-05-14 12:05:09 +02:00
// Set description
scope.description = _.has(scope.args, 'description') ? scope.args.description : undefined;
2018-05-14 12:05:09 +02:00
Fix/#3184/fix server crashs on database change (#5703) * Don't set connection field on create/edit operation on content-types & components Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Make sure that every component has a valid connection attribute Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection check on components load Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove default connections from core & webhook stores but make sure it's defined in the application lifecycle Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix component's associations that can be undefined instead of empty in populateBareAssociations Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove "default" connection from plugins' models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection attribute from generated models (cli) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Mutate each component instead of reassign Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Build core_store and webhook model based on the current config Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add connection to templates conditionally (based on args) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Set default value for description to undefined instead of empty string Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove unnecessary complexity Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update getStarted models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix attributes parsing for generate:model Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removed tpl option from generate:model/api Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove tpl option from cli Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
2020-04-07 16:31:44 +02:00
// Set connection
scope.connection = _.get(scope.args, 'connection', undefined);
scope.schema = JSON.stringify(
{
kind: 'collectionType',
connection: scope.connection,
collectionName: scope.collectionName,
info: {
name: scope.id,
description: scope.description,
},
options: {
draftAndPublish: false,
Fix/#3184/fix server crashs on database change (#5703) * Don't set connection field on create/edit operation on content-types & components Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Make sure that every component has a valid connection attribute Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection check on components load Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove default connections from core & webhook stores but make sure it's defined in the application lifecycle Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix component's associations that can be undefined instead of empty in populateBareAssociations Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove "default" connection from plugins' models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove connection attribute from generated models (cli) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Mutate each component instead of reassign Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Build core_store and webhook model based on the current config Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Add connection to templates conditionally (based on args) Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Set default value for description to undefined instead of empty string Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove unnecessary complexity Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Update getStarted models Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Fix attributes parsing for generate:model Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Removed tpl option from generate:model/api Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu> * Remove tpl option from cli Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
2020-04-07 16:31:44 +02:00
timestamps: true,
increments: true,
comment: '',
},
attributes: scope.attributes,
},
null,
2
);
2016-04-21 14:59:00 +02:00
// Trigger callback with no error to proceed.
return cb();
};