diff --git a/packages/preview-utils/index.ts b/packages/preview-utils/index.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/packages/preview-utils/src/index.ts b/packages/preview-utils/src/index.ts index 1ebf27f9fd..3286afe1aa 100644 --- a/packages/preview-utils/src/index.ts +++ b/packages/preview-utils/src/index.ts @@ -2,4 +2,5 @@ export * from './render-email-by-path'; export * from './get-email-component'; export * from './utils/email-template'; export * from './utils/rendering-utilities-exporter'; +export * from './is-file-an-email'; diff --git a/packages/preview-utils/src/is-file-an-email.ts b/packages/preview-utils/src/is-file-an-email.ts new file mode 100644 index 0000000000..212c59a362 --- /dev/null +++ b/packages/preview-utils/src/is-file-an-email.ts @@ -0,0 +1,25 @@ +import fs from 'node:fs'; +import path from 'node:path'; + +export const isFileAnEmail = (fullPath: string): boolean => { + const stat = fs.statSync(fullPath); + + if (stat.isDirectory()) return false; + + const { ext } = path.parse(fullPath); + + if (!['.js', '.tsx', '.jsx'].includes(ext)) return false; + + // This is to avoid a possible race condition where the file doesn't exist anymore + // once we are checking if it is an actual email, this couuld cause issues that + // would be very hard to debug and find out the why of it happening. + if (!fs.existsSync(fullPath)) { + return false; + } + + // check with a heuristic to see if the file has at least + // a default export + const fileContents = fs.readFileSync(fullPath, 'utf8'); + + return /\bexport\s+default\b/gm.test(fileContents); +}; diff --git a/packages/react-email/src/actions/get-emails-directory-metadata.ts b/packages/react-email/src/actions/get-emails-directory-metadata.ts index edf3fda702..6234a2ba97 100644 --- a/packages/react-email/src/actions/get-emails-directory-metadata.ts +++ b/packages/react-email/src/actions/get-emails-directory-metadata.ts @@ -2,29 +2,7 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ import fs from 'node:fs'; import path from 'node:path'; - -const isFileAnEmail = (fullPath: string): boolean => { - const stat = fs.statSync(fullPath); - - if (stat.isDirectory()) return false; - - const { ext } = path.parse(fullPath); - - if (!['.js', '.tsx', '.jsx'].includes(ext)) return false; - - // This is to avoid a possible race condition where the file doesn't exist anymore - // once we are checking if it is an actual email, this couuld cause issues that - // would be very hard to debug and find out the why of it happening. - if (!fs.existsSync(fullPath)) { - return false; - } - - // check with a heuristic to see if the file has at least - // a default export - const fileContents = fs.readFileSync(fullPath, 'utf8'); - - return /\bexport\s+default\b/gm.test(fileContents); -}; +import { isFileAnEmail } from 'preview-utils'; export interface EmailsDirectory { absolutePath: string;