|
1 | 1 | import { mkdir, rename } from 'node:fs/promises'; |
2 | | -import { basename, resolve } from 'node:path'; |
| 2 | +import { resolve } from 'node:path'; |
3 | 3 | import { existAsync, rootFolder } from './utils.mjs'; |
| 4 | +import { readdir } from 'node:fs/promises'; |
| 5 | + |
| 6 | +const artifactsDir = './temp-reports'; |
| 7 | +const benchFile = process.env.BENCH_FILE; |
4 | 8 |
|
5 | 9 | /** |
6 | | - * The object result with the artifacts names |
7 | | - * The result will be a record with the possible states: |
8 | | - * |
9 | | - * ```js |
10 | | - * // when is called by bench.yml |
11 | | - * { '18.18.0': 'result-1-18.18.0-add-property.js.md' } |
12 | | - * // when is called by watch_bench.yml |
13 | | - * { 'add-property.js:18.18.0': 'result-1-18.18.0-add-property.js.md' } |
14 | | - * ``` |
15 | | - * |
16 | | - * @type {{ result: { [nodeVersion: string]: string } }} |
| 10 | + * Get all benchmark report files from the temp-reports directory |
| 11 | + * Parse the node version from the filename format: report-runid-nodeversion-benchfile.md |
| 12 | + * @returns {Promise<{ [nodeVersion: string]: string }>} Map of node version to report filename |
17 | 13 | */ |
18 | | -const benchResult = JSON.parse(process.env.BENCH_ARTIFACTS); |
19 | | - |
20 | | -function getBenchmarkFile(key) { |
21 | | - const filepath = process.env.BENCH_FILE || key.split(':')[0]; |
22 | | - |
23 | | - return basename(filepath, '.mjs'); |
24 | | -} |
25 | | - |
26 | | -function getBenchmarkNodeVersion(key) { |
27 | | - return process.env.BENCH_FILE ? key : key.split(':')[1]; |
| 14 | +async function getBenchmarkFiles() { |
| 15 | + const files = await readdir(artifactsDir); |
| 16 | + const benchFiles = {}; |
| 17 | + |
| 18 | + for (const file of files) { |
| 19 | + // Only process files related to the current benchmark |
| 20 | + if (file.includes(benchFile)) { |
| 21 | + // Extract the node version from the filename pattern: report-runid-nodeversion-benchfile.md |
| 22 | + const parts = file.split('-'); |
| 23 | + // The node version should be the third part (index 2) in the filename |
| 24 | + if (parts.length >= 3) { |
| 25 | + const nodeVersion = parts[2]; |
| 26 | + benchFiles[nodeVersion] = file; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return benchFiles; |
28 | 32 | } |
29 | 33 |
|
30 | | -for (const key of Object.keys(benchResult.result)) { |
31 | | - const benchFile = getBenchmarkFile(key); |
32 | | - const nodeVersion = getBenchmarkNodeVersion(key).replace(/\./g, '_'); |
33 | | - |
34 | | - const major = nodeVersion.split('_')[0]; |
35 | | - const reportFilepath = benchResult.result[key]; |
36 | | - |
37 | | - const outputFolder = resolve(rootFolder, `./v${major}/v${nodeVersion}`); |
38 | | - |
39 | | - const outputFolderExist = await existAsync(outputFolder); |
40 | | - |
41 | | - if (!outputFolderExist) { |
42 | | - await mkdir(outputFolder, { |
43 | | - recursive: true, |
44 | | - }); |
| 34 | +async function processBenchmarks() { |
| 35 | + const benchFiles = await getBenchmarkFiles(); |
| 36 | + |
| 37 | + for (const nodeVersion of Object.keys(benchFiles)) { |
| 38 | + const benchmarkFile = benchFile; |
| 39 | + const reportFilepath = resolve(artifactsDir, benchFiles[nodeVersion]); |
| 40 | + |
| 41 | + const normalizedNodeVersion = nodeVersion.replace(/\./g, '_'); |
| 42 | + const major = normalizedNodeVersion.split('_')[0]; |
| 43 | + const outputFolder = resolve(rootFolder, `./v${major}/v${normalizedNodeVersion}`); |
| 44 | + |
| 45 | + if (!await existAsync(outputFolder)) { |
| 46 | + await mkdir(outputFolder, { recursive: true }); |
| 47 | + } |
| 48 | + |
| 49 | + const outputFilepath = resolve(outputFolder, benchmarkFile.replace('.mjs', '.md')); |
| 50 | + await rename(reportFilepath, outputFilepath); |
| 51 | + console.log(`Moved ${benchmarkFile} report for Node.js v${nodeVersion} to ${outputFilepath}`); |
45 | 52 | } |
46 | | - |
47 | | - await rename(`./temp-reports/${reportFilepath}`, `${outputFolder}/${benchFile}.md`, 'utf-8'); |
48 | 53 | } |
| 54 | + |
| 55 | +processBenchmarks().catch(console.error); |
0 commit comments