-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMultiFetcher.ts
50 lines (36 loc) Β· 1.54 KB
/
MultiFetcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {Fetcher, FetchOptions, MinimalFetchOptions} from './Fetcher';
import {MessageName} from './MessageName';
import {ReportError} from './Report';
import * as structUtils from './structUtils';
import {Locator} from './types';
export class MultiFetcher implements Fetcher {
private readonly fetchers: Array<Fetcher>;
constructor(fetchers: Array<Fetcher>) {
this.fetchers = fetchers;
}
supports(locator: Locator, opts: MinimalFetchOptions) {
if (!this.tryFetcher(locator, opts))
return false;
return true;
}
getLocalPath(locator: Locator, opts: FetchOptions) {
const fetcher = this.getFetcher(locator, opts);
return fetcher.getLocalPath(locator, opts);
}
async fetch(locator: Locator, opts: FetchOptions) {
const fetcher = this.getFetcher(locator, opts);
return await fetcher.fetch(locator, opts);
}
private tryFetcher(locator: Locator, opts: MinimalFetchOptions) {
const fetcher = this.fetchers.find(fetcher => fetcher.supports(locator, opts));
if (!fetcher)
return null;
return fetcher;
}
private getFetcher(locator: Locator, opts: MinimalFetchOptions) {
const fetcher = this.fetchers.find(fetcher => fetcher.supports(locator, opts));
if (!fetcher)
throw new ReportError(MessageName.FETCHER_NOT_FOUND, `${structUtils.prettyLocator(opts.project.configuration, locator)} isn't supported by any available fetcher`);
return fetcher;
}
}