UNPKG

1.71 kBJavaScriptView Raw
1const requireDeep = require('../util/requireDeep');
2
3class Strategy {
4
5 /**
6 * @param {string} name name for the Strategy
7 * @param {Array<Function>} resources array of resource creators
8 * @param {Function} compiler spec compiler for this Strategy
9 * @param {Object} runTimeOptions options to use when running Gunner
10 */
11 constructor (name, resources, compiler, runTimeOptions) {
12 this.name = name;
13 this.__resourceCreators = resources;
14 this.__runTimeOptions = runTimeOptions;
15 this.compiler = compiler;
16 this.__await__ = [];
17
18 this.__gunnerInstances = [];
19 this.resources = {};
20 this.specs = [];
21 }
22
23 /**
24 * Creates a single resource
25 * @param {string} name
26 * @param {Object} resource
27 */
28 createResource (name, resource) {
29 return this.__resourceCreators[name](resource);
30 }
31
32 /**
33 * Fetches all specs that match provided options
34 * @param {Object} options
35 * @param {string} options.plans
36 * @param {Array<string>=} options.exclude
37 * @param {string|Array<string>=} options.pattern
38 */
39 fetchSpecs (options) {
40 this.__await__.push(
41 requireDeep(options)
42 .then(required => required.map(
43 each => this.__gunnerInstances = this.compiler(this)(each)
44 )));
45 return this;
46 }
47
48 /**
49 * Adds a single spec to the strategy instance
50 * @param {Object} spec spec to add
51 */
52 addSpec (spec) {
53 this.__gunnerInstances.push(this.compiler(this)(spec));
54 return this;
55 }
56
57 /**
58 * Starts running the gunner instances with the given options
59 * @param {Object=} options
60 */
61 run (options) {
62 const runOptions = options || this.__runTimeOptions;
63
64 return Promise.all(this.__await__).then(() => (
65 this.__gunnerInstances.run(runOptions)));
66 }
67
68}
69
70module.exports = Strategy;