UNPKG

1.14 kBJavaScriptView Raw
1#!/usr/bin/env node
2import { promises as fs } from 'fs';
3import { basename, join } from 'path';
4import { pathToFileURL } from 'url';
5async function* walk(dir) {
6 for await (const d of await fs.opendir(dir)) {
7 const entry = join(dir, d.name);
8 if (d.isDirectory() && d.name !== 'node_modules')
9 yield* walk(entry);
10 else if (d.isFile())
11 yield entry;
12 }
13}
14async function runTestFile(file) {
15 for (const value of Object.values(await import(pathToFileURL(file).toString()))) {
16 if (typeof value === 'function') {
17 try {
18 await value();
19 }
20 catch (e) {
21 console.error(e instanceof Error ? e.stack : e);
22 process.exit(1);
23 }
24 }
25 }
26}
27async function run(arg = '.') {
28 if ((await fs.lstat(arg)).isFile()) {
29 return runTestFile(arg);
30 }
31 for await (const file of walk(arg)) {
32 if (basename(file) === 'test.js' ||
33 file.endsWith('.test.js')) {
34 console.log(file);
35 await runTestFile(file);
36 }
37 }
38}
39void run(process.argv[2]);