add enums

This commit is contained in:
Mark Kaylor 2021-08-26 18:02:11 +02:00
parent 19a8c7676d
commit a500e88129
2 changed files with 29 additions and 17 deletions

View File

@ -13,15 +13,6 @@ module.exports = (plop, rootDir) => {
name: 'id', name: 'id',
message: 'API name', message: 'API name',
}, },
{
type: 'list',
name: 'kind',
message: 'Please choose the model type',
choices: [
{ name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' },
],
},
{ {
type: 'confirm', type: 'confirm',
name: 'isPluginApi', name: 'isPluginApi',
@ -38,6 +29,15 @@ module.exports = (plop, rootDir) => {
return exists || 'That plugin does not exist, please try again'; return exists || 'That plugin does not exist, please try again';
}, },
}, },
{
type: 'list',
name: 'kind',
message: 'Please choose the model type',
choices: [
{ name: 'Collection Type', value: 'collectionType' },
{ name: 'Singe Type', value: 'singleType' },
],
},
{ {
type: 'confirm', type: 'confirm',
name: 'useDraftAndPublish', name: 'useDraftAndPublish',

View File

@ -73,18 +73,30 @@ module.exports = (plop, rootDir) => {
return { name: type, value: type }; return { name: type, value: type };
}), }),
}, },
{
when: answers => {
console.log(answers);
return answers.attributeType === 'enumeration';
},
type: 'input',
name: 'enum',
message: 'Add values separated by a comma',
},
], ],
}, },
], ],
actions: answers => { actions: answers => {
console.log(answers);
const attributes = answers.attributes.reduce((object, answer) => { const attributes = answers.attributes.reduce((object, answer) => {
// Rest/spread properties are not supported until Node.js 8.3.0. // Rest/spread properties are not supported until Node.js 8.3.0.
// The configured version range is '>=8.0.0' // The configured version range is '>=8.0.0'
return Object.assign( const val = { type: answer.attributeType };
object,
{ [answer.attributeName]: { type: answer.attributeType } }, if (answer.attributeType === 'enumeration') {
{} val.enum = answer.enum.split(',').map(item => item.trim());
); }
return Object.assign(object, { [answer.attributeName]: val }, {});
}, {}); }, {});
const filePath = getFilePath(answers.destination); const filePath = getFilePath(answers.destination);
@ -104,9 +116,9 @@ module.exports = (plop, rootDir) => {
type: 'modify', type: 'modify',
path: join(rootDir, `${filePath}/models/{{id}}.settings.json`), path: join(rootDir, `${filePath}/models/{{id}}.settings.json`),
transform: template => { transform: template => {
const temp = JSON.parse(template); const parsedTemplate = JSON.parse(template);
temp.attributes = attributes; parsedTemplate.attributes = attributes;
return JSON.stringify(temp, null, 2); return JSON.stringify(parsedTemplate, null, 2);
}, },
}, },
]; ];