From bddf64e32cfe1212273f936b1be859362c95d6d4 Mon Sep 17 00:00:00 2001 From: Tomasz Magulski Date: Fri, 27 May 2022 13:10:12 +0200 Subject: [PATCH] Infer webhooks in production build --- src/index.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index a274847..237df05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,22 +1,37 @@ -import path from 'path'; -import fg from 'fast-glob'; +import path from "path"; +import fg from "fast-glob"; import { print } from "graphql/language/printer.js"; -export const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slice(1); -export const dropFileExtension = (filename: string) => path.parse(filename).name; +const capitalize = (value: string) => + value.charAt(0).toUpperCase() + value.slice(1); + +const dropFileExtension = (filename: string) => path.parse(filename).name; export const inferWebhooks = async (baseURL: string, generatedGraphQL: any) => { - const entries = await fg(["*.ts"], { cwd: "pages/api/webhooks" }); + let entries; + if (process.env.NODE_ENV === "production") { + entries = await fg(["*.js"], { cwd: `${__dirname}/webhooks` }); + } else { + entries = await fg(["*.ts"], { cwd: `pages/api/webhooks` }); + } return entries.map(dropFileExtension).map((name: string) => { const camelcaseName = name.split("-").map(capitalize).join(""); const eventName = name.toUpperCase().replace(new RegExp("-", "g"), "_"); let eventType: string; - if (Object.values(generatedGraphQL.WebhookEventTypeAsyncEnum).includes(eventName)) { + if ( + Object.values(generatedGraphQL.WebhookEventTypeAsyncEnum).includes( + eventName + ) + ) { eventType = "asyncEvents"; - } else if (Object.values(generatedGraphQL.WebhookEventTypeSyncEnum).includes(eventName)) { + } else if ( + Object.values(generatedGraphQL.WebhookEventTypeSyncEnum).includes( + eventName + ) + ) { eventType = "syncEvents"; } else { throw Error("Event type not found."); @@ -37,4 +52,4 @@ export const inferWebhooks = async (baseURL: string, generatedGraphQL: any) => { targetUrl: `${baseURL}/api/webhooks/${name}`, }; }); -}; \ No newline at end of file +};