-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy patheas-build-hook.js
More file actions
executable file
·225 lines (200 loc) · 6.93 KB
/
Copy patheas-build-hook.js
File metadata and controls
executable file
·225 lines (200 loc) · 6.93 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env node
/**
* EAS Build Hook
*
* Unified entry point for all EAS build hooks (on-complete, on-error, on-success).
* The hook name is determined from the bin command name in process.argv[1]
* (e.g. sentry-eas-build-on-error → on-error) or can be passed as a CLI argument.
*
* Required environment variables:
* - SENTRY_DSN: Your Sentry DSN
*
* Optional environment variables:
* - SENTRY_EAS_BUILD_CAPTURE_SUCCESS: Set to 'true' to also capture successful builds
* - SENTRY_EAS_BUILD_TAGS: JSON string of additional tags
* - SENTRY_EAS_BUILD_ERROR_MESSAGE: Custom error message for failed builds
* - SENTRY_EAS_BUILD_SUCCESS_MESSAGE: Custom success message for successful builds
*
* @see https://docs.expo.dev/build-reference/npm-hooks/
* @see https://docs.sentry.io/platforms/react-native/
*/
/* eslint-disable no-console */
const path = require('path');
const fs = require('fs');
// ─── Environment loading ─────────────────────────────────────────────────────
/**
* Merges parsed env vars into process.env without overwriting existing values.
* This preserves EAS secrets and other pre-set environment variables.
* @param {object} parsed - Parsed environment variables from dotenv
*/
function mergeEnvWithoutOverwrite(parsed) {
for (const key of Object.keys(parsed)) {
if (process.env[key] === undefined) {
process.env[key] = parsed[key];
}
}
}
/**
* Loads environment variables from various sources:
* - @expo/env (if available)
* - .env file (via dotenv, if available)
* - .env.sentry-build-plugin file
*
* NOTE: Existing environment variables (like EAS secrets) are NOT overwritten.
*/
function loadEnv() {
// Try @expo/env first
try {
require('@expo/env').load('.');
} catch (_e) {
// Fallback to dotenv if available
try {
const dotenvPath = path.join(process.cwd(), '.env');
if (fs.existsSync(dotenvPath)) {
const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8');
const dotenv = require('dotenv');
mergeEnvWithoutOverwrite(dotenv.parse(dotenvFile));
}
} catch (_e2) {
// No dotenv available, continue with existing env vars
}
}
// Also load .env.sentry-build-plugin if it exists
try {
const sentryEnvPath = path.join(process.cwd(), '.env.sentry-build-plugin');
if (fs.existsSync(sentryEnvPath)) {
const dotenvFile = fs.readFileSync(sentryEnvPath, 'utf-8');
const dotenv = require('dotenv');
mergeEnvWithoutOverwrite(dotenv.parse(dotenvFile));
}
} catch (_e) {
// Continue without .env.sentry-build-plugin
}
}
// ─── Hooks module & options ──────────────────────────────────────────────────
/**
* Loads the EAS build hooks module from the compiled output.
* @returns {object} The hooks module exports
* @throws {Error} If the module cannot be loaded
*/
function loadHooksModule() {
try {
return require('../dist/js/tools/easBuildHooks.js');
} catch (_e) {
console.error('[Sentry] Could not load EAS build hooks module. Make sure @sentry/react-native is properly installed.');
process.exit(1);
}
}
/**
* Parses common options from environment variables.
* @returns {object} Parsed options object
*/
function parseBaseOptions() {
const options = {
dsn: process.env.SENTRY_DSN,
};
// Parse additional tags if provided
if (process.env.SENTRY_EAS_BUILD_TAGS) {
try {
const parsed = JSON.parse(process.env.SENTRY_EAS_BUILD_TAGS);
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
options.tags = parsed;
} else {
console.warn('[Sentry] SENTRY_EAS_BUILD_TAGS must be a JSON object (e.g., {"key":"value"}). Ignoring.');
}
} catch (_e) {
console.warn('[Sentry] Could not parse SENTRY_EAS_BUILD_TAGS as JSON. Ignoring.');
}
}
return options;
}
// ─── Hook configuration & execution ─────────────────────────────────────────
/**
* Hook configuration keyed by hook name.
*
* Each entry defines which extra env vars to read and which hooks module
* method to call.
*/
const HOOK_CONFIGS = {
'on-complete': {
envKeys: {
errorMessage: 'SENTRY_EAS_BUILD_ERROR_MESSAGE',
successMessage: 'SENTRY_EAS_BUILD_SUCCESS_MESSAGE',
captureSuccessfulBuilds: 'SENTRY_EAS_BUILD_CAPTURE_SUCCESS',
},
method: 'captureEASBuildComplete',
},
'on-error': {
envKeys: {
errorMessage: 'SENTRY_EAS_BUILD_ERROR_MESSAGE',
},
method: 'captureEASBuildError',
},
'on-success': {
envKeys: {
successMessage: 'SENTRY_EAS_BUILD_SUCCESS_MESSAGE',
captureSuccessfulBuilds: 'SENTRY_EAS_BUILD_CAPTURE_SUCCESS',
},
method: 'captureEASBuildSuccess',
},
};
/**
* Runs an EAS build hook by name.
*
* Loads the environment, resolves hook-specific options from env vars,
* and calls the corresponding hooks module method.
*
* @param {'on-complete' | 'on-error' | 'on-success'} hookName
*/
async function runEASBuildHook(hookName) {
const config = HOOK_CONFIGS[hookName];
if (!config) {
throw new Error(`Unknown EAS build hook: ${hookName}`);
}
loadEnv();
const hooks = loadHooksModule();
const options = parseBaseOptions();
for (const [optionKey, envKey] of Object.entries(config.envKeys)) {
if (optionKey === 'captureSuccessfulBuilds') {
options[optionKey] = process.env[envKey] === 'true';
} else if (process.env[envKey] !== undefined) {
options[optionKey] = process.env[envKey];
}
}
try {
await hooks[config.method](options);
console.log(`[Sentry] EAS build ${hookName} hook completed.`);
} catch (error) {
console.error(`[Sentry] Error in eas-build-${hookName} hook:`, error);
// Don't fail the build hook itself
}
}
// ─── Hook name resolution & entry point ─────────────────────────────────────
const HOOK_NAME_RE = /(?:sentry-eas-build-|build-)(on-(?:complete|error|success))/;
/**
* Resolves which hook to run.
*
* 1. Explicit CLI argument: `node build-hook.js on-error`
* 2. Derived from the script path in process.argv[1]
*/
function resolveHookName() {
const arg = process.argv[2];
if (arg && /^on-(complete|error|success)$/.test(arg)) {
return arg;
}
const caller = path.basename(process.argv[1] || '', '.js');
const match = caller.match(HOOK_NAME_RE);
if (match) {
return match[1];
}
console.error(
'[Sentry] Could not determine EAS build hook name. ' +
'Pass one of: on-complete, on-error, on-success',
);
process.exit(1);
}
const hookName = resolveHookName();
runEASBuildHook(hookName).catch(error => {
console.error(`[Sentry] Unexpected error in eas-build-${hookName} hook:`, error);
process.exit(1);
});