Skip to content

Commit

Permalink
feat(cli): add modifiers in make:model and make:composable
Browse files Browse the repository at this point in the history
Property can now be set as `readOnly` and `nullable` through the CLI commands `make:model` and `make:composable`.

Closes #5
  • Loading branch information
paul-thebaud committed Nov 9, 2023
1 parent dec11a7 commit af127d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/cli/src/templates/renderPropertyForDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}()` : ''})`;
}
Expand All @@ -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();
}
15 changes: 13 additions & 2 deletions packages/cli/src/utils/input/promptForProperties.ts
Original file line number Diff line number Diff line change
@@ -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$]+$/;

Expand All @@ -10,6 +10,7 @@ export type DefinitionProperty = {
name: string;
transformer?: string;
type?: string;
modifiers?: ('readOnly' | 'nullable')[];
};

async function promptForProperty(
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit af127d2

Please sign in to comment.