@@ -11,43 +11,35 @@ export interface IntegrationIndex {
1111
1212/** Gets integration to install */
1313export 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 || [ ] ;
2017
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+ ] ;
3125
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+ ] ;
3940 } else if ( typeof userIntegrations === 'function' ) {
40- integrations = userIntegrations ( defaultIntegrations ) ;
41+ integrations = userIntegrations ( integrations ) ;
4142 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 ) ) ;
5143 }
5244
5345 return integrations ;
@@ -69,7 +61,7 @@ export function setupIntegration(integration: Integration): void {
6961 * @param integrations array of integration instances
7062 * @param withDefault should enable default integrations
7163 */
72- export function setupIntegrations < O extends Options > ( options : O ) : IntegrationIndex {
64+ export function setupIntegrations ( options : Options ) : IntegrationIndex {
7365 const integrations : IntegrationIndex = { } ;
7466 getIntegrationsToSetup ( options ) . forEach ( integration => {
7567 integrations [ integration . name ] = integration ;
0 commit comments