From a500e88129bcb6ed22b402e54d2a7c2ad8e9dcac Mon Sep 17 00:00:00 2001 From: Mark Kaylor Date: Thu, 26 Aug 2021 18:02:11 +0200 Subject: [PATCH] add enums --- packages/generators/generate/plops/api.js | 18 ++++++------- packages/generators/generate/plops/model.js | 28 +++++++++++++++------ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/generators/generate/plops/api.js b/packages/generators/generate/plops/api.js index b0f31ad4bb..2f0cf626d1 100644 --- a/packages/generators/generate/plops/api.js +++ b/packages/generators/generate/plops/api.js @@ -13,15 +13,6 @@ module.exports = (plop, rootDir) => { name: 'id', 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', name: 'isPluginApi', @@ -38,6 +29,15 @@ module.exports = (plop, rootDir) => { 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', name: 'useDraftAndPublish', diff --git a/packages/generators/generate/plops/model.js b/packages/generators/generate/plops/model.js index 27f4811c04..682773c021 100644 --- a/packages/generators/generate/plops/model.js +++ b/packages/generators/generate/plops/model.js @@ -73,18 +73,30 @@ module.exports = (plop, rootDir) => { 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 => { + console.log(answers); const attributes = answers.attributes.reduce((object, answer) => { // Rest/spread properties are not supported until Node.js 8.3.0. // The configured version range is '>=8.0.0' - return Object.assign( - object, - { [answer.attributeName]: { type: answer.attributeType } }, - {} - ); + const val = { 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); @@ -104,9 +116,9 @@ module.exports = (plop, rootDir) => { type: 'modify', path: join(rootDir, `${filePath}/models/{{id}}.settings.json`), transform: template => { - const temp = JSON.parse(template); - temp.attributes = attributes; - return JSON.stringify(temp, null, 2); + const parsedTemplate = JSON.parse(template); + parsedTemplate.attributes = attributes; + return JSON.stringify(parsedTemplate, null, 2); }, }, ];