UNPKG

1.32 kBPlain TextView Raw
1import { Spec } from '@hayspec/spec';
2import * as glob from 'fast-glob';
3
4/**
5 *
6 */
7export interface RunnerResult {
8 file: string;
9 spec: Spec;
10}
11
12/**
13 *
14 */
15export interface RunnerOptions {
16 cwd?: string;
17 deep?: boolean;
18 dot?: boolean;
19}
20
21 /**
22 *
23 */
24export 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 * NOTE: Due to different NPM package managers, the `instanceof` check my be
66 * inconsistent thus the function checks for presence of the `test` method.
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}