Skip to content

Commit

Permalink
refactor(generator): conditionally include an example preset
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosCortizasCT committed Dec 10, 2024
1 parent 7afd863 commit ace6943
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
80 changes: 52 additions & 28 deletions generators/src/new-test-model/new-test-model.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ function ensureDirectory(filePath: string) {
}
}

function modelTemplatesFilter(
isPresetExampleRequired: boolean,
isDraftModel = false
) {
return (template: { templatePath: string; templateContent: string }) => {
if (isDraftModel && template.templatePath.includes('types')) {
return false;
}
if (!isPresetExampleRequired && template.templatePath.includes('example')) {
return false;
}
return true;
};
}

export const newTestModelGenerator: CodeGenerator = {
generate: async () => {
// 1. Get input information
Expand All @@ -38,6 +53,13 @@ export const newTestModelGenerator: CodeGenerator = {
initial: false,
});

const { isPresetExampleRequired } = await prompts({
type: 'confirm',
name: 'isPresetExampleRequired',
message: 'Do you want to generate a preset example?',
initial: false,
});

const { modelOwningService } = await prompts({
type: 'select',
name: 'modelOwningService',
Expand Down Expand Up @@ -95,6 +117,7 @@ export const newTestModelGenerator: CodeGenerator = {
modelCodename,
graphqlTypePrefix,
isDraftRequired,
isPresetExampleRequired,
packageVersion: corePackageJson.version,
};

Expand All @@ -111,44 +134,45 @@ export const newTestModelGenerator: CodeGenerator = {
});
}

modelTemplatesData.forEach((template) => {
const filePath = join(
outputPath,
generationType === 'standalone' ? 'src' : '',
template.templatePath
);
ensureDirectory(filePath);
console.log(`Generating file: ${filePath}`);
writeFileSync(
filePath,
renderTemplate(template.templateContent, templatesData)
);
});

if (isDraftRequired) {
modelTemplatesData.forEach((template) => {
if (template.templatePath.includes('types')) {
return;
}

modelTemplatesData
.filter(modelTemplatesFilter(isPresetExampleRequired))
.forEach((template) => {
const filePath = join(
outputPath,
'src',
`${modelCodename}-draft`,
generationType === 'standalone' ? 'src' : '',
template.templatePath
);
ensureDirectory(filePath);
console.log(`Generating file: ${filePath}`);
writeFileSync(
filePath,
renderTemplate(template.templateContent, {
isDraftModel: true,
modelName: `${modelName}Draft`,
modelCodename: `${modelCodename}-draft`,
graphqlTypePrefix,
})
renderTemplate(template.templateContent, templatesData)
);
});

if (isDraftRequired) {
modelTemplatesData
.filter(modelTemplatesFilter(isPresetExampleRequired, true))
.forEach((template) => {
const filePath = join(
outputPath,
'src',
`${modelCodename}-draft`,
template.templatePath
);
ensureDirectory(filePath);
console.log(`Generating file: ${filePath}`);
writeFileSync(
filePath,
renderTemplate(template.templateContent, {
isDraftModel: true,
isPresetExampleRequired,
modelName: `${modelName}Draft`,
modelCodename: `${modelCodename}-draft`,
graphqlTypePrefix,
})
);
});
}

console.log('\nAll set! 🚀');
Expand Down
2 changes: 1 addition & 1 deletion generators/src/new-test-model/templates/model/builders.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSpecializedBuilder } from '@commercetools-test-data/core';
import { restFieldsConfig, graphqlFieldsConfig } from './fields-config';
import type { TCreate{{it.modelName}}Builder, T{{it.modelName}}Graphql, T{{it.modelName}}Rest } from './types';
import type { TCreate{{it.modelName}}Builder, T{{it.modelName}}Graphql, T{{it.modelName}}Rest } from '{{@if(it.isDraftModel !== true)}}.{{#else}}..{{/if}}/types';

export const RestModelBuilder: TCreate{{it.modelName}}Builder<T{{it.modelName}}Rest> = () =>
createSpecializedBuilder({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { {{it.modelName}}Graphql, {{it.modelName}}Rest } from '../..';
import type {
T{{it.modelName}}Graphql,
T{{it.modelName}}Rest,
} from '../../types';
} from '{{@if(it.isDraftModel !== true)}}../..{{#else}}../../..{{/if}}/types';

export const restPreset = (): TBuilder<T{{it.modelName}}Rest> =>
{{it.modelName}}Rest.random()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{{@if(it.isPresetExampleRequired === true)}}
import * as examplePresets from './example-preset/example-preset';

export const restPresets = {
example: examplePresets.restPreset,
};

export const graphqlPresets = {
example: examplePresets.graphqlPreset,
};

{{#else}}
export const restPresets = {};
export const graphqlPresets = {};
{{/if}}

0 comments on commit ace6943

Please sign in to comment.