-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move isFileAnEmail to the preview-utils
- Loading branch information
1 parent
d0e896c
commit 4f15bef
Showing
4 changed files
with
27 additions
and
23 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters