(fullPath: string)
| 2 | import path from 'node:path'; |
| 3 | |
| 4 | const isFileAnEmail = async (fullPath: string): Promise<boolean> => { |
| 5 | let fileHandle: fs.promises.FileHandle; |
| 6 | try { |
| 7 | fileHandle = await fs.promises.open(fullPath, 'r'); |
| 8 | } catch (exception) { |
| 9 | console.warn(exception); |
| 10 | return false; |
| 11 | } |
| 12 | const stat = await fileHandle.stat(); |
| 13 | |
| 14 | if (stat.isDirectory()) { |
| 15 | await fileHandle.close(); |
| 16 | return false; |
| 17 | } |
| 18 | |
| 19 | const { ext } = path.parse(fullPath); |
| 20 | |
| 21 | if (ext === '.html') { |
| 22 | await fileHandle.close(); |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | if (!['.js', '.tsx', '.jsx'].includes(ext)) { |
| 27 | await fileHandle.close(); |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | // check with a heuristic to see if the file has at least |
| 32 | // a default export (ES6) or module.exports (CommonJS) or named exports (MDX) |
| 33 | const fileContents = await fileHandle.readFile('utf8'); |
| 34 | |
| 35 | await fileHandle.close(); |
| 36 | |
| 37 | // Check for ES6 export default syntax |
| 38 | const hasES6DefaultExport = /\bexport\s+default\b/gm.test(fileContents); |
| 39 | |
| 40 | // Check for CommonJS module.exports syntax |
| 41 | const hasCommonJSExport = /\bmodule\.exports\s*=/gm.test(fileContents); |
| 42 | |
| 43 | // Check for named exports (used in MDX files) and ensure at least one is marked as default |
| 44 | const hasNamedExport = /\bexport\s+\{[^}]*\bdefault\b[^}]*\}/gm.test( |
| 45 | fileContents, |
| 46 | ); |
| 47 | |
| 48 | return hasES6DefaultExport || hasCommonJSExport || hasNamedExport; |
| 49 | }; |
| 50 | |
| 51 | export interface EmailsDirectory { |
| 52 | absolutePath: string; |
no test coverage detected