1
+ const path = require ( 'path' ) ;
2
+ const fs = require ( 'fs' ) ;
3
+ const yaml = require ( 'js-yaml' ) ;
4
+ const {
5
+ slugify,
6
+ getCatalog,
7
+ getConnectionModes,
8
+ isCatalogItemHidden,
9
+ sanitize,
10
+ doesCatalogItemExist
11
+ } = require ( './utilities.js' ) ;
12
+
13
+ require ( 'dotenv' ) . config ( ) ;
14
+
15
+ const PAPI_URL = "https://api.segmentapis.com" ;
16
+
17
+
18
+ const updateDestinations = async ( ) => {
19
+ let destinations = [ ] ;
20
+ let destinationsUpdated = [ ] ;
21
+ let destinationCategories = [ ] ;
22
+ let categories = new Set ( ) ;
23
+ let nextPageToken = "MA==" ;
24
+
25
+ // Get all destinations from the Public API
26
+ while ( nextPageToken !== undefined ) {
27
+ const res = await getCatalog ( `${ PAPI_URL } /catalog/destinations/` , nextPageToken ) ;
28
+ destinations = destinations . concat ( res . data . destinationsCatalog ) ;
29
+ nextPageToken = res . data . pagination . next ;
30
+ }
31
+
32
+ // Sort the destinations alphabetically
33
+ destinations . sort ( ( a , b ) => {
34
+ if ( a . name . toLowerCase ( ) < b . name . toLowerCase ( ) ) {
35
+ return - 1 ;
36
+ }
37
+ if ( a . name . toLowerCase ( ) > b . name . toLowerCase ( ) ) {
38
+ return 1 ;
39
+ }
40
+ return 0 ;
41
+ } ) ;
42
+
43
+ // Loop through all destinations and create a new object with the data we want
44
+ destinations . forEach ( destination => {
45
+ let endpoints = [ ] ;
46
+ let regions = [ ] ;
47
+
48
+ let slug = slugify ( destination . name , "destinations" ) ;
49
+
50
+ if ( typeof destination . supportedRegions != "undefined" ) {
51
+ regions = destination . supportedRegions ;
52
+ } else {
53
+ regions . push ( 'us-west-2' , 'eu-west-1' ) ;
54
+ }
55
+
56
+ if ( typeof destination . regionEndpoints != "undefined" ) {
57
+ endpoints = destination . regionEndpoints ;
58
+ } else {
59
+ endpoints . push ( 'US' ) ;
60
+ }
61
+
62
+ let url = `connections/destinations/catalog/${ slug } ` ;
63
+
64
+ let tempCategories = [ destination . categories ] ;
65
+ tempCategories = tempCategories . filter ( category => category != '' ) ;
66
+ tempCategories = tempCategories . flat ( ) ;
67
+
68
+ let connection_modes = getConnectionModes ( {
69
+ components : destination . components ,
70
+ platforms : destination . supportedPlatforms ,
71
+ browserUnbundling : destination . supportedFeatures . browserUnbundling ,
72
+ browserUnbundlingPublic : destination . supportedFeatures . browserUnbundlingPublic ,
73
+ methods : destination . supportedMethods
74
+ } ) ;
75
+
76
+ let settings = destination . options ;
77
+
78
+ settings . sort ( ( a , b ) => {
79
+ if ( a . name . toLowerCase ( ) < b . name . toLowerCase ( ) ) {
80
+ return - 1 ;
81
+ }
82
+ if ( a . name . toLowerCase ( ) > b . name . toLowerCase ( ) ) {
83
+ return 1 ;
84
+ }
85
+ return 0 ;
86
+ } ) ;
87
+
88
+ settings . forEach ( setting => {
89
+ setting . description = sanitize ( setting . description ) ;
90
+ } ) ;
91
+
92
+ let actions = destination . actions ;
93
+ let presets = destination . presets ;
94
+
95
+ const clone = ( obj ) => Object . assign ( { } , obj ) ;
96
+ const renameKey = ( object , key , newKey ) => {
97
+ const clonedObj = clone ( object ) ;
98
+ const targetKey = clonedObj [ key ] ;
99
+ delete clonedObj [ key ] ;
100
+
101
+ clonedObj [ newKey ] = targetKey ;
102
+ return clonedObj ;
103
+ } ;
104
+
105
+ // I honestly don't remember why I did this.
106
+ // I think someone wanted to mention support for the Screen method to whatever destination that is
107
+ destination . supportedMethods . screen = false ;
108
+ if ( destination . id == '63e42b47479274407b671071' ) {
109
+ destination . supportedMethods . screen = true ;
110
+ }
111
+
112
+ // Pageview is renamed to Page
113
+ destination . supportedMethods = renameKey ( destination . supportedMethods , 'pageview' , 'page' ) ;
114
+
115
+ // All updated destination information gets added to this object
116
+ let updatedDestination = {
117
+ id : destination . id ,
118
+ display_name : destination . name ,
119
+ name : destination . name ,
120
+ slug,
121
+ hidden : isCatalogItemHidden ( url ) ,
122
+ endpoints,
123
+ regions,
124
+ url,
125
+ previous_names : destination . previousNames ,
126
+ website : destination . website ,
127
+ status : destination . status ,
128
+ categories : tempCategories ,
129
+ logo : {
130
+ url : destination . logos . default
131
+ } ,
132
+ mark : {
133
+ url : destination . logos . mark
134
+ } ,
135
+ methods : destination . supportedMethods ,
136
+ platforms : destination . supportedPlatforms ,
137
+ components : destination . components ,
138
+ browserUnbundlingSupported : destination . supportedFeatures . browserUnbundling ,
139
+ browserUnbundlingPublic : destination . supportedFeatures . browserUnbundlingPublic ,
140
+ replay : destination . supportedFeatures . replay ,
141
+ connection_modes,
142
+ settings,
143
+ actions,
144
+ presets
145
+ } ;
146
+
147
+ // Add the updated destination to the destinationsUpdated array
148
+ destinationsUpdated . push ( updatedDestination ) ;
149
+ doesCatalogItemExist ( updatedDestination ) ;
150
+ tempCategories . reduce ( ( s , e ) => s . add ( e ) , categories ) ;
151
+ } ) ;
152
+
153
+ const destinationArray = Array . from ( categories ) ;
154
+ destinationArray . forEach ( category => {
155
+ destinationCategories . push ( {
156
+ display_name : category ,
157
+ slug : slugify ( category )
158
+ } ) ;
159
+ destinationCategories . sort ( ( a , b ) => {
160
+ if ( a . display_name . toLowerCase ( ) < b . display_name . toLowerCase ( ) ) {
161
+ return - 1 ;
162
+ }
163
+ if ( a . display_name . toLowerCase ( ) > b . display_name . toLowerCase ( ) ) {
164
+ return 1 ;
165
+ }
166
+ return 0 ;
167
+ } ) ;
168
+ } ) ;
169
+
170
+ const options = {
171
+ noArrayIndent : true
172
+ } ;
173
+ const todayDate = new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
174
+
175
+ // Create destination catalog YAML file
176
+ let output = "# AUTOGENERATED FROM PUBLIC API. DO NOT EDIT\n" ;
177
+ output += "# destination data last updated " + todayDate + " \n" ;
178
+ output += yaml . dump ( {
179
+ items : destinationsUpdated
180
+ } , options ) ;
181
+ fs . writeFileSync ( path . resolve ( __dirname , `../../src/_data/catalog/destinations.yml` ) , output ) ;
182
+
183
+ // Create destination-category mapping YAML file
184
+ output = "# AUTOGENERATED FROM PUBLIC API. DO NOT EDIT\n" ;
185
+ output += "# destination categories last updated " + todayDate + " \n" ;
186
+ output += yaml . dump ( {
187
+ items : destinationCategories
188
+ } , options ) ;
189
+ fs . writeFileSync ( path . resolve ( __dirname , `../../src/_data/catalog/destination_categories.yml` ) , output ) ;
190
+
191
+ console . log ( "destinations done" ) ;
192
+ } ;
193
+
194
+
195
+ exports . updateDestinations = updateDestinations ;
0 commit comments