From aae3945843b61f327c899948ffc832cae2d93ca9 Mon Sep 17 00:00:00 2001 From: josefaidt Date: Sun, 9 Apr 2023 10:07:20 -0700 Subject: [PATCH 1/3] create prisma-scripts, fix circular dependency --- apps/bot.amplify.aws/src/lib/db.ts | 3 +++ package.json | 2 +- packages/prisma-client/package.json | 4 ---- packages/prisma-client/scripts/package.json | 3 --- .../scripts => prisma-scripts}/init.ts | 2 +- packages/prisma-scripts/package.json | 19 +++++++++++++++++++ .../scripts => prisma-scripts}/seed.ts | 4 ++-- pnpm-lock.yaml | 18 +++++++++++------- 8 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 apps/bot.amplify.aws/src/lib/db.ts delete mode 100644 packages/prisma-client/scripts/package.json rename packages/{prisma-client/scripts => prisma-scripts}/init.ts (96%) create mode 100644 packages/prisma-scripts/package.json rename packages/{prisma-client/scripts => prisma-scripts}/seed.ts (97%) diff --git a/apps/bot.amplify.aws/src/lib/db.ts b/apps/bot.amplify.aws/src/lib/db.ts new file mode 100644 index 00000000..d5b9bfe1 --- /dev/null +++ b/apps/bot.amplify.aws/src/lib/db.ts @@ -0,0 +1,3 @@ +import { PrismaClient } from '@hey-amplify/prisma-client' + +export const prisma = new PrismaClient() diff --git a/package.json b/package.json index 5544cf85..d4a8b6ec 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ }, "prisma": { "schema": "./packages/prisma-client/schema.prisma", - "seed": "vite-node ./packages/prisma-client/scripts/init.ts" + "seed": "vite-node ./packages/prisma-scripts/init.ts" }, "prettier": { "tabWidth": 2, diff --git a/packages/prisma-client/package.json b/packages/prisma-client/package.json index e5382d0c..fad92b26 100644 --- a/packages/prisma-client/package.json +++ b/packages/prisma-client/package.json @@ -20,10 +20,6 @@ "@prisma/client": "^4.12.0" }, "devDependencies": { - "@faker-js/faker": "^7.6.0", - "@hey-amplify/constants": "workspace:*", - "@hey-amplify/discord": "workspace:*", - "@hey-amplify/features": "workspace:*", "prisma": "^4.12.0" } } diff --git a/packages/prisma-client/scripts/package.json b/packages/prisma-client/scripts/package.json deleted file mode 100644 index 3dbc1ca5..00000000 --- a/packages/prisma-client/scripts/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/packages/prisma-client/scripts/init.ts b/packages/prisma-scripts/init.ts similarity index 96% rename from packages/prisma-client/scripts/init.ts rename to packages/prisma-scripts/init.ts index fc3583e1..a82c0d7e 100644 --- a/packages/prisma-client/scripts/init.ts +++ b/packages/prisma-scripts/init.ts @@ -1,4 +1,4 @@ -import { PrismaClient } from '../build/generated/client/index.js' +import { PrismaClient } from '@hey-amplify/prisma-client' import { ACCESS_LEVELS, FEATURE_TYPES } from '@hey-amplify/constants' import { integrations, types as featureTypes } from '@hey-amplify/features' import { createCommandFeatures } from '@hey-amplify/discord' diff --git a/packages/prisma-scripts/package.json b/packages/prisma-scripts/package.json new file mode 100644 index 00000000..2dffeefb --- /dev/null +++ b/packages/prisma-scripts/package.json @@ -0,0 +1,19 @@ +{ + "name": "@hey-amplify/prisma-scripts", + "private": true, + "type": "module", + "version": "1.0.0", + "exports": { + "./package.json": "./package.json" + }, + "scripts": { + "build": "echo 'no build needed'" + }, + "devDependencies": { + "@faker-js/faker": "^7.6.0", + "@hey-amplify/constants": "workspace:*", + "@hey-amplify/discord": "workspace:*", + "@hey-amplify/features": "workspace:*", + "@hey-amplify/prisma-client": "workspace:*" + } +} diff --git a/packages/prisma-client/scripts/seed.ts b/packages/prisma-scripts/seed.ts similarity index 97% rename from packages/prisma-client/scripts/seed.ts rename to packages/prisma-scripts/seed.ts index 1ef1e9f6..30f5d155 100644 --- a/packages/prisma-client/scripts/seed.ts +++ b/packages/prisma-scripts/seed.ts @@ -1,11 +1,11 @@ import { faker } from '@faker-js/faker' import { ACCESS_LEVELS } from '@hey-amplify/constants' -import { PrismaClient } from '../build/generated/client/index.js' +import { PrismaClient } from '@hey-amplify/prisma-client' import type { DiscordUser, DiscordRole, Prisma, -} from '../build/generated/client/index.js' +} from '@hey-amplify/prisma-client' const prisma = new PrismaClient() diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5baab7e1..f11f52d7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -264,22 +264,26 @@ importers: packages/prisma-client: specifiers: - '@faker-js/faker': ^7.6.0 - '@hey-amplify/constants': workspace:* - '@hey-amplify/discord': workspace:* - '@hey-amplify/features': workspace:* '@prisma/client': ^4.12.0 prisma: ^4.12.0 - vite-node: ^0.29.8 dependencies: '@prisma/client': 4.12.0_prisma@4.12.0 + devDependencies: + prisma: 4.12.0 + + packages/prisma-scripts: + specifiers: + '@faker-js/faker': ^7.6.0 + '@hey-amplify/constants': workspace:* + '@hey-amplify/discord': workspace:* + '@hey-amplify/features': workspace:* + '@hey-amplify/prisma-client': workspace:* devDependencies: '@faker-js/faker': 7.6.0 '@hey-amplify/constants': link:../constants '@hey-amplify/discord': link:../discord '@hey-amplify/features': link:../features - prisma: 4.12.0 - vite-node: 0.29.8 + '@hey-amplify/prisma-client': link:../prisma-client packages/scripts: specifiers: From b24afee773ccc7ea436e6a9106883b86b098fbb8 Mon Sep 17 00:00:00 2001 From: josefaidt Date: Sun, 9 Apr 2023 10:22:50 -0700 Subject: [PATCH 2/3] add bot test scaffolding --- apps/discord-bot/tests/discord-bot.test.ts | 21 ++++++++++++++++++++- apps/discord-bot/tsconfig.json | 3 ++- apps/discord-bot/vitest.config.ts | 8 ++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 apps/discord-bot/vitest.config.ts diff --git a/apps/discord-bot/tests/discord-bot.test.ts b/apps/discord-bot/tests/discord-bot.test.ts index fb33626e..7e2fb9af 100644 --- a/apps/discord-bot/tests/discord-bot.test.ts +++ b/apps/discord-bot/tests/discord-bot.test.ts @@ -1 +1,20 @@ -test.todo('discord-bot') +import { Events, Message } from 'discord.js' +import { createBot } from '@hey-amplify/discord' +import type { Client } from 'discord.js' + +let bot: Client +beforeAll(async () => { + bot = await createBot() +}) + +afterAll(() => { + bot?.destroy() +}) + +describe('discord-bot', () => { + // TODO: add tests + it('should respond', async () => { + // bot.emit(Events.MessageCreate, { + // }) + }) +}) diff --git a/apps/discord-bot/tsconfig.json b/apps/discord-bot/tsconfig.json index aa333880..87c1013b 100644 --- a/apps/discord-bot/tsconfig.json +++ b/apps/discord-bot/tsconfig.json @@ -2,5 +2,6 @@ "extends": "../../tsconfig.json", "compilerOptions": { "resolveJsonModule": true - } + }, + "include": ["env.d.ts", "discord-bot.ts", "tests/**/*.ts"] } diff --git a/apps/discord-bot/vitest.config.ts b/apps/discord-bot/vitest.config.ts new file mode 100644 index 00000000..f305a750 --- /dev/null +++ b/apps/discord-bot/vitest.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + include: ['tests/*.test.ts'], + globals: true, + }, +}) From cd193e549255d0109cf38cccd51def7dca8bb505 Mon Sep 17 00:00:00 2001 From: josefaidt Date: Sun, 9 Apr 2023 10:23:46 -0700 Subject: [PATCH 3/3] createBot to return client --- packages/discord/src/client.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/discord/src/client.ts b/packages/discord/src/client.ts index 6e3334bb..b18a44a4 100644 --- a/packages/discord/src/client.ts +++ b/packages/discord/src/client.ts @@ -326,8 +326,9 @@ client.on(Events.ThreadUpdate, async (thread) => { console.log(`Thread ${thread.id} updated`) }) -export function createBot(token = process.env.DISCORD_BOT_TOKEN) { - return client.login(token) +export async function createBot(token = process.env.DISCORD_BOT_TOKEN) { + await client.login(token) + return client } // capture SIGINT (Ctrl+C) to gracefully shutdown