Skip to content

Commit

Permalink
move isFileAnEmail to the preview-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmfern committed Dec 19, 2024
1 parent d0e896c commit 4f15bef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
Empty file removed packages/preview-utils/index.ts
Empty file.
1 change: 1 addition & 0 deletions packages/preview-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

25 changes: 25 additions & 0 deletions packages/preview-utils/src/is-file-an-email.ts
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4f15bef

Please sign in to comment.