Fix spaces in attribute names

This commit is contained in:
Maxwell Simmer 2019-07-09 23:51:21 +02:00
parent abef483bc4
commit b04177e37a

View File

@ -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 */
/** /**
* This `before` function is run before generating targets. * This `before` function is run before generating targets.
@ -38,39 +41,41 @@ module.exports = (scope, cb) => {
_.defaults(scope, { _.defaults(scope, {
idPluralized: pluralize.plural(_.trim(_.camelCase(scope.id))), idPluralized: pluralize.plural(_.trim(_.camelCase(scope.id))),
parentId: _.isEmpty(parent) ? undefined : _.trim(_.deburr(parent)), parentId: _.isEmpty(parent) ? undefined : _.trim(_.deburr(parent)),
parentIdPluralized: _.isEmpty(scope.parentId) ? undefined : pluralize.plural(_.trim(_.camelCase(scope.parentId))), parentIdPluralized: _.isEmpty(scope.parentId)
environment: process.env.NODE_ENV || 'development' ? undefined
: pluralize.plural(_.trim(_.camelCase(scope.parentId))),
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',
}); });
// Take another pass to take advantage of the defaults absorbed in previous passes. // Take another pass to take advantage of the defaults absorbed in previous passes.
_.defaults(scope, { _.defaults(scope, {
rootPath: scope.rootPath, rootPath: scope.rootPath,
filename: `${scope.globalID}${scope.ext}`, filename: `${scope.globalID}${scope.ext}`,
filenameSettings: scope.globalID + '.settings.json', filenameSettings: scope.globalID + '.settings.json',
folderPrefix: !scope.args.api && scope.args.plugin ? 'plugins' : 'api', folderPrefix: !scope.args.api && scope.args.plugin ? 'plugins' : 'api',
folderName: _.camelCase(scope.parentId || scope.id).toLowerCase() folderName: _.camelCase(scope.parentId || scope.id).toLowerCase(),
}); });
// Humanize output. // Humanize output.
_.defaults(scope, { _.defaults(scope, {
humanizeId: _.camelCase(scope.id).toLowerCase(), humanizeId: _.camelCase(scope.id).toLowerCase(),
humanizeIdPluralized: pluralize.plural(_.camelCase(scope.id).toLowerCase()), humanizeIdPluralized: pluralize.plural(_.camelCase(scope.id).toLowerCase()),
humanizedPath: `\`./${scope.folderPrefix}/${scope.parentId ? '' + scope.folderName : ''}\`` humanizedPath: `\`./${scope.folderPrefix}/${
scope.parentId ? '' + scope.folderName : ''
}\``,
}); });
// Validate optional attribute arguments. // Validate optional attribute arguments.
const invalidAttributes = []; const invalidAttributes = [];
// Map attributes and split them for CLI. // Map attributes and split them for CLI.
scope.attributes = scope.args.attributes.map((attribute) => { scope.attributes = scope.args.attributes.map(attribute => {
if (_.isString(attribute)) { if (_.isString(attribute)) {
const parts = attribute.split(':'); const parts = attribute.split(':');
@ -78,15 +83,17 @@ module.exports = (scope, cb) => {
// 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(_.lowerCase(parts[0]).toLowerCase())), name: _.trim(_.deburr(parts[0].toLowerCase())),
params: { params: {
type: _.trim(_.deburr(_.lowerCase(parts[1]).toLowerCase())) type: _.trim(_.deburr(parts[1].toLowerCase())),
} },
}; };
} else { } else {
return _.has(attribute, 'params.type') ? attribute : undefined; return _.has(attribute, 'params.type') ? attribute : undefined;
@ -102,29 +109,56 @@ 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(
params: attribute.params _.unescape(
}))); compiled({
}).join(',\n'); name: attribute.name,
params: attribute.params,
})
)
);
})
.join(',\n');
// 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;
// Get default connection // Get default connection
try { try {
scope.connection = _.get(scope.args, 'connection') || JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'database.json'))).defaultConnection || ''; scope.connection =
_.get(scope.args, '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);
} }