-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhandleOnBuildEnd.ts
113 lines (106 loc) · 3.87 KB
/
handleOnBuildEnd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { rm } from 'node:fs/promises';
import type { Config } from '@react-router/dev/dist/config';
import SentryCli from '@sentry/cli';
import { glob } from 'glob';
import type { SentryReactRouterBuildOptions } from '../types';
type BuildEndHook = NonNullable<Config['buildEnd']>;
function getSentryConfig(viteConfig: unknown): SentryReactRouterBuildOptions {
if (!viteConfig || typeof viteConfig !== 'object' || !('sentryConfig' in viteConfig)) {
// eslint-disable-next-line no-console
console.error('[Sentry] sentryConfig not found - it needs to be passed to vite.config.ts');
}
return (viteConfig as { sentryConfig: SentryReactRouterBuildOptions }).sentryConfig;
}
/**
* A build end hook that handles Sentry release creation and source map uploads.
* It creates a new Sentry release if configured, uploads source maps to Sentry,
* and optionally deletes the source map files after upload.
*/
export const sentryOnBuildEnd: BuildEndHook = async ({ reactRouterConfig, viteConfig }) => {
const {
authToken,
org,
project,
release,
sourceMapsUploadOptions = { enabled: true },
debug = false,
} = getSentryConfig(viteConfig);
const cliInstance = new SentryCli(null, {
authToken,
org,
project,
});
// check if release should be created
if (release?.name) {
try {
await cliInstance.releases.new(release.name);
} catch (error) {
// eslint-disable-next-line no-console
console.error('[Sentry] Could not create release', error);
}
}
if (sourceMapsUploadOptions?.enabled ?? (true && viteConfig.build.sourcemap !== false)) {
// inject debugIds
try {
await cliInstance.execute(['sourcemaps', 'inject', reactRouterConfig.buildDirectory], debug);
} catch (error) {
// eslint-disable-next-line no-console
console.error('[Sentry] Could not inject debug ids', error);
}
// upload sourcemaps
try {
await cliInstance.releases.uploadSourceMaps(release?.name || 'undefined', {
include: [
{
paths: [reactRouterConfig.buildDirectory],
},
],
});
} catch (error) {
// eslint-disable-next-line no-console
console.error('[Sentry] Could not upload sourcemaps', error);
}
}
// delete sourcemaps after upload
let updatedFilesToDeleteAfterUpload = sourceMapsUploadOptions?.filesToDeleteAfterUpload;
// set a default value no option was set
if (typeof sourceMapsUploadOptions?.filesToDeleteAfterUpload === 'undefined') {
updatedFilesToDeleteAfterUpload = [`${reactRouterConfig.buildDirectory}/**/*.map`];
if (debug) {
// eslint-disable-next-line no-console
console.info(
`[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
updatedFilesToDeleteAfterUpload,
)}\` to delete generated source maps after they were uploaded to Sentry.`,
);
}
}
if (updatedFilesToDeleteAfterUpload) {
try {
const filePathsToDelete = await glob(updatedFilesToDeleteAfterUpload, {
absolute: true,
nodir: true,
});
if (debug) {
filePathsToDelete.forEach(filePathToDelete => {
// eslint-disable-next-line no-console
console.info(`Deleting asset after upload: ${filePathToDelete}`);
});
}
await Promise.all(
filePathsToDelete.map(filePathToDelete =>
rm(filePathToDelete, { force: true }).catch((e: unknown) => {
if (debug) {
// This is allowed to fail - we just don't do anything
// eslint-disable-next-line no-console
console.debug(`An error occurred while attempting to delete asset: ${filePathToDelete}`, e);
}
}),
),
);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error deleting files after sourcemap upload:', error);
}
}
};