( cwd: string )
| 36 | ); |
| 37 | |
| 38 | export const findEntrypoint = async ( |
| 39 | cwd: string |
| 40 | ): Promise<string | undefined> => { |
| 41 | let packageJsonObject: { |
| 42 | main?: string; |
| 43 | dependencies?: Record<string, string>; |
| 44 | } | null = null; |
| 45 | try { |
| 46 | const packageJson = await readFile(join(cwd, 'package.json'), 'utf-8'); |
| 47 | packageJsonObject = JSON.parse(packageJson); |
| 48 | } catch (_) { |
| 49 | // ignore |
| 50 | } |
| 51 | |
| 52 | if (packageJsonObject) { |
| 53 | const main = |
| 54 | typeof packageJsonObject.main === 'string' |
| 55 | ? packageJsonObject.main.trim() |
| 56 | : ''; |
| 57 | if (main) { |
| 58 | const abs = resolve(cwd, main); |
| 59 | const rel = relative(cwd, abs); |
| 60 | if (!rel.startsWith('..') && rel !== '') { |
| 61 | try { |
| 62 | await readFile(abs, 'utf-8'); |
| 63 | return rel.split(sep).join('/'); |
| 64 | } catch { |
| 65 | // main missing or unreadable; fall through to filename heuristics |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | let framework: string | undefined; |
| 72 | if (packageJsonObject) { |
| 73 | framework = frameworks.find( |
| 74 | framework => packageJsonObject!.dependencies?.[framework] |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | if (!framework) { |
| 79 | for (const entrypoint of entrypoints) { |
| 80 | const entrypointPath = join(cwd, entrypoint); |
| 81 | try { |
| 82 | await readFile(entrypointPath, 'utf-8'); |
| 83 | return entrypoint; |
| 84 | } catch (_) { |
| 85 | // ignore |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | const regex = framework ? createFrameworkRegex(framework) : undefined; |
| 91 | |
| 92 | for (const entrypoint of entrypoints) { |
| 93 | const entrypointPath = join(cwd, entrypoint); |
| 94 | try { |
| 95 | const content = await readFile(entrypointPath, 'utf-8'); |
no test coverage detected