-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathindex.js
66 lines (53 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { promises as fs } from 'node:fs';
import { fork } from 'node:child_process';
import { URL, fileURLToPath } from 'node:url';
import { once } from 'node:events';
import { extname } from 'node:path';
async function getPathChoices() {
const dirents = await fs.readdir(new URL('.', import.meta.url), {
withFileTypes: true
});
const choices = [];
for (const dirent of dirents) {
if (!dirent.isDirectory()) continue;
choices.push(dirent.name);
}
return choices;
}
const argv = hideBin(process.argv);
async function getName() {
return yargs(argv)
.option('name', {
demandOption: true,
choices: await getPathChoices()
})
.parseSync().name;
}
const runnerPath = fileURLToPath(new URL('runner.js', import.meta.url)),
path = new URL(`${await getName()}/`, import.meta.url);
async function getMetadata() {
try {
return await import(new URL('index.js', path));
} catch (err) {
if (err.code === 'ERR_MODULE_NOT_FOUND') return;
throw err;
}
}
const metadata = await getMetadata(),
timestamp = Date.now();
for (const file of await fs.readdir(path)) {
if (file === 'index.js' || extname(file) !== '.js') continue;
const benchmarkProcess = fork(runnerPath, [
...argv,
'--path',
fileURLToPath(path) + file
]);
await once(benchmarkProcess, 'message');
benchmarkProcess.send({
metadata,
timestamp
});
await once(benchmarkProcess, 'close');
}