Skip to content

Commit

Permalink
Cleanup & Misc fixes (#28)
Browse files Browse the repository at this point in the history
* Cleanup

* Update stuff

* Formatting

* Fix github credentials
  • Loading branch information
natesawant authored Jun 20, 2024
1 parent a807621 commit 5df5d14
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 203 deletions.
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
"parse-github-url": "^1.0.2",
"prisma": "^5.14.0"
},
"version": "0.0.1"
"version": "0.0.2"
}
3 changes: 3 additions & 0 deletions src/app/_components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const Navbar: React.FC = () => {
<Link href="/">
<button className="hover:underline font-bold">Home</button>
</Link>
<Link href="/cli">
<button className="hover:underline font-bold">CLI</button>
</Link>
<Link href="/new-project">
<button className="hover:underline">New Project</button>
</Link>
Expand Down
7 changes: 6 additions & 1 deletion src/app/api/github/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export async function POST(request: NextRequest) {
const branch = ref.split("/")[-1];
const { owner, name } = repository;
if (branch) {
await PushSchemaFromBranch(branch, owner, name);
await PushSchemaFromBranch(
`https://github.com/${owner}/${name}`,
branch,
owner,
name,
);
return NextResponse.json({ status: 200 });
} else {
return NextResponse.json({ status: 500 });
Expand Down
31 changes: 31 additions & 0 deletions src/app/cli/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getServerAuthSession } from "~/server/auth";
import { api } from "~/trpc/server";
import SignInScreen from "../_components/SignInScreen";
import Navbar from "../_components/NavBar";

export default async function CLIInfo() {
const session = await getServerAuthSession();
const token = await api.token();

return session ? (
<>
<Navbar />
<div className="min-h-screen min-x-screen">
<div className="px-4 sm:px-12 md:px-24 lg:px-48 py-12 min-x-screen min-h-screen bg-gradient-to-b from-generate-dark to-black from-33 text-white">
<div className=" text-lg">How to run:</div>
<div>
<div className=" text-white">npx generate-devdb</div>
<div className=" text-white">bunx generate-devdb</div>
</div>
<div className=" text-lg">
Token:{" "}
{token ? token.sessionToken : "You aren't supposed to see this"}
</div>
</div>
</div>
</>
) : (
<SignInScreen />
);
//return <>Token: {token ? token.sessionToken : "You aren't supposed to see this"}</>;
}
24 changes: 0 additions & 24 deletions src/app/dummy/page.tsx

This file was deleted.

20 changes: 15 additions & 5 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { gitHubRouter } from "./routers/github-router";
import { createCallerFactory, createTRPCRouter } from "~/server/api/trpc";
import {
createCallerFactory,
createTRPCRouter,
protectedProcedure,
} from "~/server/api/trpc";
import { greeting } from "./routers/greeting";
import { project } from "./routers/project";
import { githubWebhookRouter } from "./routers/prisma";
import { dummy } from "./routers/dummy";

/**
Expand All @@ -11,11 +13,19 @@ import { dummy } from "./routers/dummy";
* All routers added in /api/routers should be manually added here.
*/
export const appRouter = createTRPCRouter({
github: gitHubRouter,
health: greeting,
database: project,
webhook: githubWebhookRouter,
dummy: dummy,
token: protectedProcedure.query(({ ctx }) => {
return ctx.db.session.findFirst({
select: {
sessionToken: true,
},
where: {
userId: ctx.session.user.id,
},
});
}),
});

// export type definition of API
Expand Down
13 changes: 10 additions & 3 deletions src/server/api/routers/dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";

export const dummy = createTRPCRouter({
testDummy: publicProcedure
.input(z.object({ name: z.string() }))
.mutation(async () => {
const results = await dummyCreate();
.input(
z.object({
repository: z.string(),
branch: z.string(),
models: z.array(z.object({ name: z.string(), count: z.number() })),
}),
)
.mutation(async ({ input }) => {
const { repository, branch, models } = input;
const results = await dummyCreate(repository, branch, models);

return results;
}),
Expand Down
85 changes: 0 additions & 85 deletions src/server/api/routers/github-router.ts

This file was deleted.

63 changes: 0 additions & 63 deletions src/server/api/routers/prisma.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/server/api/routers/project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gitUrlParse from "git-url-parse";
import { CreateWebhook } from "../../external/github";
import { waitUntil } from "async-wait-until";
import { z } from "zod";

Expand All @@ -12,7 +13,6 @@ import {
StopRDSInstance,
} from "~/server/external/aws";
import { DBProvider } from "~/server/external/types";
import { PushPrismaSchema } from "~/app/api/github/utils";
import { PushSchemaFromBranch } from "~/server/prisma/schema";

export const project = {
Expand Down Expand Up @@ -108,6 +108,7 @@ export const project = {
});

const result = await CreateRDSInstance(id, input.provider);
await CreateWebhook(href);

// Wait until the database is available to then push the schema
waitUntil(
Expand All @@ -117,7 +118,7 @@ export const project = {
)
.then(async (matching) => {
console.log(matching);
await PushSchemaFromBranch(branch, owner, name);
await PushSchemaFromBranch(input.repoUrl, branch, owner, name);
})
.catch((reason) => {
console.error(reason);
Expand Down
27 changes: 22 additions & 5 deletions src/server/dummyData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PrismaClient } from "@prisma/client";
import OpenAI from "openai";
import { readFileSync } from "fs";
import gitUrlParse from "git-url-parse";
import { GetSchemaContents } from "./external/github";

const prisma = new PrismaClient();

Expand Down Expand Up @@ -115,9 +116,21 @@ async function tryCreateDataWithRetry(
}
}

async function dummyCreate(): Promise<{ message: string }> {
async function dummyCreate(
repository: string,
branch: string,
modelsToGenerate: { name: string; count: number }[],
): Promise<{ message: string }> {
const parsedUrl = gitUrlParse(repository);
const { owner, name } = parsedUrl;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { data } = await GetSchemaContents(repository, branch);

const schema = data as unknown as string;

const allNames = modelsToGenerate.map((info) => info.name);

console.log("Creating dummy data...");
const schema = readFileSync("./prisma/schema.prisma", "utf8");
const models = schema.match(/model \w+ {[^}]+}/g);
console.log(models);

Expand All @@ -132,8 +145,12 @@ async function dummyCreate(): Promise<{ message: string }> {

console.log(fields);

if (modelName && fields) {
await tryCreateDataWithRetry(modelName, fields, schema, 5);
if (modelName && modelName in allNames && fields) {
const info = modelsToGenerate.find(
(info) => info.name === modelName,
) ?? { name: "none", count: 0 };
for (let i = 0; i < info?.count; i++)
await tryCreateDataWithRetry(modelName, fields, schema, 5);
}
}
}
Expand Down
Loading

0 comments on commit 5df5d14

Please sign in to comment.