From af127d2c64c73f5d03347ca4d5a5c85a314b6f6c Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 9 Nov 2023 19:30:52 +0100 Subject: [PATCH] feat(cli): add modifiers in make:model and make:composable Property can now be set as `readOnly` and `nullable` through the CLI commands `make:model` and `make:composable`. Closes #5 --- .../cli/src/templates/renderPropertyForDef.ts | 14 +++++++++++++- .../cli/src/utils/input/promptForProperties.ts | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/templates/renderPropertyForDef.ts b/packages/cli/src/templates/renderPropertyForDef.ts index a706a354..11aba7fb 100644 --- a/packages/cli/src/templates/renderPropertyForDef.ts +++ b/packages/cli/src/templates/renderPropertyForDef.ts @@ -4,6 +4,13 @@ type PropertyForDefTemplateData = { property: DefinitionProperty; }; +function renderPropertyModifiers(property: DefinitionProperty) { + const modifiers = (property.modifiers ?? []); + modifiers.sort(); + + return modifiers.map((m) => `.${m}()`).join(''); +} + function renderAttributeProperty(property: DefinitionProperty) { return `attr${property.type ? `<${property.type}>` : ''}(${property.transformer ? `${property.transformer}()` : ''})`; } @@ -13,7 +20,12 @@ function renderRelationProperty(property: DefinitionProperty) { } export default function renderPropertyForDef({ property }: PropertyForDefTemplateData) { + const propertyDefinition = property.typology === 'attr' + ? renderAttributeProperty(property) + : renderRelationProperty(property); + const propertyModifiers = renderPropertyModifiers(property); + return ` -${property.name}: ${property.typology === 'attr' ? renderAttributeProperty(property) : renderRelationProperty(property)} +${property.name}: ${propertyDefinition}${propertyModifiers} `.trim(); } diff --git a/packages/cli/src/utils/input/promptForProperties.ts b/packages/cli/src/utils/input/promptForProperties.ts index 39b527a8..025ae955 100644 --- a/packages/cli/src/utils/input/promptForProperties.ts +++ b/packages/cli/src/utils/input/promptForProperties.ts @@ -1,7 +1,7 @@ import { CLIConfig } from '@foscia/cli/utils/config/config'; import { ImportsList } from '@foscia/cli/utils/imports/makeImportsList'; import promptForModelType from '@foscia/cli/utils/input/promptForModelType'; -import { input, select } from '@inquirer/prompts'; +import { checkbox, input, select } from '@inquirer/prompts'; const VALID_NAME_REGEX = /^(?!\d)[\w$]+$/; @@ -10,6 +10,7 @@ export type DefinitionProperty = { name: string; transformer?: string; type?: string; + modifiers?: ('readOnly' | 'nullable')[]; }; async function promptForProperty( @@ -62,7 +63,17 @@ async function promptForProperty( }, }); - const property = { name, typology } as DefinitionProperty; + const property = { + name, + typology, + modifiers: await checkbox({ + message: 'Give specificities:', + choices: [ + { name: 'Is nullable', value: 'nullable' }, + { name: 'Is read-only', value: 'readOnly' }, + ] as const, + }), + } as DefinitionProperty; if (typology === 'attr') { const transformer = await select({