mirror of
https://github.com/strapi/strapi.git
synced 2025-12-29 16:16:20 +00:00
Fix model generator
This commit is contained in:
parent
3ab4da68ab
commit
526a2ce671
@ -6,7 +6,6 @@
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"fs-extra": "10.0.0",
|
||||
"inquirer-recursive": "0.0.7",
|
||||
"node-plop": "0.26.2",
|
||||
"plop": "2.7.4",
|
||||
"pluralize": "8.0.0"
|
||||
|
||||
@ -9,11 +9,10 @@ const generatePlugin = require('./plops/plugin');
|
||||
const generatePolicy = require('./plops/policy');
|
||||
const generateService = require('./plops/service');
|
||||
|
||||
module.exports = (plop) => {
|
||||
module.exports = plop => {
|
||||
// Plop config
|
||||
plop.setWelcomeMessage('Strapi Generators');
|
||||
plop.addHelper('pluralize', text => pluralize(text));
|
||||
plop.setPrompt('recursive', require('inquirer-recursive'));
|
||||
|
||||
// Generators
|
||||
generateApi(plop);
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const { join } = require('path');
|
||||
const getDestinationPrompts = require('./utils/get-destination-prompts');
|
||||
const getFilePath = require('./utils/get-file-path');
|
||||
|
||||
@ -27,69 +26,109 @@ const DEFAULT_TYPES = [
|
||||
'boolean',
|
||||
];
|
||||
|
||||
module.exports = (plop, rootDir) => {
|
||||
const promptConfigQuestions = (plop, inquirer) => {
|
||||
return inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'id',
|
||||
message: 'Model name',
|
||||
},
|
||||
{
|
||||
type: 'list',
|
||||
name: 'kind',
|
||||
message: 'Please choose the model type',
|
||||
choices: [
|
||||
{ name: 'Collection Type', value: 'collectionType' },
|
||||
{ name: 'Singe Type', value: 'singleType' },
|
||||
],
|
||||
},
|
||||
...getDestinationPrompts('model', plop.getDestBasePath()),
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'useDraftAndPublish',
|
||||
message: 'Use draft and publish?',
|
||||
},
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'addAttributes',
|
||||
message: 'Do you want to add attributes?',
|
||||
},
|
||||
]);
|
||||
};
|
||||
|
||||
const promptAttributeQuestions = inquirer => {
|
||||
return inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'attributeName',
|
||||
message: 'Name of attribute',
|
||||
},
|
||||
{
|
||||
type: 'list',
|
||||
name: 'attributeType',
|
||||
message: 'What type of attribute',
|
||||
pageSize: DEFAULT_TYPES.length,
|
||||
choices: DEFAULT_TYPES.map(type => {
|
||||
return { name: type, value: type };
|
||||
}),
|
||||
},
|
||||
{
|
||||
when: answers => answers.attributeType === 'enumeration',
|
||||
type: 'input',
|
||||
name: 'enum',
|
||||
message: 'Add values separated by a comma',
|
||||
},
|
||||
{
|
||||
when: answers => answers.attributeType === 'media',
|
||||
type: 'list',
|
||||
name: 'multiple',
|
||||
message: 'Choose media type',
|
||||
choices: [
|
||||
{ name: 'Multiple', value: true },
|
||||
{ name: 'Single', value: false },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'addAttributes',
|
||||
message: 'Do you want to add another attribute?',
|
||||
},
|
||||
]);
|
||||
};
|
||||
|
||||
module.exports = plop => {
|
||||
// Model generator
|
||||
plop.setGenerator('model', {
|
||||
description: 'Generate a model for an API',
|
||||
prompts: [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'id',
|
||||
message: 'Model name',
|
||||
},
|
||||
{
|
||||
type: 'list',
|
||||
name: 'kind',
|
||||
message: 'Please choose the model type',
|
||||
choices: [
|
||||
{ name: 'Collection Type', value: 'collectionType' },
|
||||
{ name: 'Singe Type', value: 'singleType' },
|
||||
],
|
||||
},
|
||||
...getDestinationPrompts('model', plop.getDestBasePath()),
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'useDraftAndPublish',
|
||||
message: 'Use draft and publish?',
|
||||
},
|
||||
{
|
||||
type: 'recursive',
|
||||
message: 'Add attribute?',
|
||||
name: 'attributes',
|
||||
prompts: [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'attributeName',
|
||||
message: 'Name of attribute',
|
||||
},
|
||||
{
|
||||
type: 'list',
|
||||
name: 'attributeType',
|
||||
message: 'What type of attribute',
|
||||
pageSize: DEFAULT_TYPES.length,
|
||||
choices: DEFAULT_TYPES.map(type => {
|
||||
return { name: type, value: type };
|
||||
}),
|
||||
},
|
||||
{
|
||||
when: answers => answers.attributeType === 'enumeration',
|
||||
type: 'input',
|
||||
name: 'enum',
|
||||
message: 'Add values separated by a comma',
|
||||
},
|
||||
{
|
||||
when: answers => answers.attributeType === 'media',
|
||||
type: 'list',
|
||||
name: 'multiple',
|
||||
message: 'Choose media type',
|
||||
choices: [
|
||||
{ name: 'Multiple', value: true },
|
||||
{ name: 'Single', value: false },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
prompts: async 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 => {
|
||||
const attributes = answers.attributes.reduce((object, answer) => {
|
||||
const val = { type: answer.attributeType };
|
||||
@ -116,12 +155,12 @@ module.exports = (plop, rootDir) => {
|
||||
},
|
||||
{
|
||||
type: 'add',
|
||||
path: join(rootDir, `${filePath}/models/{{id}}.settings.json`),
|
||||
path: `${filePath}/models/{{id}}.settings.json`,
|
||||
templateFile: 'templates/model.settings.json.hbs',
|
||||
},
|
||||
{
|
||||
type: 'modify',
|
||||
path: join(rootDir, `${filePath}/models/{{id}}.settings.json`),
|
||||
path: `${filePath}/models/{{id}}.settings.json`,
|
||||
transform: template => {
|
||||
const parsedTemplate = JSON.parse(template);
|
||||
parsedTemplate.attributes = attributes;
|
||||
|
||||
10
yarn.lock
10
yarn.lock
@ -11159,14 +11159,6 @@ init-package-json@^1.10.3:
|
||||
validate-npm-package-license "^3.0.1"
|
||||
validate-npm-package-name "^3.0.0"
|
||||
|
||||
inquirer-recursive@0.0.7:
|
||||
version "0.0.7"
|
||||
resolved "https://registry.yarnpkg.com/inquirer-recursive/-/inquirer-recursive-0.0.7.tgz#09f65757e3ee10273d8c6b97e36663484741dba5"
|
||||
integrity sha512-z15FD5N3GrzXM7OwEijm/9FbZzUiQUJQxrtUM0mOy3KeKshp3v/FhjspM8E3a9IgG6sYlN619lHpAdupn90ikQ==
|
||||
dependencies:
|
||||
inquirer "^7.3.3"
|
||||
lodash "^4.17.20"
|
||||
|
||||
inquirer@8.1.0:
|
||||
version "8.1.0"
|
||||
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-8.1.0.tgz#68ce5ce5376cf0e89765c993d8b7c1e62e184d69"
|
||||
@ -11206,7 +11198,7 @@ inquirer@^6.2.0, inquirer@^6.2.1, inquirer@^6.3.1:
|
||||
strip-ansi "^5.1.0"
|
||||
through "^2.3.6"
|
||||
|
||||
inquirer@^7.1.0, inquirer@^7.3.3:
|
||||
inquirer@^7.1.0:
|
||||
version "7.3.3"
|
||||
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-7.3.3.tgz#04d176b2af04afc157a83fd7c100e98ee0aad003"
|
||||
integrity sha512-JG3eIAj5V9CwcGvuOmoo6LB9kbAYT8HXffUl6memuszlwDC/qvFAJw49XJ5NROSFNPxp3iQg1GqkFhaY/CR0IA==
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user