Skip to content
Source
Sign inSign up

upload-sourcemaps.mts

61 lines2.2 KB

1#!/usr/bin/env node
2// Usage: `ota <dist>` after `eas update --emit-metadata`, or `build` from eas-build-on-success.
3// Problems are warnings, never failures: an unreadable stack is not worth failing a release over.
4import {
5 buildDsymUpload,
6 buildSourceMapUpload,
7 otaSourceMapUploads,
8 parseCredentials,
9 uploadDsym,
10 uploadSourceMap,
11 type SourceMapUpload,
12} from "./sourcemap-upload.mts";
13​
14const warn = (message: string) => {
15 // GitHub Actions shows this as an annotation; elsewhere it is a log line.
16 console.warn(process.env.GITHUB_ACTIONS ? `::warning::${message}` : `warning: ${message}`);
17};
18​
19const errorMessage = (error: unknown) => (error instanceof Error ? error.message : String(error));
20​
21async function main() {
22 const [mode, distDir] = process.argv.slice(2);
23 const credentials = parseCredentials(process.env.MOBILE_SOURCEMAPS_R2);
24 if (!credentials) {
25 warn("MOBILE_SOURCEMAPS_R2 is not set; crash stacks from this bundle stay minified.");
26 return;
27 }
28 let uploads: ReadonlyArray<SourceMapUpload>;
29 if (mode === "ota" && distDir) {
30 uploads = otaSourceMapUploads(distDir);
31 } else if (mode === "build") {
32 uploads = [buildSourceMapUpload(process.env, process.cwd())];
33 } else {
34 throw new Error("Usage: upload-sourcemaps.mts ota <dist-dir> | build");
35 }
36 if (uploads.length === 0) warn("Found no source maps to upload.");
37 for (const upload of uploads) {
38 try {
39 const bytes = await uploadSourceMap(credentials, upload);
40 console.log(`Uploaded ${upload.key} (${(bytes / 1e6).toFixed(1)} MB)`);
41 } catch (error) {
42 warn(`Could not upload ${upload.key}: ${errorMessage(error)}`);
43 }
44 }
45 if (mode !== "build" || process.env.EAS_BUILD_PLATFORM !== "ios") return;
46 const dsym = buildDsymUpload(process.env, process.cwd());
47 if (!dsym) {
48 warn("Found no dSYM under ios/build; native iOS frames from this build stay unreadable.");
49 return;
50 }
51 try {
52 const bytes = await uploadDsym(credentials, dsym);
53 console.log(`Uploaded ${dsym.key} (${(bytes / 1e6).toFixed(1)} MB)`);
54 } catch (error) {
55 warn(`Could not upload ${dsym.key}: ${errorMessage(error)}`);
56 }
58​
59await main().catch((error: unknown) => {
60 warn(`Source map upload skipped: ${errorMessage(error)}`);
61});