mirror of
https://github.com/strapi/strapi.git
synced 2025-09-02 13:23:12 +00:00
Fixed issue with duplicate model folder getting created when generating model with api option
This commit is contained in:
parent
0b822a641b
commit
b70b3397fb
@ -13,7 +13,10 @@ const _ = require('lodash');
|
|||||||
const pluralize = require('pluralize');
|
const pluralize = require('pluralize');
|
||||||
|
|
||||||
// Fetch stub attribute template on initial load.
|
// Fetch stub attribute template on initial load.
|
||||||
const attributeTemplate = fs.readFileSync(path.resolve(__dirname, '..', 'templates', 'attribute.template'), 'utf8');
|
const attributeTemplate = fs.readFileSync(
|
||||||
|
path.resolve(__dirname, '..', 'templates', 'attribute.template'),
|
||||||
|
'utf8'
|
||||||
|
);
|
||||||
/* eslint-disable prefer-template */
|
/* eslint-disable prefer-template */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,26 +29,28 @@ const attributeTemplate = fs.readFileSync(path.resolve(__dirname, '..', 'templat
|
|||||||
|
|
||||||
module.exports = (scope, cb) => {
|
module.exports = (scope, cb) => {
|
||||||
if (!scope.rootPath || !scope.id) {
|
if (!scope.rootPath || !scope.id) {
|
||||||
return cb.invalid('Usage: `$ strapi generate:model modelName --api apiName --plugin pluginName`');
|
return cb.invalid(
|
||||||
|
'Usage: `$ strapi generate:model modelName --api apiName --plugin pluginName`'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// `scope.args` are the raw command line arguments.
|
// `scope.args` are the raw command line arguments.
|
||||||
_.defaults(scope, {
|
_.defaults(scope, {
|
||||||
id: _.trim(_.deburr(scope.id)),
|
id: _.trim(_.deburr(scope.id)),
|
||||||
idPluralized: pluralize.plural(_.trim(_.deburr(scope.id))),
|
idPluralized: pluralize.plural(_.trim(_.deburr(scope.id))),
|
||||||
environment: process.env.NODE_ENV || 'development'
|
environment: process.env.NODE_ENV || 'development',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Determine default values based on the available scope.
|
// Determine default values based on the available scope.
|
||||||
_.defaults(scope, {
|
_.defaults(scope, {
|
||||||
globalID: _.upperFirst(_.camelCase(scope.id)),
|
globalID: _.upperFirst(_.camelCase(scope.id)),
|
||||||
ext: '.js'
|
ext: '.js',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Determine the destination path.
|
// Determine the destination path.
|
||||||
let filePath;
|
let filePath;
|
||||||
if (scope.args.api) {
|
if (scope.args.api) {
|
||||||
filePath = `./api/${scope.args.api}/models`;
|
filePath = `./api/${scope.args.api}`;
|
||||||
} else if (scope.args.plugin) {
|
} else if (scope.args.plugin) {
|
||||||
filePath = `./plugins/${scope.args.plugin}/models`;
|
filePath = `./plugins/${scope.args.plugin}/models`;
|
||||||
} else {
|
} else {
|
||||||
@ -57,41 +62,47 @@ module.exports = (scope, cb) => {
|
|||||||
rootPath: scope.rootPath,
|
rootPath: scope.rootPath,
|
||||||
filePath,
|
filePath,
|
||||||
filename: scope.globalID + scope.ext,
|
filename: scope.globalID + scope.ext,
|
||||||
filenameSettings: scope.globalID + '.settings.json'
|
filenameSettings: scope.globalID + '.settings.json',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Humanize output.
|
// Humanize output.
|
||||||
_.defaults(scope, {
|
_.defaults(scope, {
|
||||||
humanizeId: _.camelCase(scope.id).toLowerCase(),
|
humanizeId: _.camelCase(scope.id).toLowerCase(),
|
||||||
humanizedPath: '`' + scope.filePath + '`'
|
humanizedPath: '`' + scope.filePath + '`',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validate optional attribute arguments.
|
// Validate optional attribute arguments.
|
||||||
const invalidAttributes = [];
|
const invalidAttributes = [];
|
||||||
|
|
||||||
// Map attributes and split them.
|
// Map attributes and split them.
|
||||||
scope.attributes = scope.args.attributes.map((attribute) => {
|
scope.attributes = scope.args.attributes.map(attribute => {
|
||||||
const parts = attribute.split(':');
|
const parts = attribute.split(':');
|
||||||
|
|
||||||
parts[1] = parts[1] ? parts[1] : 'string';
|
parts[1] = parts[1] ? parts[1] : 'string';
|
||||||
|
|
||||||
// Handle invalid attributes.
|
// Handle invalid attributes.
|
||||||
if (!parts[1] || !parts[0]) {
|
if (!parts[1] || !parts[0]) {
|
||||||
invalidAttributes.push('Error: Invalid attribute notation `' + attribute + '`.');
|
invalidAttributes.push(
|
||||||
|
'Error: Invalid attribute notation `' + attribute + '`.'
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: _.trim(_.deburr(_.camelCase(parts[0]).toLowerCase())),
|
name: _.trim(_.deburr(_.camelCase(parts[0]).toLowerCase())),
|
||||||
type: _.trim(_.deburr(_.camelCase(parts[1]).toLowerCase()))
|
type: _.trim(_.deburr(_.camelCase(parts[1]).toLowerCase())),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set collectionName
|
// Set collectionName
|
||||||
scope.collectionName = _.has(scope.args, 'collectionName') ? scope.args.collectionName : undefined;
|
scope.collectionName = _.has(scope.args, 'collectionName')
|
||||||
|
? scope.args.collectionName
|
||||||
|
: undefined;
|
||||||
|
|
||||||
// Set description
|
// Set description
|
||||||
scope.description = _.has(scope.args, 'description') ? scope.args.description : undefined;
|
scope.description = _.has(scope.args, 'description')
|
||||||
|
? scope.args.description
|
||||||
|
: undefined;
|
||||||
|
|
||||||
// Handle invalid action arguments.
|
// Handle invalid action arguments.
|
||||||
// Send back invalidActions.
|
// Send back invalidActions.
|
||||||
@ -100,23 +111,44 @@ module.exports = (scope, cb) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make sure there aren't duplicates.
|
// Make sure there aren't duplicates.
|
||||||
if (_(scope.attributes.map(attribute => (attribute.name))).uniq().valueOf().length !== scope.attributes.length) {
|
if (
|
||||||
|
_(scope.attributes.map(attribute => attribute.name))
|
||||||
|
.uniq()
|
||||||
|
.valueOf().length !== scope.attributes.length
|
||||||
|
) {
|
||||||
return cb.invalid('Duplicate attributes not allowed!');
|
return cb.invalid('Duplicate attributes not allowed!');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render some stringified code from the action template
|
// Render some stringified code from the action template
|
||||||
// and make it available in our scope for use later on.
|
// and make it available in our scope for use later on.
|
||||||
scope.attributes = scope.attributes.map((attribute) => {
|
scope.attributes = scope.attributes
|
||||||
const compiled = _.template(attributeTemplate);
|
.map(attribute => {
|
||||||
return _.trimEnd(_.unescape(compiled({
|
const compiled = _.template(attributeTemplate);
|
||||||
name: attribute.name,
|
return _.trimEnd(
|
||||||
type: attribute.type
|
_.unescape(
|
||||||
})));
|
compiled({
|
||||||
}).join(',\n');
|
name: attribute.name,
|
||||||
|
type: attribute.type,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.join(',\n');
|
||||||
|
|
||||||
// Get default connection
|
// Get default connection
|
||||||
try {
|
try {
|
||||||
scope.connection = JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'database.json'))).defaultConnection || '';
|
scope.connection =
|
||||||
|
JSON.parse(
|
||||||
|
fs.readFileSync(
|
||||||
|
path.resolve(
|
||||||
|
scope.rootPath,
|
||||||
|
'config',
|
||||||
|
'environments',
|
||||||
|
scope.environment,
|
||||||
|
'database.json'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).defaultConnection || '';
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return cb.invalid(err);
|
return cb.invalid(err);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user