Skip to content

Commit

Permalink
feat: added function to add entities to sidebar automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoalbanese committed Jan 16, 2024
1 parent f2a2ad9 commit aca09f8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
52 changes: 52 additions & 0 deletions src/commands/generate/generators/model/views-shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { existsSync, readFileSync } from "fs";
import { formatFilePath } from "../../../filePaths/index.js";
import { formatTableName } from "../../utils.js";
import { replaceFile } from "../../../../utils.js";

export const addLinkToSidebar = (tableName: string) => {
const { tableNameKebabCase, tableNameNormalEnglishCapitalised } =
formatTableName(tableName);
const sidebarConfigPath = formatFilePath("config/nav.ts", {
prefix: "rootPath",
removeExtension: false,
});
const configExists = existsSync(sidebarConfigPath);
if (!configExists) return;

const configContents = readFileSync(sidebarConfigPath, "utf-8");
const initContents = `export const additionalLinks: AdditionalLinks[] = [];`;
const replacedInitContents = `export const additionalLinks: AdditionalLinks[] = [
{
title: "Entities",
links: [
{
href: "/${tableNameKebabCase}",
title: "${tableNameNormalEnglishCapitalised}",
icon: Globe,
},
],
},
];
`;
let newContent: string;
if (configContents.search(initContents) > 0) {
newContent = configContents.replace(initContents, replacedInitContents);
} else {
if (configContents.search(tableNameKebabCase) > 0) return;
const searchQuery = ` title: "Entities",
links: [
`;
const replacement = ` title: "Entities",
links: [
{
href: "/${tableNameKebabCase}",
title: "${tableNameNormalEnglishCapitalised}",
icon: Globe,
},
`;
newContent = configContents.replace(searchQuery, replacement);
console.log(newContent);
}
replaceFile(sidebarConfigPath, newContent);
};
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,7 @@ const EmptyState = ({ openModal }: { openModal: TOpenModal }) => {
</p>
<div className="mt-6">
<Button onClick={() => openModal()}>
<PlusIcon className="h-4" /> New ${tableNameSingularCapitalised}
</Button>
<PlusIcon className="h-4" /> New ${tableNameNormalEnglishCapitalised} </Button>
</div>
</div>
);
Expand Down
25 changes: 18 additions & 7 deletions src/commands/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import {
import { scaffoldModel } from "./generators/model/index.js";
import { scaffoldServerActions } from "./generators/serverActions.js";
import { scaffoldViewsAndComponentsWithServerActions } from "./generators/views-with-server-actions.js";
import { addLinkToSidebar } from "./generators/model/views-shared.js";

function provideInstructions() {
consola.info(
"Quickly generate your Model (Drizzle schema + queries / mutations), Controllers (API Routes and TRPC Routes), and Views",
"Quickly generate your Model (Drizzle schema + queries / mutations), Controllers (API Routes and TRPC Routes), and Views"
);
}

Expand Down Expand Up @@ -170,9 +171,7 @@ async function askForResourceType() {
: false,
},
].filter((item) =>
viewRequested
? !viewRequested.includes(item.value.split("_")[0])
: item,
viewRequested ? !viewRequested.includes(item.value.split("_")[0]) : item
),
})) as TResource[];
}
Expand Down Expand Up @@ -210,7 +209,7 @@ async function askForFields(orm: ORMType, dbType: DBType, tableName: string) {
const currentSchemas = getCurrentSchemas();

const baseFieldTypeChoices = Object.keys(
createOrmMappings()[orm][dbType].typeMappings,
createOrmMappings()[orm][dbType].typeMappings
)
.filter((field) => field !== "id")
.map((field) => {
Expand All @@ -223,7 +222,7 @@ async function askForFields(orm: ORMType, dbType: DBType, tableName: string) {
currentSchemas[0] === toCamelCase(tableName));
const fieldTypeChoices = removeReferenceOption
? baseFieldTypeChoices.filter(
(field) => field.name.toLowerCase() !== "references",
(field) => field.name.toLowerCase() !== "references"
)
: baseFieldTypeChoices;

Expand Down Expand Up @@ -354,6 +353,18 @@ export async function buildSchema() {
};
}

if (
resourceType.includes("views_and_components_trpc") ||
resourceType.includes("views_and_components_server_actions")
) {
const addToSidebar = await confirm({
message:
"Would you like to add a link to this new entity in your sidebar?",
default: true,
});
if (addToSidebar) addLinkToSidebar(tableName);
}

if (resourceType.includes("model")) scaffoldModel(schema, driver, hasSrc);
if (resourceType.includes("api_route")) scaffoldAPIRoute(schema);
if (resourceType.includes("trpc_route")) scaffoldTRPCRoute(schema);
Expand All @@ -364,7 +375,7 @@ export async function buildSchema() {
scaffoldViewsAndComponentsWithServerActions(schema);
} else {
consola.warn(
"You need to have an ORM installed in order to use the scaffold command.",
"You need to have an ORM installed in order to use the scaffold command."
);
addPackage();
}
Expand Down

0 comments on commit aca09f8

Please sign in to comment.