1 | #!/usr/bin/env node
|
2 | import { dirname, resolve } from 'node:path';
|
3 | import { fileURLToPath, pathToFileURL } from 'node:url';
|
4 | import process from 'node:process';
|
5 | import { promisify } from 'node:util';
|
6 | import { createReadStream, readFile as baseReadFile } from 'node:fs';
|
7 |
|
8 | const readFile = promisify(baseReadFile);
|
9 |
|
10 | import arg from 'arg';
|
11 | import { globby } from 'globby';
|
12 | import {
|
13 | createDiffReporter,
|
14 | createJSONReporter,
|
15 | createTAPReporter,
|
16 | } from 'zora-reporters';
|
17 |
|
18 | const __filename = fileURLToPath(import.meta.url);
|
19 | const __dirname = dirname(__filename);
|
20 |
|
21 | const reporterMap = {
|
22 | diff: createDiffReporter(),
|
23 | tap: createTAPReporter(),
|
24 | json: createJSONReporter(),
|
25 | log: createJSONReporter(),
|
26 | };
|
27 |
|
28 | const DEFAULT_FILE_PATTERNS = [
|
29 | '**/test.js',
|
30 | '**/*.spec.js',
|
31 | '**/*.test.js',
|
32 | '**/test/**/*.js',
|
33 | '**/tests/**/*.js',
|
34 | '**/__tests__/**/*.js',
|
35 | '!**/node_modules',
|
36 | '!node_modules',
|
37 | ];
|
38 |
|
39 | const {
|
40 | ['--reporter']: reporter = 'diff',
|
41 | ['--only']: only = false,
|
42 | ['--help']: help = false,
|
43 | _: filePatterns,
|
44 | } = arg({
|
45 | ['--reporter']: String,
|
46 | ['--only']: Boolean,
|
47 | ['--help']: Boolean,
|
48 |
|
49 |
|
50 | ['--module-loader']: String,
|
51 | ['-R']: '--reporter',
|
52 | });
|
53 |
|
54 | (async () => {
|
55 | if (help) {
|
56 | createReadStream(resolve(__dirname, './usage.txt')).pipe(process.stdout);
|
57 | return;
|
58 | }
|
59 |
|
60 |
|
61 | if (only) {
|
62 | process.env.ZORA_ONLY = true;
|
63 | }
|
64 |
|
65 |
|
66 | const { hold, report } = await import('zora');
|
67 | hold();
|
68 |
|
69 | const reporterInstance = reporterMap[reporter] || reporter.diff;
|
70 | const files = await globby(
|
71 | filePatterns.length ? filePatterns : DEFAULT_FILE_PATTERNS
|
72 | );
|
73 |
|
74 | if (!files.length) {
|
75 | console.warn(`no file matching the patterns: ${filePatterns.join(', ')}`);
|
76 | return;
|
77 | }
|
78 |
|
79 | for (const file of files) {
|
80 | const filePath = resolve(process.cwd(), file);
|
81 | await import(pathToFileURL(filePath));
|
82 | }
|
83 |
|
84 | await report({
|
85 | reporter: reporterInstance,
|
86 | });
|
87 | })();
|