169 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-03-18 11:12:50 +01:00
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const fs = require('fs');
const path = require('path');
// Public node modules.
const _ = require('lodash');
const pluralize = require('pluralize');
// Fetch stub attribute template on initial load.
2019-07-09 23:51:21 +02:00
const attributeTemplate = fs.readFileSync(
path.resolve(__dirname, '..', 'templates', 'attribute.template'),
'utf8'
);
2018-05-04 17:39:37 +02:00
/* eslint-disable prefer-template */
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
*/
module.exports = (scope, cb) => {
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
}
// Check `api` and `plugin` parameters
const parent = scope.args.api || scope.args.plugin;
2017-03-09 14:26:47 +01:00
// Format `id`.
scope.id = _.trim(_.camelCase(scope.id));
2016-03-18 11:12:50 +01:00
// `scope.args` are the raw command line arguments.
_.defaults(scope, {
2017-03-09 14:26:47 +01:00
idPluralized: pluralize.plural(_.trim(_.camelCase(scope.id))),
parentId: _.isEmpty(parent) ? undefined : _.trim(_.deburr(parent)),
2019-07-09 23:51:21 +02:00
parentIdPluralized: _.isEmpty(scope.parentId)
? undefined
: pluralize.plural(_.trim(_.camelCase(scope.parentId))),
environment: process.env.NODE_ENV || 'development',
2016-03-18 11:12:50 +01:00
});
// Determine default values based on the available scope.
_.defaults(scope, {
globalID: _.upperFirst(_.camelCase(scope.id)),
2019-07-09 23:51:21 +02:00
ext: '.js',
2016-03-18 11:12:50 +01:00
});
// Take another pass to take advantage of the defaults absorbed in previous passes.
_.defaults(scope, {
rootPath: scope.rootPath,
filename: `${scope.globalID}${scope.ext}`,
filenameSettings: scope.globalID + '.settings.json',
folderPrefix: !scope.args.api && scope.args.plugin ? 'plugins' : 'api',
2019-07-09 23:51:21 +02:00
folderName: _.camelCase(scope.parentId || scope.id).toLowerCase(),
2016-03-18 11:12:50 +01:00
});
// Humanize output.
_.defaults(scope, {
humanizeId: _.camelCase(scope.id).toLowerCase(),
humanizeIdPluralized: pluralize.plural(_.camelCase(scope.id).toLowerCase()),
2019-07-09 23:51:21 +02:00
humanizedPath: `\`./${scope.folderPrefix}/${
scope.parentId ? '' + scope.folderName : ''
}\``,
2016-03-18 11:12:50 +01:00
});
// Validate optional attribute arguments.
const invalidAttributes = [];
2017-08-04 18:22:18 +02:00
// Map attributes and split them for CLI.
2019-07-09 23:51:21 +02:00
scope.attributes = scope.args.attributes.map(attribute => {
2017-08-04 18:22:18 +02:00
if (_.isString(attribute)) {
const parts = attribute.split(':');
parts[1] = parts[1] || 'string';
// Handle invalid attributes.
if (!parts[1] || !parts[0]) {
2019-07-09 23:51:21 +02:00
invalidAttributes.push(
'Error: Invalid attribute notation `' + attribute + '`.'
);
2017-08-04 18:22:18 +02:00
return;
}
return {
2019-07-09 23:51:21 +02:00
name: _.trim(_.deburr(parts[0].toLowerCase())),
2017-08-07 18:05:40 +02:00
params: {
2019-07-09 23:51:21 +02:00
type: _.trim(_.deburr(parts[1].toLowerCase())),
},
2017-08-04 18:22:18 +02:00
};
} else {
return _.has(attribute, 'params.type') ? attribute : undefined;
}
});
2018-05-04 17:39:37 +02:00
scope.attributes = _.compact(scope.attributes);
// Handle invalid action arguments.
// Send back invalidActions.
if (invalidAttributes.length) {
return cb.invalid(invalidAttributes);
}
// Make sure there aren't duplicates.
2019-07-09 23:51:21 +02:00
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.
2019-07-09 23:51:21 +02:00
scope.attributes = scope.attributes
.map(attribute => {
const compiled = _.template(attributeTemplate);
return _.trimEnd(
_.unescape(
compiled({
name: attribute.name,
params: attribute.params,
})
)
);
})
.join(',\n');
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
: undefined;
2017-08-11 18:23:19 +02:00
// Set description
2019-07-09 23:51:21 +02:00
scope.description = _.has(scope.args, 'description')
? scope.args.description
: undefined;
2016-03-18 11:12:50 +01:00
// Get default connection
try {
2019-07-09 23:51:21 +02:00
scope.connection =
_.get(scope.args, 'connection') ||
JSON.parse(
fs.readFileSync(
path.resolve(
scope.rootPath,
'config',
'environments',
scope.environment,
'database.json'
)
)
).defaultConnection ||
'';
2016-03-18 11:12:50 +01:00
} catch (err) {
return cb.invalid(err);
}
// Trigger callback with no error to proceed.
return cb.success();
};