323 lines
8.1 KiB
JavaScript
Raw Normal View History

2021-08-18 18:10:24 +02:00
'use strict';
const { join } = require('path');
2021-08-19 16:42:48 +02:00
const fs = require('fs-extra');
2021-08-20 13:26:43 +02:00
const pluralize = require('pluralize');
2021-08-18 18:10:24 +02:00
2021-08-24 11:46:04 +02:00
const rootDir = process.cwd();
const getFilePath = destination => {
if (destination === 'api') {
return `api/{{api}}`;
}
if (destination === 'plugin') {
return `plugins/{{plugin}}`;
}
return `api/{{id}}`;
};
const getDestinationPrompts = action => {
return [
{
type: 'list',
name: 'destination',
message: `Where do you want to add this ${action}?`,
choices: [
{
name: `Add ${action} to ${action === 'policy' ? 'root of project' : 'new API'}`,
value: 'new',
},
{ name: `Add ${action} to existing API`, value: 'api' },
{ name: `Add ${action} to existing plugin`, value: 'plugin' },
],
},
{
when: answers => answers.destination === 'api',
type: 'input',
message: 'Which API is this for?',
name: 'api',
validate: async input => {
const exists = await fs.pathExists(join(rootDir, `api/${input}`));
return exists || 'That api does not exist, please try again';
},
},
{
when: answers => answers.destination === 'plugin',
type: 'input',
message: 'Which plugin is this for?',
name: 'plugin',
validate: async input => {
const exists = await fs.pathExists(join(rootDir, `plugins/${input}`));
return exists || 'That plugin does not exist, please try again';
},
},
];
};
2021-08-18 18:10:24 +02:00
module.exports = function(plop) {
2021-08-19 16:42:48 +02:00
plop.setWelcomeMessage('Strapi Generators');
2021-08-20 13:26:43 +02:00
plop.addHelper('pluralize', text => pluralize(text));
// API generator
plop.setGenerator('api', {
description: 'Generate a basic API',
2021-08-19 14:15:25 +02:00
prompts: [
{
type: 'input',
name: 'id',
message: 'API name',
2021-08-19 14:15:25 +02:00
},
{
2021-08-24 11:46:04 +02:00
type: 'list',
name: 'kind',
message: 'Please choose the model type',
choices: [
{ name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' },
],
},
2021-08-19 14:15:25 +02:00
{
2021-08-24 11:46:04 +02:00
type: 'confirm',
name: 'isPluginApi',
message: 'Is this API for a plugin?',
2021-08-19 14:15:25 +02:00
},
{
2021-08-24 11:46:04 +02:00
when: answers => answers.isPluginApi,
type: 'input',
name: 'plugin',
message: 'Plugin name',
validate: async input => {
const exists = await fs.pathExists(join(rootDir, `plugins/${input}`));
return exists || 'That plugin does not exist, please try again';
},
2021-08-19 14:15:25 +02:00
},
{
2021-08-24 11:46:04 +02:00
type: 'confirm',
name: 'useDraftAndPublish',
message: 'Use draft and publish?',
},
2021-08-19 14:15:25 +02:00
],
2021-08-24 11:46:04 +02:00
actions: answers => {
let filePath;
if (answers.isPluginApi && answers.plugin) {
filePath = `plugins/{{plugin}}`;
} else {
filePath = `api/{{id}}`;
}
const baseActions = [
{
type: 'add',
path: join(rootDir, `${filePath}/controllers/{{id}}.js`),
templateFile: 'templates/controller.js.hbs',
},
{
type: 'add',
path: join(rootDir, `${filePath}/models/{{id}}.js`),
templateFile: 'templates/model.js.hbs',
},
{
type: 'add',
path: join(rootDir, `${filePath}/models/{{id}}.settings.json`),
templateFile: 'templates/model.settings.json.hbs',
},
{
type: 'add',
path: join(rootDir, `${filePath}/services/{{id}}.js`),
templateFile: 'templates/service.js.hbs',
},
];
if (answers.isPluginApi) {
return baseActions;
} else {
return [
{
type: 'add',
path: join(rootDir, `${filePath}/config/routes.json`),
templateFile: 'templates/api-routes.json.hbs',
},
...baseActions,
];
}
},
2021-08-19 14:15:25 +02:00
});
// Controller generator
plop.setGenerator('controller', {
description: 'Generate a controller for an API',
prompts: [
{
type: 'input',
name: 'id',
message: 'Controller name',
},
2021-08-24 11:46:04 +02:00
...getDestinationPrompts('controller'),
],
2021-08-24 11:46:04 +02:00
actions: answers => {
const filePath = getFilePath(answers.destination);
return [
{
type: 'add',
path: join(rootDir, `${filePath}/controllers/{{id}}.js`),
templateFile: 'templates/controller.js.hbs',
},
];
},
});
2021-08-26 10:15:16 +02:00
plop.setPrompt('recursive', require('inquirer-recursive'));
// Model generator
plop.setGenerator('model', {
description: 'Generate a model for an API',
2021-08-20 13:26:43 +02:00
prompts: [
{
type: 'input',
name: 'id',
message: 'Model name',
2021-08-20 13:26:43 +02:00
},
2021-08-24 11:46:04 +02:00
{
type: 'list',
name: 'kind',
message: 'Please choose the model type',
choices: [
{ name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' },
],
},
...getDestinationPrompts('model'),
2021-08-26 10:15:16 +02:00
{
type: 'addAttributes',
name: 'attributes',
},
2021-08-20 13:26:43 +02:00
{
type: 'confirm',
name: 'useDraftAndPublish',
message: 'Use draft and publish?',
},
],
2021-08-24 11:46:04 +02:00
actions: answers => {
const filePath = getFilePath(answers.destination);
return [
{
type: 'add',
path: join(rootDir, `${filePath}/models/{{id}}.js`),
templateFile: 'templates/model.js.hbs',
},
{
type: 'add',
path: join(rootDir, `${filePath}/models/{{id}}.settings.json`),
templateFile: 'templates/model.settings.json.hbs',
},
];
},
2021-08-20 13:26:43 +02:00
});
2021-08-26 10:15:16 +02:00
const { prompts } = plop.inquirer.prompt;
console.log(prompts);
2021-08-19 16:42:48 +02:00
// Plugin generator
plop.setGenerator('plugin', {
description: 'Generate a basic plugin',
prompts: [
{
type: 'input',
name: 'id',
message: 'Plugin name',
},
],
2021-08-24 11:46:04 +02:00
actions: answers => {
fs.copySync(join(__dirname, 'files', 'plugin'), join(rootDir, 'plugins', answers.id));
2021-08-19 16:42:48 +02:00
return [
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/services/{{id}}.js'),
templateFile: 'templates/service.js.hbs',
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/controllers/{{id}}.js'),
templateFile: 'templates/controller.js.hbs',
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/config/routes.json'),
2021-08-20 13:26:43 +02:00
templateFile: 'templates/plugin-routes.json.hbs',
2021-08-19 16:42:48 +02:00
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/README.md'),
templateFile: 'templates/README.md.hbs',
},
{
type: 'add',
path: join(rootDir, 'plugins/{{id}}/package.json'),
templateFile: 'templates/plugin-package.json.hbs',
},
];
},
});
// Policy generator
plop.setGenerator('policy', {
description: 'Generate a policy for an API',
prompts: [
{
type: 'input',
name: 'id',
message: 'Policy name',
},
2021-08-24 11:46:04 +02:00
...getDestinationPrompts('policy'),
],
2021-08-24 11:46:04 +02:00
actions: answers => {
let filePath;
if (answers.destination === 'api') {
filePath = `api/{{api}}`;
} else if (answers.destination === 'plugin') {
filePath = `plugins/{{plugin}}`;
} else {
filePath = ``;
}
return [
{
type: 'add',
path: join(rootDir, `${filePath}/config/policies/{{id}}.js`),
templateFile: 'templates/policy.js.hbs',
},
];
},
});
// Service generator
plop.setGenerator('service', {
description: 'Generate a service for an API',
prompts: [
{
type: 'input',
name: 'id',
message: 'Service name',
},
2021-08-24 11:46:04 +02:00
...getDestinationPrompts('service'),
],
2021-08-24 11:46:04 +02:00
actions: answers => {
const filePath = getFilePath(answers.destination);
return [
{
type: 'add',
path: join(rootDir, `${filePath}/services/{{id}}.js`),
templateFile: 'templates/service.js.hbs',
},
];
},
});
2021-08-18 18:10:24 +02:00
};