8000 working for collections, needs cleanup · szechyjs/sentry-javascript@58a0c77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58a0c77

Browse files
committed
working for collections, needs cleanup
1 parent 3562e6b commit 58a0c77

File tree

3 files changed

+147
-29
lines changed

3 files changed

+147
-29
lines changed

packages/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"build:es5": "tsc -p tsconfig.build.json",
3636
"build:esm": "tsc -p tsconfig.esm.json",
3737
"build:scripts": "tsc -p tsconfig.scripts.json",
38-
"build:types": "yarn build:scripts && node scripts/build/generate-types.js",
38+
"build:types": "yarn build:scripts && node scripts/build/generate-types.js && prettier --write \"src/types.ts\"",
3939
"build:watch": "yarn build:types && run-p build:watch:esm build:watch:scripts",
4040
"build:watch:esm": "yarn build:types && tsc -p tsconfig.esm.json -w --preserveWatchOutput",
4141
"build:watch:scripts": "yarn build:types && tsc -p tsconfig.scripts.json -w --preserveWatchOutput",

packages/nextjs/scripts/generate-types.ts

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,38 @@ type PlainObject = { [key: string]: any };
4343

4444
// TODO - combine these (only store values for collections?)
4545
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+
}> = [];
4752

48-
const allNames = new Set([...Object.keys(nodeSDK), ...Object.keys(reactSDK)]);
53+
const allExportNames = new Set([...Object.keys(nodeSDK), ...Object.keys(reactSDK)]);
4954

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];
5358

5459
// First, the easy stuff - things that only appear in one or the other package.
5560
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'" });
5863
return;
5964
}
6065

6166
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'" });
6469
return;
6570
}
6671

6772
// If we've gotten this far, it means that both packages export something named `name`. In some cases, that's because
6873
// they're literally exporting the same thing (a type imported from `@sentry/types`, for example). If so, there's no
6974
// actual clash, so just copy over node's copy since it's equal to react's copy.
7075
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'" });
7378
return;
7479
}
7580

@@ -83,9 +88,9 @@ allNames.forEach(name => {
8388
// In theory there are other collections besides objects and arrays, but thankfully we don't have any in this case.
8489
if (!Array.isArray(nodeExport) && !isPlainObject(nodeExport)) {
8590
// 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'" });
8994
}
9095
// otherwise, bail
9196
return;
@@ -113,7 +118,14 @@ allNames.forEach(name => {
113118
// And now we do it all again, in miniature
114119
const allCollectionNames = new Set([...Object.keys(nodeCollection), ...Object.keys(reactCollection)]);
115120
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+
// }> = [];
117129

118130
allCollectionNames.forEach(elementName => {
119131
const nodeCollectionElement = nodeCollection[elementName];
@@ -122,14 +134,16 @@ allNames.forEach(name => {
122134
// grab everything that's only in node...
123135
if (nodeCollectionElement && !reactCollectionElement) {
124136
mergedCollection[elementName] = nodeCollectionElement;
125-
mergedCollectionWithSource.push({ elementName, source: '@sentry/node' });
137+
mergedCollectionBySource.node.push(elementName);
138+
// mergedCollectionWithSources.push({ elementName, source: 'nodeSDK' });
126139
return;
127140
}
128141

129142
// ... and everything that's only in react
130143
if (reactCollectionElement && !nodeCollectionElement) {
131144
mergedCollection[elementName] = reactCollectionElement;
132-
mergedCollectionWithSource.push({ elementName, source: '@senty/react' });
145+
mergedCollectionBySource.react.push(elementName);
146+
// mergedCollectionWithSources.push({ elementName, source: 'reactSDK' });
133147
return;
134148
}
135149

@@ -142,7 +156,8 @@ allNames.forEach(name => {
142156
Object.getPrototypeOf(nodeCollectionElement) === Object.getPrototypeOf(reactCollectionElement))
143157
) {
144158
mergedCollection[elementName] = nodeCollectionElement;
145-
mergedCollectionWithSource.push({ elementName, source: '@sentry/node' });
159+
mergedCollectionBySource.node.push(elementName);
160+
// mergedCollectionWithSources.push({ elementName, source: 'nodeSDK' });
146161
return;
147162
}
148163

@@ -152,18 +167,19 @@ allNames.forEach(name => {
152167

153168
// having merged the two collections, if we started with an array, convert back to one
154169
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
157172
}
158173
// otherwise, just use the merged object
159174
else {
160-
mergedExports[name] = mergedCollection;
161-
mergedExportsWithSource.push({ name, source: 'object' });
175+
mergedExports[exportName] = mergedCollection;
176+
mergedExportsWithSources.push({ exportName, source: 'object', elementSources: mergedCollectionBySource });
162177
}
163178
});
164179

165180
// TODO - should we be importing from the two index files instead?
166181
// TODO - export correct SDK name value
182+
// TODO - call prettier from here? clean up package.json
167183

168184
// 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
169185
// to. (TODO: Convert to a string per line automatically)
@@ -181,25 +197,76 @@ const outputLines = [
181197
' * More detail can be found in the script that (compiles to the script that) generated this file,',
182198
' * `/scripts/generate-types.ts`.',
183199
' */',
200+
'',
201+
"import * as nodeSDK from '@sentry/node'",
202+
"import * as reactSDK from '@sentry/react'",
203+
'',
184204
];
185205

186-
mergedExportsWithSource.forEach(element => {
187-
const { name, source } = element;
206+
mergedExportsWithSources.forEach(element => {
207+
const { exportName, source, elementSources } = element;
188208

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};`);
191211
return;
192212
}
193213

194214
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(', ')} ]`);
196240
}
197241

198242
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;
200261
}
201262
});
202263

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);
203270
// eslint-disable-next-line no-console
204271
console.log('Generating `types.ts`...');
205272

packages/nextjs/src/types.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
* More detail can be found in the script that (compiles to the script that) generated this file,
55
* `/scripts/generate-types.ts`.
66
*/
7+
8+
import * as nodeSDK from '@sentry/node';
9+
import * as reactSDK from '@sentry/react';
10+
711
export { Severity } from '@sentry/node';
812
export { Status } from '@sentry/node';
913
export { addGlobalEventProcessor } from '@sentry/node';
@@ -28,8 +32,55 @@ export { setUser } from '@sentry/node';
2832
export { withScope } from '@sentry/node';
2933
export { NodeBackend } from '@sentry/node';
3034
export { NodeClient } from '@sentry/node';
35+
const nodeDefaultIntegrationsNames = [
36+
'InboundFilters',
37+
'FunctionToString',
38+
'Console',
39+
'Http',
40+
'OnUncaughtException',
41+
'OnUnhandledRejection',
42+
];
43+
const reactDefaultIntegrationsNames = ['TryCatch', 'Breadcrumbs', 'GlobalHandlers', 'UserAgent'];
44+
const nodeDefaultIntegrations = nodeSDK.defaultIntegrations.filter(
45+
element => element.name in nodeDefaultIntegrationsNames,
46+
);
47+
const reactDefaultIntegrations = reactSDK.defaultIntegrations.filter(
48+
element => element.name in reactDefaultIntegrationsNames,
49+
);
50+
export const defaultIntegrations = [...nodeDefaultIntegrations, ...reactDefaultIntegrations];
3151
export { init } from '@sentry/node';
3252
export { Handlers } from '@sentry/node';
53+
const nodeTransportsNames = ['BaseTransport', 'HTTPTransport', 'HTTPSTransport'];
54+
const reactTransportsNames = ['FetchTransport', 'XHRTransport'];
55+
const nodeTransports = {} as { [key: string]: any };
56+
const reactTransports = {} as { [key: string]: any };
57+
nodeTransportsNames.forEach(elementName => {
58+
nodeTransports[elementName] = nodeSDK.Transports[elementName as keyof typeof nodeSDK.Transports];
59+
});
60+
reactTransportsNames.forEach(elementName => {
61+
reactTransports[elementName] = reactSDK.Transports[elementName as keyof typeof reactSDK.Transports];
62+
});
63+
export const Transports = { ...nodeTransports, ...reactTransports };
64+
const nodeIntegrationsNames = [
65+
'FunctionToString',
66+
'InboundFilters',
67+
'Console',
68+
'Http',
69+
'OnUncaughtException',
70+
'OnUnhandledRejection',
71+
'LinkedErrors',
72+
'Modules',
73+
];
74+
const reactIntegrationsNames = ['GlobalHandlers', 'TryCatch', 'Breadcrumbs', 'UserAgent'];
75+
const nodeIntegrations = {} as { [key: string]: any };
76+
const reactIntegrations = {} as { [key: string]: any };
77+
nodeIntegrationsNames.forEach(elementName => {
78+
nodeIntegrations[elementName] = nodeSDK.Integrations[elementName as keyof typeof nodeSDK.Integrations];
79+
});
80+
reactIntegrationsNames.forEach(elementName => {
81+
reactIntegrations[elementName] = reactSDK.Integrations[elementName as keyof typeof reactSDK.Integrations];
82+
});
83+
export const Integrations = { ...nodeIntegrations, ...reactIntegrations };
3384
export { BrowserClient } from '@sentry/react';
3485
export { injectReportDialog } from '@sentry/react';
3586
export { eventFromException } from '@sentry/react';

0 commit comments

Comments
 (0)
0