|
1 | 1 | import { add, complete, cycle, suite } from 'benny'; |
2 | 2 | import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'node:fs'; |
3 | | -import { join } from 'node:path'; |
| 3 | +import { join, dirname } from 'node:path'; |
4 | 4 | import { writePreviewGraph } from './graph'; |
5 | 5 | import { getRegisteredBenchmarks } from './register'; |
6 | 6 | import type { BenchmarkCase, BenchmarkResult } from './types'; |
7 | 7 |
|
8 | | -const DOCS_DIR = join(__dirname, '../../docs'); |
| 8 | +/** |
| 9 | + * A getDirname that works in CJS and ESM, since this file is directly shared |
| 10 | + * across both kinds of projects. |
| 11 | + * @TODO We can remove this once we've migrated all consumers to ESM. |
| 12 | + * |
| 13 | + * @see https://stackoverflow.com/a/79251101/13503626 |
| 14 | + */ |
| 15 | +function pathFromStack() { |
| 16 | + const { stack } = new Error(); |
| 17 | + if (!stack) { |
| 18 | + throw new Error('Could not get stack'); |
| 19 | + } |
| 20 | + const lines = stack.split('\n'); |
| 21 | + for (const line of lines) { |
| 22 | + if (line.includes(' (/') || line.includes(' (file://')) { |
| 23 | + // assumes UNIX-like paths |
| 24 | + const location = line.split(' (')[1].replace('file://', ''); |
| 25 | + const filepath = location.split(':')[0]; |
| 26 | + const dirpath = dirname(filepath); |
| 27 | + return { dirpath, filepath }; |
| 28 | + } |
| 29 | + } |
| 30 | + throw new Error('Could not get dirname'); |
| 31 | +} |
| 32 | +const DOCS_DIR = join(pathFromStack().dirpath, '../../docs'); |
9 | 33 | const RUNTIME = process.env.RUNTIME || 'node'; |
10 | 34 | const RUNTIME_VERSION = process.env.RUNTIME_VERSION || process.version; |
11 | 35 | const RUNTIME_FOR_PREVIEW = 'node'; |
|
0 commit comments