mirror of
https://github.com/strapi/strapi.git
synced 2025-11-30 09:01:16 +00:00
use plop.getGenerator
This commit is contained in:
parent
e9f729a664
commit
6a549e4843
@ -3,8 +3,6 @@
|
||||
const { join } = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const validateInput = require('./utils/validate-input');
|
||||
const contentTypePrompts = require('./content-type').prompts;
|
||||
const contentTypeActions = require('./content-type').actions;
|
||||
|
||||
module.exports = plop => {
|
||||
// API generator
|
||||
@ -74,12 +72,10 @@ module.exports = plop => {
|
||||
return api;
|
||||
}
|
||||
|
||||
// TODO: make prompts and actions more re-usable and composable
|
||||
const contentType = await contentTypePrompts(plop, inquirer);
|
||||
|
||||
return {
|
||||
...api,
|
||||
...contentType,
|
||||
// TODO: make prompts and actions more re-usable and composable
|
||||
...(await plop.getGenerator('content-type').prompts(inquirer)),
|
||||
};
|
||||
},
|
||||
actions(answers) {
|
||||
@ -125,7 +121,7 @@ module.exports = plop => {
|
||||
},
|
||||
...baseActions,
|
||||
// TODO: make prompts and actions more re-usable and composable
|
||||
...(answers.createContentType ? contentTypeActions(answers) : []),
|
||||
...(answers.createContentType ? plop.getGenerator('content-type').actions(answers) : []),
|
||||
];
|
||||
},
|
||||
});
|
||||
|
||||
@ -133,90 +133,77 @@ const promptAttributeQuestions = inquirer => {
|
||||
]);
|
||||
};
|
||||
|
||||
// TODO: make prompts and actions more re-usable and composable
|
||||
const prompts = async (plop, inquirer) => {
|
||||
const config = await promptConfigQuestions(plop, inquirer);
|
||||
|
||||
if (!config.addAttributes) {
|
||||
return {
|
||||
...config,
|
||||
attributes: [],
|
||||
};
|
||||
}
|
||||
|
||||
const attributes = [];
|
||||
|
||||
const genAttribute = async () => {
|
||||
const answers = await promptAttributeQuestions(inquirer);
|
||||
|
||||
attributes.push(answers);
|
||||
|
||||
if (answers.addAttributes) {
|
||||
return genAttribute();
|
||||
}
|
||||
};
|
||||
|
||||
await genAttribute();
|
||||
|
||||
return {
|
||||
...config,
|
||||
attributes,
|
||||
};
|
||||
};
|
||||
|
||||
// TODO: make prompts and actions more re-usable and composable
|
||||
const actions = answers => {
|
||||
const attributes = answers.attributes.reduce((object, answer) => {
|
||||
const val = { type: answer.attributeType };
|
||||
|
||||
if (answer.attributeType === 'enumeration') {
|
||||
val.enum = answer.enum.split(',').map(item => item.trim());
|
||||
}
|
||||
|
||||
if (answer.attributeType === 'media') {
|
||||
val.allowedTypes = ['images', 'files', 'videos'];
|
||||
val.multiple = answer.multiple;
|
||||
}
|
||||
|
||||
return Object.assign(object, { [answer.attributeName]: val }, {});
|
||||
}, {});
|
||||
|
||||
const filePath = getFilePath(answers.destination);
|
||||
|
||||
return [
|
||||
{
|
||||
type: 'add',
|
||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||
templateFile: 'templates/content-type.schema.json.hbs',
|
||||
data: {
|
||||
id: answers.singularName,
|
||||
collectionName: slugify(answers.pluralName, { separator: '_' }),
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'modify',
|
||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||
transform(template) {
|
||||
const parsedTemplate = JSON.parse(template);
|
||||
parsedTemplate.attributes = attributes;
|
||||
return JSON.stringify(parsedTemplate, null, 2);
|
||||
},
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
module.exports = plop => {
|
||||
// Model generator
|
||||
plop.setGenerator('content-type', {
|
||||
description: 'Generate a content type for an API',
|
||||
async prompts(inquirer) {
|
||||
return prompts(plop, inquirer);
|
||||
const config = await promptConfigQuestions(plop, inquirer);
|
||||
|
||||
if (!config.addAttributes) {
|
||||
return {
|
||||
...config,
|
||||
attributes: [],
|
||||
};
|
||||
}
|
||||
|
||||
const attributes = [];
|
||||
|
||||
const genAttribute = async () => {
|
||||
const answers = await promptAttributeQuestions(inquirer);
|
||||
|
||||
attributes.push(answers);
|
||||
|
||||
if (answers.addAttributes) {
|
||||
return genAttribute();
|
||||
}
|
||||
};
|
||||
|
||||
await genAttribute();
|
||||
|
||||
return {
|
||||
...config,
|
||||
attributes,
|
||||
};
|
||||
},
|
||||
actions(answers) {
|
||||
return actions(answers);
|
||||
const attributes = answers.attributes.reduce((object, answer) => {
|
||||
const val = { type: answer.attributeType };
|
||||
|
||||
if (answer.attributeType === 'enumeration') {
|
||||
val.enum = answer.enum.split(',').map(item => item.trim());
|
||||
}
|
||||
|
||||
if (answer.attributeType === 'media') {
|
||||
val.allowedTypes = ['images', 'files', 'videos'];
|
||||
val.multiple = answer.multiple;
|
||||
}
|
||||
|
||||
return Object.assign(object, { [answer.attributeName]: val }, {});
|
||||
}, {});
|
||||
|
||||
const filePath = getFilePath(answers.destination);
|
||||
|
||||
return [
|
||||
{
|
||||
type: 'add',
|
||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||
templateFile: 'templates/content-type.schema.json.hbs',
|
||||
data: {
|
||||
id: answers.singularName,
|
||||
collectionName: slugify(answers.pluralName, { separator: '_' }),
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'modify',
|
||||
path: `${filePath}/content-types/{{ singularName }}/schema.json`,
|
||||
transform(template) {
|
||||
const parsedTemplate = JSON.parse(template);
|
||||
parsedTemplate.attributes = attributes;
|
||||
return JSON.stringify(parsedTemplate, null, 2);
|
||||
},
|
||||
},
|
||||
];
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.prompts = prompts;
|
||||
module.exports.actions = actions;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user