forked from getsentry/sentry-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpo-upload-sourcemaps.js
More file actions
executable file
·203 lines (179 loc) · 6.36 KB
/
expo-upload-sourcemaps.js
File metadata and controls
executable file
·203 lines (179 loc) · 6.36 KB
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
#!/usr/bin/env node
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const process = require('process');
const SENTRY_ORG = 'SENTRY_ORG';
const SENTRY_PROJECT = 'SENTRY_PROJECT';
const SENTRY_AUTH_TOKEN = 'SENTRY_AUTH_TOKEN';
const SENTRY_CLI_EXECUTABLE = 'SENTRY_CLI_EXECUTABLE';
function getEnvVar(varname) {
return process.env[varname];
}
function getSentryPluginPropertiesFromExpoConfig() {
try {
const stdOutBuffer = execSync('npx expo config --json');
const config = JSON.parse(stdOutBuffer.toString());
const plugins = config.plugins;
if (!plugins) {
return null;
}
const sentryPlugin = plugins.find(plugin => {
if (!Array.isArray(plugin) || plugin.length < 2) {
return false;
}
const [pluginName] = plugin;
return pluginName === '@sentry/react-native/expo';
});
if (!sentryPlugin) {
return null;
}
const [, pluginConfig] = sentryPlugin;
return pluginConfig;
} catch (error) {
console.error('Error fetching expo config:', error);
return null;
}
}
function readAndPrintJSONFile(filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(`The file "${filePath}" does not exist.`);
}
try {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
} catch (err) {
console.error('Error reading or parsing JSON file:', err);
throw err;
}
}
function writeJSONFile(filePath, object) {
// Convert the updated JavaScript object back to a JSON string
const updatedJsonString = JSON.stringify(object, null, 2);
fs.writeFileSync(filePath, updatedJsonString, 'utf8', writeErr => {
if (writeErr) {
console.error('Error writing to the file:', writeErr);
} else {
console.log('File updated successfully.');
}
});
}
function isAsset(filename) {
return filename.endsWith('.map') || filename.endsWith('.js') || filename.endsWith('.hbc');
}
function getAssetPathsSync(directory) {
const files = [];
const items = fs.readdirSync(directory, { withFileTypes: true });
for (const item of items) {
const fullPath = path.join(directory, item.name);
if (item.isDirectory()) {
// eslint-disable-next-line no-unused-vars
files.push(...getAssetPathsSync(fullPath));
} else if (item.isFile() && isAsset(item.name)) {
files.push(fullPath);
}
}
return files;
}
function groupAssets(assetPaths) {
const groups = {};
for (const assetPath of assetPaths) {
const parsedPath = path.parse(assetPath);
const extname = parsedPath.ext;
const assetGroupName = extname === '.map' ? path.join(parsedPath.dir, parsedPath.name) : path.format(parsedPath);
if (!groups[assetGroupName]) {
groups[assetGroupName] = [assetPath];
} else {
groups[assetGroupName].push(assetPath);
}
}
return groups;
}
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; // Ensures precedence .env.development > .env (the same as @expo/cli)
const projectRoot = '.'; // Assume script is run from the project root
try {
require('@expo/env').load(projectRoot);
} catch (error) {
console.warn('⚠️ Failed to load environment variables using @expo/env.');
console.warn(error);
}
let sentryOrg = getEnvVar(SENTRY_ORG);
let sentryProject = getEnvVar(SENTRY_PROJECT);
let authToken = getEnvVar(SENTRY_AUTH_TOKEN);
const sentryCliBin = getEnvVar(SENTRY_CLI_EXECUTABLE) || require.resolve('@sentry/cli/bin/sentry-cli');
if (!sentryOrg || !sentryProject) {
console.log('🐕 Fetching from expo config...');
const pluginConfig = getSentryPluginPropertiesFromExpoConfig();
if (!pluginConfig) {
console.error("Could not fetch '@sentry/react-native' plugin properties from expo config.");
process.exit(1);
}
if (!sentryOrg) {
if (!pluginConfig.organization) {
console.error(
`Could not resolve sentry org, set it in the environment variable ${SENTRY_ORG} or in the '@sentry/react-native' plugin properties in your expo config.`,
);
process.exit(1);
}
sentryOrg = pluginConfig.organization;
console.log(`${SENTRY_ORG} resolved to ${sentryOrg} from expo config.`);
}
if (!sentryProject) {
if (!pluginConfig.project) {
console.error(
`Could not resolve sentry project, set it in the environment variable ${SENTRY_PROJECT} or in the '@sentry/react-native' plugin properties in your expo config.`,
);
process.exit(1);
}
sentryProject = pluginConfig.project;
console.log(`${SENTRY_PROJECT} resolved to ${sentryProject} from expo config.`);
}
}
if (!authToken) {
console.error(`${SENTRY_AUTH_TOKEN} environment variable must be set.`);
process.exit(1);
}
const outputDir = process.argv[2];
if (!outputDir) {
console.error('Provide the directory with your bundles and sourcemaps as the first argument.');
console.error('Example: node node_modules/@sentry/react-native/scripts/expo-upload-sourcemaps dist');
process.exit(1);
}
const files = getAssetPathsSync(outputDir);
const groupedAssets = groupAssets(files);
const totalAssets = Object.keys(groupedAssets).length;
let numAssetsUploaded = 0;
for (const [assetGroupName, assets] of Object.entries(groupedAssets)) {
const sourceMapPath = assets.find(asset => asset.endsWith('.map'));
if (sourceMapPath) {
const sourceMap = readAndPrintJSONFile(sourceMapPath);
if (sourceMap.debugId) {
sourceMap.debug_id = sourceMap.debugId;
}
writeJSONFile(sourceMapPath, sourceMap);
console.log(`⬆️ Uploading ${assetGroupName} bundle and sourcemap...`);
} else {
console.log(`❓ Sourcemap for ${assetGroupName} not found, skipping...`);
continue;
}
const isHermes = assets.find(asset => asset.endsWith('.hbc'));
const windowsCallback = process.platform === "win32" ? 'node ' : '';
execSync(`${windowsCallback}${sentryCliBin} sourcemaps upload ${isHermes ? '--debug-id-reference' : ''} ${assets.join(' ')}`, {
env: {
...process.env,
[SENTRY_PROJECT]: sentryProject,
[SENTRY_ORG]: sentryOrg,
},
stdio: 'inherit',
});
numAssetsUploaded++;
}
if (numAssetsUploaded === totalAssets) {
console.log('✅ Uploaded bundles and sourcemaps to Sentry successfully.');
} else {
console.warn(
`⚠️ Uploaded ${numAssetsUploaded} of ${totalAssets} bundles and sourcemaps. ${
numAssetsUploaded === 0 ? 'Ensure you are running `expo export` with the `--dump-sourcemap` flag.' : ''
}`,
);
}