2017-02-14 01:18:07 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
2017-06-08 17:16:20 +01:00
|
|
|
const path = require('path');
|
2018-05-04 17:52:04 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const _ = require('lodash');
|
2017-02-14 01:18:07 +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) {
|
|
|
|
return cb.invalid('Usage: `$ strapi generate:plugin pluginName`');
|
|
|
|
}
|
|
|
|
|
2020-03-19 18:35:08 +01:00
|
|
|
// Format `id`.
|
|
|
|
const name = scope.name || _.trim(_.camelCase(scope.id));
|
2017-02-14 01:18:07 +01:00
|
|
|
|
|
|
|
// Determine default values based on the available scope.
|
|
|
|
_.defaults(scope, {
|
2020-03-19 18:35:08 +01:00
|
|
|
globalID: _.upperFirst(name),
|
2019-05-02 17:51:58 +02:00
|
|
|
ext: '.js',
|
2017-02-14 01:18:07 +01:00
|
|
|
});
|
|
|
|
|
2017-06-08 17:16:20 +01:00
|
|
|
// Plugin info.
|
|
|
|
_.defaults(scope, {
|
2020-03-19 18:35:08 +01:00
|
|
|
name,
|
2017-06-08 17:16:20 +01:00
|
|
|
author: scope.author || 'A Strapi developer',
|
|
|
|
email: scope.email || '',
|
2019-05-02 17:51:58 +02:00
|
|
|
year: new Date().getFullYear(),
|
|
|
|
license: 'MIT',
|
2017-06-08 17:16:20 +01:00
|
|
|
});
|
2017-02-14 01:18:07 +01:00
|
|
|
|
|
|
|
// Take another pass to take advantage of the defaults absorbed in previous passes.
|
|
|
|
_.defaults(scope, {
|
2019-05-02 17:51:58 +02:00
|
|
|
filename: `${scope.globalID}${scope.ext}`,
|
2020-03-19 18:35:08 +01:00
|
|
|
filePath: './plugins',
|
2017-02-14 01:18:07 +01:00
|
|
|
});
|
|
|
|
|
2019-05-02 17:51:58 +02:00
|
|
|
const pluginDir = path.resolve(scope.rootPath, 'plugins');
|
|
|
|
fs.ensureDirSync(pluginDir);
|
|
|
|
|
2019-10-10 11:45:26 +02:00
|
|
|
// Copy the admin files.
|
2020-03-19 18:35:08 +01:00
|
|
|
fs.copySync(path.resolve(__dirname, '..', 'files'), path.resolve(pluginDir, name));
|
2019-10-10 11:45:26 +02:00
|
|
|
|
2017-02-14 01:18:07 +01:00
|
|
|
// Trigger callback with no error to proceed.
|
|
|
|
return cb.success();
|
|
|
|
};
|