@@ -11,43 +11,35 @@ export interface IntegrationIndex {
11
11
12
12
/** Gets integration to install */
13
13
export function getIntegrationsToSetup ( options : Options ) : Integration [ ] {
14
- const defaultIntegrations = ( options . defaultIntegrations && [ ...options . defaultIntegrations ] ) || [ ] ;
15
- const userIntegrations = options . integrations ;
16
- let integrations : Integration [ ] = [ ] ;
17
- if ( Array . isArray ( userIntegrations ) ) {
18
- const userIntegrationsNames = userIntegrations . map ( i => i . name ) ;
19
- const pickedIntegrationsNames : string [ ] = [ ] ;
14
+ const defaultIntegrations = options . _internal ?. defaultIntegrations || [ ] ;
15
+ const discoveredIntegrations = options . _internal ?. discoveredIntegrations || [ ] ;
16
+ const userIntegrations = options . integrations || [ ] ;
20
17
21
- // Leave only unique default integrations, that were not overridden with provided user integrations
22
- defaultIntegrations . forEach ( defaultIntegration => {
23
- if (
24
- userIntegrationsNames . indexOf ( defaultIntegration . name ) === - 1 &&
25
- pickedIntegrationsNames . indexOf ( defaultIntegration . name ) === - 1
26
- ) {
27
- integrations . push ( defaultIntegration ) ;
28
- pickedIntegrationsNames . push ( defaultIntegration . name ) ;
29
- }
30
- } ) ;
18
+ // Filter out default integrations that are also discovered
19
+ let integrations : Integration [ ] = [
20
+ ...defaultIntegrations . filter ( defaultIntegration =>
21
+ discoveredIntegrations . every ( discoveredIntegration => discoveredIntegration . name !== defaultIntegration . name ) ,
22
+ ) ,
23
+ ...discoveredIntegrations ,
24
+ ] ;
31
25
32
- // Don't add same user integration twice
33
- userIntegrations . forEach ( userIntegration => {
34
- if ( pickedIntegrationsNames . indexOf ( userIntegration . name ) === - 1 ) {
35
- integrations . push ( userIntegration ) ;
36
- pickedIntegrationsNames . push ( userIntegration . name ) ;
37
- }
38
- } ) ;
26
+ if ( Array . isArray ( userIntegrations ) ) {
27
+ // Filter out integrations that are also included in user options
28
+ integrations = [
29
+ ...integrations . filter ( integrations =>
30
+ userIntegrations . every ( userIntegration => userIntegration . name !== integrations . name ) ,
31
+ ) ,
32
+ // And filter out duplicated user options integrations
33
+ ...userIntegrations . reduce ( ( acc , userIntegration ) => {
34
+ if ( acc . every ( accIntegration => userIntegration . name !== accIntegration . name ) ) {
35
+ acc . push ( userIntegration ) ;
36
+ }
37
+ return acc ;
38
+ } , [ ] as Integration [ ] ) ,
39
+ ] ;
39
40
} else if ( typeof userIntegrations === 'function' ) {
40
- integrations = userIntegrations ( defaultIntegrations ) ;
41
+ integrations = userIntegrations ( integrations ) ;
41
42
integrations = Array . isArray ( integrations ) ? integrations : [ integrations ] ;
42
- } else {
43
- integrations = [ ...defaultIntegrations ] ;
44
- }
45
-
46
- // Make sure that if present, `Debug` integration will always run last
47
- const integrationsNames = integrations . map ( i => i . name ) ;
48
- const alwaysLastToRun = 'Debug' ;
49
- if ( integrationsNames . indexOf ( alwaysLastToRun ) !== - 1 ) {
50
- integrations . push ( ...integrations . splice ( integrationsNames . indexOf ( alwaysLastToRun ) , 1 ) ) ;
51
43
}
52
44
53
45
return integrations ;
@@ -69,7 +61,7 @@ export function setupIntegration(integration: Integration): void {
69
61
* @param integrations array of integration instances
70
62
* @param withDefault should enable default integrations
71
63
*/
72
- export function setupIntegrations < O extends Options > ( options : O ) : IntegrationIndex {
64
+ export function setupIntegrations ( options : Options ) : IntegrationIndex {
73
65
const integrations : IntegrationIndex = { } ;
74
66
getIntegrationsToSetup ( options ) . forEach ( integration => {
75
67
integrations [ integration . name ] = integration ;
0 commit comments