-
Notifications
You must be signed in to change notification settings - Fork 84
/
upload-sourcemaps.ts
210 lines (184 loc) · 5.64 KB
/
upload-sourcemaps.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import spawnAsync from '@expo/spawn-async';
import path from 'path';
import rimraf from 'rimraf';
import mkdirp from 'mkdirp';
import fs from 'fs';
import sentryCliBinary from '@sentry/cli';
type ExpoSentryConfig = {
organization: string;
project: string;
authToken: string;
url: string;
} & SentryUploadOptions;
type SentryUploadOptions = {
release: string;
deployEnv: string | null;
setCommits: boolean | string;
useGlobalSentryCli: boolean;
distribution: string;
platform?: string | undefined;
};
type Options = {
log: (message: string) => void;
projectRoot: string;
androidManifest: Manifest;
androidBundle: string;
androidSourceMap: string;
iosManifest: Manifest;
iosSourceMap: string;
iosBundle: string;
config?: ExpoSentryConfig;
};
type Manifest = { revisionId: string; version: string };
module.exports = async (options: Options) => {
let {
config,
log,
iosBundle,
iosSourceMap,
iosManifest,
androidBundle,
androidSourceMap,
projectRoot,
} = options;
const tmpdir = path.resolve(projectRoot, '.tmp', 'sentry');
rimraf.sync(tmpdir);
mkdirp.sync(tmpdir);
try {
// We use the same filenames for sourcemaps as Sentry does (even though the naming is unfortunate)
fs.writeFileSync(tmpdir + '/main.jsbundle', iosBundle, 'utf-8');
fs.writeFileSync(tmpdir + '/main.jsbundle.map', iosSourceMap, 'utf-8');
fs.writeFileSync(tmpdir + '/index.android.bundle', androidBundle, 'utf-8');
fs.writeFileSync(tmpdir + '/index.android.bundle.map', androidSourceMap, 'utf-8');
if (!config) {
log('No config found in app.json, falling back to environment variables...');
}
const childProcessEnv = Object.assign({}, process.env, {
SENTRY_ORG: (config && config.organization) || process.env.SENTRY_ORG,
SENTRY_PROJECT: (config && config.project) || process.env.SENTRY_PROJECT,
SENTRY_AUTH_TOKEN: (config && config.authToken) || process.env.SENTRY_AUTH_TOKEN,
SENTRY_URL: (config && config.url) || process.env.SENTRY_URL || 'https://sentry.io/',
});
const uploadOptions = getUploadOptions(config, process.env, iosManifest);
await createAndUploadRelease(uploadOptions, childProcessEnv, projectRoot, tmpdir, log);
} catch (e) {
log(messageForError(e));
log(
`Verify that your Sentry configuration in app.json is correct and refer to https://docs.expo.io/versions/latest/guides/using-sentry.html`
);
} finally {
rimraf.sync(tmpdir);
}
};
function getUploadOptions(
config: ExpoSentryConfig | undefined,
env: NodeJS.ProcessEnv,
manifest: Manifest
): SentryUploadOptions {
const { release, deployEnv, setCommits, useGlobalSentryCli, distribution } = config || {};
return {
release: release || env.SENTRY_RELEASE || manifest.revisionId,
deployEnv: deployEnv || env.SENTRY_DEPLOY_ENV || null,
setCommits: setCommits || env.SENTRY_SET_COMMITS || false,
useGlobalSentryCli: !!useGlobalSentryCli,
distribution: distribution || env.SENTRY_DIST || manifest.version,
};
}
function extensionsForPlatform(platform: string = ''): string[] {
if (platform === 'android') {
return ['--ext', 'bundle', '--ext', 'map', '--ignore', 'main.jsbundle.map'];
} else if (platform === 'ios') {
return ['--ext', 'jsbundle', '--ext', 'map', '--ignore', 'index.android.bundle.map'];
} else {
// Otherwise let's just pass in all the sourcemap files
return ['--ext', 'jsbundle', '--ext', 'bundle', '--ext', 'map'];
}
}
async function createAndUploadRelease(
userOptions: SentryUploadOptions,
childProcessEnv: NodeJS.ProcessEnv,
projectRoot: string,
tmpdir: string,
log: (message: string) => void
) {
const {
useGlobalSentryCli,
release,
distribution,
setCommits,
deployEnv,
platform,
} = userOptions;
const sentryCliBinaryPath = useGlobalSentryCli ? 'sentry-cli' : sentryCliBinary.getPath();
let output;
let createReleaseResult = await spawnAsync(sentryCliBinaryPath, ['releases', 'new', release], {
cwd: tmpdir,
env: childProcessEnv,
});
output = createReleaseResult.stdout.toString();
log(output);
let uploadResult = await spawnAsync(
sentryCliBinaryPath,
[
'releases',
'files',
release,
'upload-sourcemaps',
'.',
...extensionsForPlatform(platform),
'--rewrite',
'--dist',
distribution,
'--strip-prefix',
projectRoot,
],
{
cwd: tmpdir,
env: childProcessEnv,
}
);
output = uploadResult.stdout.toString();
log(output);
if (setCommits) {
let commitsResult = await spawnAsync(
sentryCliBinaryPath,
['releases', 'set-commits', '--auto', release],
{
env: childProcessEnv,
}
);
output = commitsResult.stdout.toString();
log(output);
}
let finalizeReleaseResult = await spawnAsync(
sentryCliBinaryPath,
['releases', 'finalize', release],
{
env: childProcessEnv,
}
);
output = finalizeReleaseResult.stdout.toString();
log(output);
if (deployEnv) {
let deployResult = await spawnAsync(
sentryCliBinaryPath,
['releases', 'deploys', release, 'new', '-e', deployEnv],
{
env: childProcessEnv,
}
);
// filter out unnamed deloy
output = deployResult.stdout.toString().replace('unnamed ', '');
log(output);
}
}
function messageForError(e: Error & { stderr?: string }) {
let message = e.stderr ? e.stderr.replace(/^\s+|\s+$/g, '') : e.message;
if (message) {
if (message.indexOf('error: ') === 0) {
message = message.replace('error: ', '');
}
return `Error uploading sourcemaps to Sentry: ${message}`;
}
return 'Error uploading sourcemaps to Sentry';
}