Skip to content

Commit

Permalink
fixed trpc issue and initial route groups implementation done
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoalbanese committed Feb 7, 2024
1 parent 8dcbcdf commit 2e98090
Show file tree
Hide file tree
Showing 21 changed files with 521 additions and 91 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kirimase",
"version": "0.0.52",
"version": "0.0.53",
"description": "A Rails-like CLI for building full-stack Next.js apps faster",
"main": "index.js",
"type": "module",
Expand Down
6 changes: 3 additions & 3 deletions src/commands/add/auth/clerk/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const generateMiddlewareTs = () => {
// This example protects all routes including api/trpc routes
// Please edit this to allow other routes to be public as needed.
// See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your middleware
export default authMiddleware({});
export default authMiddleware({ ignoredRoutes: ["/"] });
export const config = {
matcher: ['/((?!.+\\\\\.[\\\\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
Expand All @@ -19,7 +19,7 @@ const generateSignInPageTs = () => {
export default function Page() {
return (
<main className="grid place-items-center pt-4">
<SignIn />
<SignIn redirectUrl={"/dashboard"} />
</main>
);
}`;
Expand All @@ -30,7 +30,7 @@ const generateSignUpPageTs = () => {
export default function Page() {
return (
<main className="grid place-items-center pt-4">
<SignUp />
<SignUp redirectUrl={"/dashboard"} />
</main>
);
}`;
Expand Down
15 changes: 12 additions & 3 deletions src/commands/add/auth/clerk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
updateConfigFile,
} from "../../../../utils.js";
import { addToDotEnv } from "../../orm/drizzle/generators.js";
import { addContextProviderToLayout, addToInstallList } from "../../utils.js";
import {
addContextProviderToAppLayout,
addContextProviderToAuthLayout,
addToInstallList,
} from "../../utils.js";
import { clerkGenerators } from "./generators.js";
import { formatFilePath, getFilePaths } from "../../../filePaths/index.js";
import { libAuthUtilsTs } from "../next-auth/generators.js";
Expand All @@ -28,6 +32,7 @@ export const addClerk = async () => {
clerk: { middleware, signInPage, signUpPage },
shared: {
auth: { authUtils },
init,
},
} = getFilePaths();
const {
Expand All @@ -37,7 +42,8 @@ export const addClerk = async () => {
generateSignUpPageTs,
homePageWithUserButton,
} = clerkGenerators;
addContextProviderToLayout("ClerkProvider");
addContextProviderToAuthLayout("ClerkProvider");
addContextProviderToAppLayout("ClerkProvider");
addToDotEnv(
[
{ key: "NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY", value: "", public: true },
Expand All @@ -64,7 +70,10 @@ export const addClerk = async () => {
);

replaceFile(
rootPath.concat("app/page.tsx"),
formatFilePath(init.dashboardRoute, {
removeExtension: false,
prefix: "rootPath",
}),
homePageWithUserButton(componentLib)
);

Expand Down
24 changes: 21 additions & 3 deletions src/commands/add/auth/clerk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ import fs from "fs";
import { createFile, replaceFile } from "../../../../utils.js";
import { formatFilePath, getFilePaths } from "../../../filePaths/index.js";

export const updateClerkMiddlewareForStripe = (rootPath: string) => {
// export const updateClerkMiddlewareForStripe = (rootPath: string) => {
// const { clerk } = getFilePaths();
// const initMWContent = `export default authMiddleware({});`;
// const updatedMWContent = `export default authMiddleware({ ignoredRoutes: ["/api/webhooks/stripe"] });`;
// const mwPath = formatFilePath(clerk.middleware, {
// prefix: "rootPath",
// removeExtension: false,
// });
// const mwExists = fs.existsSync(mwPath);
// if (mwExists) {
// const mwContent = fs.readFileSync(mwPath, "utf-8");
// const newUtilsContent = mwContent.replace(initMWContent, updatedMWContent);
// replaceFile(mwPath, newUtilsContent);
// } else {
// console.error("Middleware does not exist");
// }
// };

export const addToClerkIgnoredRoutes = (newPath: string) => {
const { clerk } = getFilePaths();
const initMWContent = `export default authMiddleware({});`;
const updatedMWContent = `export default authMiddleware({ ignoredRoutes: "/api/webhooks/stripe" });`;
const initMWContent = "ignoredRoutes: [";
const updatedMWContent = "ignoredRoutes: [" + ` "${newPath}", `;
const mwPath = formatFilePath(clerk.middleware, {
prefix: "rootPath",
removeExtension: false,
Expand Down
7 changes: 5 additions & 2 deletions src/commands/add/auth/kinde/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const addKinde = async () => {
);
// update root page
createFile(
formatFilePath("app/page.tsx", {
formatFilePath(shared.init.dashboardRoute, {
prefix: "rootPath",
removeExtension: false,
}),
Expand All @@ -69,7 +69,10 @@ export const addKinde = async () => {
{ key: "KINDE_ISSUER_URL", value: "https://kirimase.kinde.com" },
{ key: "KINDE_SITE_URL", value: "http://localhost:3000" },
{ key: "KINDE_POST_LOGOUT_REDIRECT_URL", value: "http://localhost:3000" },
{ key: "KINDE_POST_LOGIN_REDIRECT_URL", value: "http://localhost:3000" },
{
key: "KINDE_POST_LOGIN_REDIRECT_URL",
value: "http://localhost:3000/dashboard",
},
]);
// install @kinde-oss/kinde-auth-nextjs
// await installPackages(
Expand Down
34 changes: 2 additions & 32 deletions src/commands/add/auth/lucia/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,10 @@ const generateSignUpPage = (withShadCn: boolean) => {
prefix: "alias",
})}";
import Link from "next/link";
import { getPageSession } from "${formatFilePath(lucia.libAuthLucia, {
prefix: "alias",
removeExtension: true,
})}";
import { redirect } from "next/navigation";
import { Input } from "${alias}/components/ui/input";
import { Label } from "${alias}/components/ui/label";
const Page = async () => {
const session = await getPageSession();
if (session) redirect("/");
return (
<main className="max-w-lg mx-auto my-4 bg-secondary p-10">
<h1 className="text-2xl font-bold text-center">Create an account</h1>
Expand Down Expand Up @@ -104,15 +97,8 @@ export default Page;
prefix: "alias",
})}";
import Link from "next/link";
import { redirect } from "next/navigation";
import { getUserAuth } from "${formatFilePath(shared.auth.authUtils, {
prefix: "alias",
removeExtension: true,
})}";
const Page = async () => {
const { session } = await getUserAuth();
if (session) redirect("/");
return (
<main className="max-w-lg mx-auto my-4 bg-neutral-100 p-10">
<h1 className="text-2xl font-bold text-center">Create an account</h1>
Expand Down Expand Up @@ -168,16 +154,9 @@ const generateSignInPage = (withShadCn: boolean) => {
})}";
import { Input } from "${alias}/components/ui/input";
import { Label } from "${alias}/components/ui/label";
import { getPageSession } from "${formatFilePath(lucia.libAuthLucia, {
prefix: "alias",
removeExtension: true,
})}";
import Link from "next/link";
import { redirect } from "next/navigation";
const Page = async () => {
const session = await getPageSession();
if (session?.user) redirect("/");
return (
<main className="max-w-lg mx-auto my-4 bg-secondary p-10">
<h1 className="text-2xl font-bold text-center">
Expand Down Expand Up @@ -215,16 +194,9 @@ export default Page;
removeExtension: true,
prefix: "alias",
})}";
import { getUserAuth } from "${formatFilePath(shared.auth.authUtils, {
prefix: "alias",
removeExtension: true,
})}";
import Link from "next/link";
import { redirect } from "next/navigation";
const Page = async () => {
const { session } = await getUserAuth();
if (session?.user) redirect("/");
return (
<main className="max-w-lg mx-auto my-4 bg-neutral-100 p-10">
<h1 className="text-2xl font-bold text-center">
Expand Down Expand Up @@ -466,11 +438,9 @@ import { getUserAuth } from "${formatFilePath(shared.auth.authUtils, {
prefix: "alias",
removeExtension: true,
})}";
import { redirect } from "next/navigation";
export default async function Home() {
const { session } = await getUserAuth();
if (!session) redirect("/sign-up");
return (
<main className="">
<h1 className="text-2xl font-bold my-2">Profile</h1>
Expand Down Expand Up @@ -555,7 +525,7 @@ export const POST = async (request: NextRequest) => {
return new Response(null, {
status: 302,
headers: {
Location: "/", // redirect to profile page
Location: "/dashboard", // redirect to profile page
},
});
} catch (e) {
Expand Down Expand Up @@ -640,7 +610,7 @@ export const POST = async (request: NextRequest) => {
return new Response(null, {
status: 302,
headers: {
Location: "/", // redirect to profile page
Location: "/dashboard", // redirect to profile page
},
});
} catch (e) {
Expand Down
8 changes: 7 additions & 1 deletion src/commands/add/auth/lucia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ export const addLucia = async () => {
}),
viewsAndComponents.authFormComponent
);
replaceFile(rootPath.concat("app/page.tsx"), viewsAndComponents.homePage);
replaceFile(
formatFilePath(shared.init.dashboardRoute, {
removeExtension: false,
prefix: "rootPath",
}),
viewsAndComponents.homePage
);
createFile(
rootPath.concat("app/loading.tsx"),
viewsAndComponents.loadingPage
Expand Down
17 changes: 14 additions & 3 deletions src/commands/add/auth/next-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
libAuthUtilsTs,
} from "./generators.js";
import { AuthDriver, AuthProvider, AuthProviders } from "./utils.js";
import { addContextProviderToLayout, addToInstallList } from "../../utils.js";
import {
addContextProviderToAppLayout,
// addContextProviderToAuthLayout,
addToInstallList,
} from "../../utils.js";
import { addToDotEnv } from "../../orm/drizzle/generators.js";
import { addToPrismaSchema } from "../../../generate/utils.js";
import { prismaGenerate } from "../../orm/utils.js";
Expand Down Expand Up @@ -108,7 +112,13 @@ export const addNextAuth = async (
// 6. If trpc installed, add protectedProcedure // this wont run because it is installed before trpc
updateTrpcWithSessionIfInstalled();

replaceFile(rootPath.concat("app/page.tsx"), generateUpdatedRootRoute());
replaceFile(
formatFilePath(shared.init.dashboardRoute, {
removeExtension: false,
prefix: "rootPath",
}),
generateUpdatedRootRoute()
);

// add to env
addToDotEnv(
Expand Down Expand Up @@ -165,7 +175,8 @@ export const addNextAuth = async (
addPackageToConfig("next-auth");
updateConfigFile({ auth: "next-auth" });
// 9. Instruct user to add the <Provider /> to their root layout.
addContextProviderToLayout("NextAuthProvider");
// addContextProviderToAuthLayout("NextAuthProvider");
addContextProviderToAppLayout("NextAuthProvider");
if (orm === "prisma") await prismaGenerate(preferredPackageManager);
// consola.success("Successfully added Next Auth to your project!");
};
5 changes: 1 addition & 4 deletions src/commands/add/auth/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import {
installShadcnUIComponents,
readConfigFile,
} from "../../../../utils.js";
import {
addContextProviderToLayout,
addToShadcnComponentList,
} from "../../utils.js";
import { addToShadcnComponentList } from "../../utils.js";
import {
createAccountApiTs,
createAccountCardComponent,
Expand Down
7 changes: 4 additions & 3 deletions src/commands/add/componentLib/shadcn-ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
} from "../../../../utils.js";
import { AvailablePackage, PMType } from "../../../../types.js";
import {
addContextProviderToLayout,
addContextProviderToAppLayout,
addContextProviderToRootLayout,
addToInstallList,
addToShadcnComponentList,
} from "../../utils.js";
Expand Down Expand Up @@ -85,7 +86,7 @@ const manualInstallShadCn = async (
generateThemeToggler()
);
// add context provider to layout
addContextProviderToLayout("ThemeProvider");
addContextProviderToRootLayout("ThemeProvider");
};

export const installShadcnUI = async (
Expand Down Expand Up @@ -138,7 +139,7 @@ export const installShadcnUI = async (
"dropdown-menu",
]);

addContextProviderToLayout("ShadcnToast");
addContextProviderToAppLayout("ShadcnToast");

// if (packages.includes("next-auth")) updateSignInComponentWithShadcnUI();
};
Expand Down
17 changes: 13 additions & 4 deletions src/commands/add/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { confirm } from "@inquirer/prompts";
import {
createFile,
installPackages,
readConfigFile,
replaceFile,
Expand All @@ -23,6 +24,9 @@ import { formatFilePath, getFilePaths } from "../filePaths/index.js";
import { addKinde } from "./auth/kinde/index.js";
import { addNavbarAndSettings } from "./misc/navbar/generators.js";
import {
createAppLayoutFile,
createAuthLayoutFile,
createLandingPage,
generateGenericHomepage,
generateGlobalsCss,
generateUpdatedTWConfig,
Expand All @@ -39,7 +43,8 @@ import {
askPscale,
} from "./prompts.js";
import {
addContextProviderToLayout,
addAuthCheckToAppLayout,
addContextProviderToAppLayout,
addToInstallList,
installPackagesFromList,
installShadcnComponentList,
Expand Down Expand Up @@ -131,6 +136,9 @@ export const addPackage = async (options?: InitOptions) => {
spinner.start();
spinner.text = "Beginning Configuration Process";

createAppLayoutFile();
createLandingPage();

if (config.componentLib === undefined) {
if (promptResponse.componentLib === "shadcn-ui") {
spinner.text = "Configuring Shadcn-UI";
Expand Down Expand Up @@ -159,7 +167,7 @@ export const addPackage = async (options?: InitOptions) => {
updateConfigFile({ componentLib: null });
}
if (!config.t3) {
addContextProviderToLayout("Navbar");
addContextProviderToAppLayout("Navbar");
}
}

Expand Down Expand Up @@ -194,15 +202,15 @@ export const addPackage = async (options?: InitOptions) => {
"Configuring " +
promptResponse.auth[0].toUpperCase() +
promptResponse.orm.slice(1);

if (promptResponse.auth) createAuthLayoutFile();
if (promptResponse.auth === "next-auth")
await addNextAuth(promptResponse.authProviders, options);
if (promptResponse.auth === "clerk") await addClerk();
if (promptResponse.auth === "lucia") await addLucia();
if (promptResponse.auth === "kinde") await addKinde();
if (!promptResponse.auth) {
replaceFile(
formatFilePath("app/page.tsx", {
formatFilePath(shared.init.dashboardRoute, {
prefix: "rootPath",
removeExtension: false,
}),
Expand All @@ -214,6 +222,7 @@ export const addPackage = async (options?: InitOptions) => {
await createAccountSettingsPage();
}
addNavbarAndSettings();
addAuthCheckToAppLayout();
}

// check if misc
Expand Down
Loading

0 comments on commit 2e98090

Please sign in to comment.