Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inquirer update #50

Merged
merged 9 commits into from
Jul 17, 2024
Merged
Binary file modified bun.lockb
Binary file not shown.
43 changes: 24 additions & 19 deletions lib/generators/constant.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import type { Answers } from "inquirer";
import { input } from "@inquirer/prompts";
import type { AppendAction } from "../utils/actions";
import type { GeneratorDefinition } from "../utils/generator";
import type { GeneratorDefinition, QuestionsObject } from "../utils/generator";
import { stringEmpty } from "../utils/questions/validators";

const generator: GeneratorDefinition<Answers> = {
type ConstantAnswers = {
constantName: string;
constantValue: string;
};

const generator: GeneratorDefinition<ConstantAnswers> = {
name: "Constant",
description: "Create a new workspace constant",
questions: [
{
type: "input",
name: "constantName",
message: "Constant:",
validate: stringEmpty,
},
{
type: "input",
name: "constantValue",
message: "Value:",
default: "New Value",
validate: stringEmpty,
},
],
questions: {
constantName: () =>
input({
message: "Constant:",
required: true,
validate: stringEmpty,
}),
constantValue: () =>
input({
message: "Value:",
default: "New Value",
required: true,
validate: stringEmpty,
}),
} as QuestionsObject,
actions: [
{
type: "append",
path: "architecture/workspace.dsl",
pattern: /# Constants/,
templateFile: "templates/constant.hbs",
} as AppendAction,
} as AppendAction<ConstantAnswers>,
],
};

Expand Down
131 changes: 69 additions & 62 deletions lib/generators/container.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { resolve } from "node:path";
import { Separator, input, select } from "@inquirer/prompts";
import { file } from "bun";
import { kebabCase, pascalCase } from "change-case";
import type { Answers, QuestionCollection } from "inquirer";
import inquirer from "inquirer";
import type { AddAction, AppendAction } from "../utils/actions";
import type { GeneratorDefinition } from "../utils/generator";
import { removeSpaces } from "../utils/handlebars";
import { getRelationships } from "../utils/questions/relationships";
import { getSystemQuestion } from "../utils/questions/system";
import {
type Relationship,
addRelationshipsToElement,
} from "../utils/questions/relationships";
import { resolveSystemQuestion } from "../utils/questions/system";
import {
chainValidators,
duplicatedSystemName,
Expand All @@ -16,83 +18,88 @@ import {
} from "../utils/questions/validators";
import { getWorkspaceJson, getWorkspacePath } from "../utils/workspace";

const generator: GeneratorDefinition<Answers> = {
type ContainerAnswers = {
systemName: string;
elementName: string;
containerDescription: string;
containerType: string;
containerTechnology: string;
includeTabs: string;
includeSource: string;
relationships: Record<string, Relationship>;
};

const generator: GeneratorDefinition<ContainerAnswers> = {
name: "Container",
description: "Create a new system container",
questions: async (prompt, generator) => {
questions: async (generator) => {
const workspaceInfo = await getWorkspaceJson(
getWorkspacePath(generator.destPath),
);
const systemQuestion = await getSystemQuestion(

const systemName = await resolveSystemQuestion(
workspaceInfo ?? generator.destPath,
{
message: "Parent system:",
},
{ message: "Parent system:" },
);

const questions: QuestionCollection<Answers> = [
systemQuestion,
{
type: "input",
name: "elementName",
message: "Container Name:",
validate: chainValidators(
stringEmpty,
duplicatedSystemName,
validateDuplicatedElements(workspaceInfo),
),
},
{
type: "input",
name: "containerDescription",
message: "Container Description:",
default: "Untitled Container",
validate: stringEmpty,
},
{
type: "list",
name: "containerType",
message: "Container type:",
choices: [
"EventBus",
"MessageBroker",
"Function",
"Database",
"WebApp",
"MobileApp",
"None of the above",
],
},
{
type: "input",
name: "containerTechnology",
message: "Container technology:",
},
];
const elementName = await input({
message: "Container Name:",
required: true,
validate: chainValidators<{ systemName: string }>(
stringEmpty,
duplicatedSystemName,
validateDuplicatedElements(workspaceInfo),
)({ systemName }),
});

const containerDescription = await input({
message: "Container Description:",
default: "Untitled Container",
validate: stringEmpty,
});

const containerType = await select({
message: "Container type:",
default: "None of the above",
choices: [
{ name: "EventBus", value: "EventBus" },
{ name: "MessageBroker", value: "MessageBroker" },
{ name: "Function", value: "Function" },
{ name: "Database", value: "Database" },
{ name: "WebApp", value: "WebApp" },
{ name: "MobileApp", value: "MobileApp" },
{ name: "None of the above", value: "None of the above" },
],
});

const containerTechnology = await input({
message: "Container technology:",
});

const relationshipDefaults = {
defaultRelationship: "Uses",
defaultRelationshipType: "incoming",
};

const partialAnswers = await prompt(questions);
const relationships = await getRelationships(
partialAnswers.elementName,
const relationships = await addRelationshipsToElement(
elementName,
workspaceInfo,
prompt,
{
filterChoices: (elm) =>
elm instanceof inquirer.Separator ||
elm.value !== partialAnswers.systemName,
elm instanceof Separator || elm.value !== systemName,
...relationshipDefaults,
includeContainers: partialAnswers.systemName,
includeContainers: systemName,
},
);

const compiledAnswers = {
...partialAnswers,
systemName,
elementName,
containerDescription,
containerType,
containerTechnology,
includeTabs: "",
includeSource: `${kebabCase(partialAnswers.systemName)}.dsl`,
includeSource: `${kebabCase(systemName)}.dsl`,
relationships,
};

Expand All @@ -104,7 +111,7 @@ const generator: GeneratorDefinition<Answers> = {
path: "architecture/containers/{{kebabCase systemName}}/{{kebabCase elementName}}.dsl",
skipIfExists: true,
templateFile: "templates/containers/container.hbs",
} as AddAction,
} as AddAction<ContainerAnswers>,
{
type: "append",
path: "architecture/relationships/_system.dsl",
Expand Down Expand Up @@ -132,7 +139,7 @@ const generator: GeneratorDefinition<Answers> = {
},
pattern: /.*\n!include.*/,
templateFile: "templates/include.hbs",
} as AppendAction,
} as AppendAction<ContainerAnswers>,
{
type: "append",
path: "architecture/views/{{kebabCase systemName}}.dsl",
Expand All @@ -157,13 +164,13 @@ const generator: GeneratorDefinition<Answers> = {
);
},
templateFile: "templates/views/container.hbs",
} as AppendAction,
} as AppendAction<ContainerAnswers>,
{
type: "append",
createIfNotExists: true,
path: "architecture/relationships/{{kebabCase systemName}}.dsl",
templateFile: "templates/relationships/multiple.hbs",
} as AppendAction,
} as AppendAction<ContainerAnswers>,
],
};

Expand Down
Loading