1 | import { Spec } from '@hayspec/spec';
|
2 | import * as glob from 'fast-glob';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export interface RunnerResult {
|
8 | file: string;
|
9 | spec: Spec;
|
10 | }
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export interface RunnerOptions {
|
16 | cwd?: string;
|
17 | deep?: boolean;
|
18 | dot?: boolean;
|
19 | }
|
20 |
|
21 | |
22 |
|
23 |
|
24 | export class Runner {
|
25 | protected options: RunnerOptions;
|
26 | public results: RunnerResult[] = [];
|
27 |
|
28 | |
29 |
|
30 |
|
31 | public constructor(options?: RunnerOptions) {
|
32 | this.options = {
|
33 | cwd: process.cwd(),
|
34 | deep: true,
|
35 | dot: false,
|
36 | ...options,
|
37 | };
|
38 | }
|
39 |
|
40 | |
41 |
|
42 |
|
43 | public async require(...patterns: string[]) {
|
44 | const options = {
|
45 | absolute: true,
|
46 | ...this.options,
|
47 | };
|
48 | const files = await glob(patterns, options) as string[];
|
49 |
|
50 | files.forEach((file) => {
|
51 | this.loadSpec(file);
|
52 | });
|
53 | }
|
54 |
|
55 | |
56 |
|
57 |
|
58 | public clear () {
|
59 | this.results = [];
|
60 | return this;
|
61 | }
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | protected loadSpec(file: string) {
|
69 | const spec = require(file);
|
70 |
|
71 | if (spec instanceof Spec) {
|
72 | this.results.push({ file, spec });
|
73 | } else if (spec.default instanceof Spec) {
|
74 | this.results.push({ file, spec: spec.default });
|
75 | }
|
76 | }
|
77 |
|
78 | }
|