@@ -43,33 +43,38 @@ type PlainObject = { [key: string]: any };
43
43
44
44
// TODO - combine these (only store values for collections?)
45
45
const mergedExports : PlainObject = { } ;
46
- const mergedExportsWithSource : Array < { name : string ; source : string } > = [ ] ;
46
+ const mergedExportsWithSources : Array < {
47
+ exportName : string ;
48
+ source : string ;
49
+ // elementSources?: Array<{ elementName: string; source: string }>;
50
+ elementSources ?: { node : string [ ] ; react : string [ ] } ;
51
+ } > = [ ] ;
47
52
48
- const allNames = new Set ( [ ...Object . keys ( nodeSDK ) , ...Object . keys ( reactSDK ) ] ) ;
53
+ const allExportNames = new Set ( [ ...Object . keys ( nodeSDK ) , ...Object . keys ( reactSDK ) ] ) ;
49
54
50
- allNames . forEach ( name => {
51
- const nodeExport = ( nodeSDK as PlainObject ) [ name ] ;
52
- const reactExport = ( reactSDK as PlainObject ) [ name ] ;
55
+ allExportNames . forEach ( exportName => {
56
+ const nodeExport = ( nodeSDK as PlainObject ) [ exportName ] ;
57
+ const reactExport = ( reactSDK as PlainObject ) [ exportName ] ;
53
58
54
59
// First, the easy stuff - things that only appear in one or the other package.
55
60
if ( nodeExport && ! reactExport ) {
56
- mergedExports [ name ] = nodeExport ;
57
- mergedExportsWithSource . push ( { name , source : '@sentry/node' } ) ;
61
+ mergedExports [ exportName ] = nodeExport ;
62
+ mergedExportsWithSources . push ( { exportName , source : " '@sentry/node'" } ) ;
58
63
return ;
59
64
}
60
65
61
66
if ( reactExport && ! nodeExport ) {
62
- mergedExports [ name ] = reactExport ;
63
- mergedExportsWithSource . push ( { name , source : '@sentry/react' } ) ;
67
+ mergedExports [ exportName ] = reactExport ;
68
+ mergedExportsWithSources . push ( { exportName , source : " '@sentry/react'" } ) ;
64
69
return ;
65
70
}
66
71
67
72
// If we've gotten this far, it means that both packages export something named `name`. In some cases, that's because
68
73
// they're literally exporting the same thing (a type imported from `@sentry/types`, for example). If so, there's no
69
74
// actual clash, so just copy over node's copy since it's equal to react's copy.
70
75
if ( nodeExport === reactExport ) {
71
- mergedExports [ name ] = nodeExport ;
72
- mergedExportsWithSource . push ( { name , source : '@sentry/node' } ) ;
76
+ mergedExports [ exportName ] = nodeExport ;
77
+ mergedExportsWithSources . push ( { exportName , source : " '@sentry/node'" } ) ;
73
78
return ;
74
79
}
75
80
@@ -83,9 +88,9 @@ allNames.forEach(name => {
83
88
// In theory there are other collections besides objects and arrays, but thankfully we don't have any in this case.
84
89
if ( ! Array . isArray ( nodeExport ) && ! isPlainObject ( nodeExport ) ) {
85
90
// can't leave this out, so use the node version for now
86
- if ( name === 'init' ) {
87
- mergedExports [ name ] = nodeExport ;
88
- mergedExportsWithSource . push ( { name , source : '@sentry/node' } ) ;
91
+ if ( exportName === 'init' ) {
92
+ mergedExports [ exportName ] = nodeExport ;
93
+ mergedExportsWithSources . push ( { exportName , source : " '@sentry/node'" } ) ;
89
94
}
90
95
// otherwise, bail
91
96
return ;
@@ -113,7 +118,14 @@ allNames.forEach(name => {
113
118
// And now we do it all again, in miniature
114
119
const allCollectionNames = new Set ( [ ...Object . keys ( nodeCollection ) , ...Object . keys ( reactCollection ) ] ) ;
115
120
const mergedCollection : PlainObject = { } ;
116
- const mergedCollectionWithSource : PlainObject = [ ] ;
121
+ const mergedCollectionBySource : {
122
+ node : string [ ] ;
123
+ react : string [ ] ;
124
+ } = { node : [ ] , react : [ ] } ;
125
+ // const mergedCollectionWithSources: Array<{
126
+ // elementName: string;
127
+ // source: string;
128
+ // }> = [];
117
129
118
130
allCollectionNames . forEach ( elementName => {
119
131
const nodeCollectionElement = nodeCollection [ elementName ] ;
@@ -122,14 +134,16 @@ allNames.forEach(name => {
122
134
// grab everything that's only in node...
123
135
if ( nodeCollectionElement && ! reactCollectionElement ) {
124
136
mergedCollection [ elementName ] = nodeCollectionElement ;
125
- mergedCollectionWithSource . push ( { elementName, source : '@sentry/node' } ) ;
137
+ mergedCollectionBySource . node . push ( elementName ) ;
138
+ // mergedCollectionWithSources.push({ elementName, source: 'nodeSDK' });
126
139
return ;
127
140
}
128
141
129
142
// ... and everything that's only in react
130
143
if ( reactCollectionElement && ! nodeCollectionElement ) {
131
144
mergedCollection [ elementName ] = reactCollectionElement ;
132
- mergedCollectionWithSource . push ( { elementName, source : '@senty/react' } ) ;
145
+ mergedCollectionBySource . react . push ( elementName ) ;
146
+ // mergedCollectionWithSources.push({ elementName, source: 'reactSDK' });
133
147
return ;
134
148
}
135
149
@@ -142,7 +156,8 @@ allNames.forEach(name => {
142
156
Object . getPrototypeOf ( nodeCollectionElement ) === Object . getPrototypeOf ( reactCollectionElement ) )
143
157
) {
144
158
mergedCollection [ elementName ] = nodeCollectionElement ;
145
- mergedCollectionWithSource . push ( { elementName, source : '@sentry/node' } ) ;
159
+ mergedCollectionBySource . node . push ( elementName ) ;
160
+ // mergedCollectionWithSources.push({ elementName, source: 'nodeSDK' });
146
161
return ;
147
162
}
148
163
@@ -152,18 +167,19 @@ allNames.forEach(name => {
152
167
153
168
// having merged the two collections, if we started with an array, convert back to one
154
169
if ( Array . isArray ( nodeExport ) ) {
155
- mergedExports [ name ] = Object . values ( mergedCollection ) ;
156
- mergedExportsWithSource . push ( { name , source : 'array' } ) ; // TODO have to build the collection as a string
170
+ mergedExports [ exportName ] = Object . values ( mergedCollection ) ;
171
+ mergedExportsWithSources . push ( { exportName , source : 'array' , elementSources : mergedCollectionBySource } ) ; // TODO have to build the collection as a string
157
172
}
158
173
// otherwise, just use the merged object
159
174
else {
160
- mergedExports [ name ] = mergedCollection ;
161
- mergedExportsWithSource . push ( { name , source : 'object' } ) ;
175
+ mergedExports [ exportName ] = mergedCollection ;
176
+ mergedExportsWithSources . push ( { exportName , source : 'object' , elementSources : mergedCollectionBySource } ) ;
162
177
}
163
178
} ) ;
164
179
165
180
// TODO - should we be importing from the two index files instead?
166
181
// TODO - export correct SDK name value
182
+ // TODO - call prettier from here? clean up package.json
167
183
168
184
// This is here as a real comment (rather than an array of strings) because it's easier to edit that way if we ever need
169
185
// to. (TODO: Convert to a string per line automatically)
@@ -181,25 +197,76 @@ const outputLines = [
181
197
' * More detail can be found in the script that (compiles to the script that) generated this file,' ,
182
198
' * `/scripts/generate-types.ts`.' ,
183
199
' */' ,
200
+ '' ,
201
+ "import * as nodeSDK from '@sentry/node'" ,
202
+ "import * as reactSDK from '@sentry/react'" ,
203
+ '' ,
184
204
] ;
185
205
186
- mergedExportsWithSource . forEach ( element => {
187
- const { name , source } = element ;
206
+ mergedExportsWithSources . forEach ( element => {
207
+ const { exportName , source, elementSources } = element ;
188
208
189
- if ( source === '@sentry/node' || source === '@sentry/react' ) {
190
- outputLines . push ( `export { ${ name } } from ' ${ source } ' ;` ) ;
209
+ if ( source === " '@sentry/node'" || source === " '@sentry/react'" ) {
210
+ outputLines . push ( `export { ${ exportName } } from ${ source } ;`) ;
191
211
return ;
192
212
}
193
213
194
214
if ( source === 'array' ) {
195
- // TODO
215
+ const titleCaseExportName = exportName . replace ( exportName [ 0 ] , exportName [ 0 ] . toUpperCase ( ) ) ;
216
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
217
+ const { node : nodeElementNames , react : reactElementNames } = elementSources ! ;
218
+
219
+ outputLines . push ( `const node${ titleCaseExportName } Names = ${ JSON . stringify ( nodeElementNames ) } ` ) ;
220
+ outputLines . push ( `const react${ titleCaseExportName } Names = ${ JSON . stringify ( reactElementNames ) } ` ) ;
221
+ outputLines . push (
222
+ `const node${ titleCaseExportName } = nodeSDK.${ exportName } .filter(element => element.name in node${ titleCaseExportName } Names)` ,
223
+ ) ;
224
+ outputLines . push (
225
+ `const react${ titleCaseExportName } = reactSDK.${ exportName } .filter(element => element.name in react${ titleCaseExportName } Names)` ,
226
+ ) ;
227
+ outputLines . push ( `export const ${ exportName } = [ ...node${ titleCaseExportName } , ...react${ titleCaseExportName } ]` ) ;
228
+
229
+ return ;
230
+
231
+ // outputLines.push(`const react${titleCaseExportName} = ${JSON.stringify(reactElements)}`);
232
+
233
+ // outputLines.push(`export const ${exportName} = [ ${namespacedElements?.join(', ')} ]`);
234
+
235
+ // outputLines.push(`const node${titleCaseExportName} = nodeSDK.${exportName}.filter(element => element.name in ${JSON.stringify(nodeElements)})`);
236
+ // const namespacedElements = elementSources?.map(
237
+ // ({ elementName, source }) => `${source}.${exportName}.${elementName}`,
238
+ // );
239
+ // outputLines.push(`export const ${exportName} = [ ${namespacedElements?.join(', ')} ]`);
196
240
}
197
241
198
242
if ( source === 'object' ) {
199
- // TODO
243
+ const titleCaseExportName = exportName . replace ( exportName [ 0 ] , exportName [ 0 ] . toUpperCase ( ) ) ;
244
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
245
+ const { node : nodeElementNames , react : reactElementNames } = elementSources ! ;
246
+
247
+ outputLines . push ( `const node${ titleCaseExportName } Names = ${ JSON . stringify ( nodeElementNames ) } ` ) ;
248
+ outputLines . push ( `const react${ titleCaseExportName } Names = ${ JSON . stringify ( reactElementNames ) } `) ;
249
+ outputLines . push ( `const node${ titleCaseExportName } = { } as { [key: string]: any };` ) ;
250
+ outputLines . push ( `const react${ titleCaseExportName } = { } as { [key: string]: any };` ) ;
251
+ outputLines . push (
252
+ `node${ titleCaseExportName } Names.forEach(elementName => { node${ titleCaseExportName } [elementName] = nodeSDK.${ exportName } [elementName as keyof typeof nodeSDK.${ exportName } ]});` ,
253
+ ) ;
254
+ outputLines . push (
255
+ `react${ titleCaseExportName } Names.forEach(elementName => { react${ titleCaseExportName } [elementName] = reactSDK.${ exportName } [elementName as keyof typeof reactSDK.${ exportName } ]});` ,
256
+ ) ;
257
+ outputLines . push ( `export const ${ exportName } = { ...node${ titleCaseExportName } , ...react${ titleCaseExportName } }` ) ;
258
+
259
+ // nodeElementNames.forEach(elementName => { x[elementName] = nodeSDK.y[elementName] });
260
+ return ;
200
261
}
201
262
} ) ;
202
263
264
+ // export const ${collectionName} = [nodeSDK.Http, ${source}.${elementName}, etc]
265
+ // export const ${collectionName} = {Http: nodeSDK.Http, ${elementName}: ${source}.${elementName}, etc}
266
+
267
+ // nodeSDK.defaultIntegrations.filter(integration => nodeElements.includes(integration.name));
268
+
269
+ console . log ( outputLines ) ;
203
270
// eslint-disable-next-line no-console
204
271
console . log ( 'Generating `types.ts`...' ) ;
205
272
0 commit comments