Skip to content

Commit d50d215

Browse files
committed
Code separation
1 parent ca650bd commit d50d215

4 files changed

Lines changed: 114 additions & 189 deletions

File tree

packages/core/scripts/eas-build-on-complete.js

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -26,79 +26,20 @@
2626
* @see https://docs.sentry.io/platforms/react-native/
2727
*/
2828

29-
const path = require('path');
30-
const fs = require('fs');
31-
32-
// Try to load environment variables
33-
function loadEnv() {
34-
// Try @expo/env first
35-
try {
36-
require('@expo/env').load('.');
37-
} catch (_e) {
38-
// Fallback to dotenv if available
39-
try {
40-
const dotenvPath = path.join(process.cwd(), '.env');
41-
if (fs.existsSync(dotenvPath)) {
42-
const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8');
43-
const dotenv = require('dotenv');
44-
Object.assign(process.env, dotenv.parse(dotenvFile));
45-
}
46-
} catch (_e2) {
47-
// No dotenv available, continue with existing env vars
48-
}
49-
}
50-
51-
// Also load .env.sentry-build-plugin if it exists
52-
try {
53-
const sentryEnvPath = path.join(process.cwd(), '.env.sentry-build-plugin');
54-
if (fs.existsSync(sentryEnvPath)) {
55-
const dotenvFile = fs.readFileSync(sentryEnvPath, 'utf-8');
56-
const dotenv = require('dotenv');
57-
Object.assign(process.env, dotenv.parse(dotenvFile));
58-
}
59-
} catch (_e) {
60-
// Continue without .env.sentry-build-plugin
61-
}
62-
}
29+
const { loadEnv, loadHooksModule, parseBaseOptions, runHook } = require('./eas-build-utils');
6330

6431
async function main() {
6532
loadEnv();
6633

67-
// Dynamically import the hooks module (it's compiled to dist/)
68-
let captureEASBuildComplete;
69-
try {
70-
// Try the compiled output first
71-
const hooks = require('../dist/js/tools/easBuildHooks.js');
72-
captureEASBuildComplete = hooks.captureEASBuildComplete;
73-
} catch (_e) {
74-
console.error('[Sentry] Could not load EAS build hooks module. Make sure @sentry/react-native is properly installed.');
75-
process.exit(1);
76-
}
77-
78-
// Parse options from environment variables
34+
const hooks = loadHooksModule();
7935
const options = {
80-
dsn: process.env.SENTRY_DSN,
36+
...parseBaseOptions(),
8137
errorMessage: process.env.SENTRY_EAS_BUILD_ERROR_MESSAGE,
8238
successMessage: process.env.SENTRY_EAS_BUILD_SUCCESS_MESSAGE,
8339
captureSuccessfulBuilds: process.env.SENTRY_EAS_BUILD_CAPTURE_SUCCESS === 'true',
8440
};
8541

86-
// Parse additional tags if provided
87-
if (process.env.SENTRY_EAS_BUILD_TAGS) {
88-
try {
89-
options.tags = JSON.parse(process.env.SENTRY_EAS_BUILD_TAGS);
90-
} catch (_e) {
91-
console.warn('[Sentry] Could not parse SENTRY_EAS_BUILD_TAGS as JSON. Ignoring.');
92-
}
93-
}
94-
95-
try {
96-
await captureEASBuildComplete(options);
97-
console.log('[Sentry] EAS build complete hook finished.');
98-
} catch (error) {
99-
console.error('[Sentry] Error in eas-build-on-complete hook:', error);
100-
// Don't fail the build hook itself
101-
}
42+
await runHook('on-complete', () => hooks.captureEASBuildComplete(options));
10243
}
10344

10445
main();

packages/core/scripts/eas-build-on-error.js

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,77 +18,18 @@
1818
* @see https://docs.sentry.io/platforms/react-native/
1919
*/
2020

21-
const path = require('path');
22-
const fs = require('fs');
23-
24-
// Try to load environment variables
25-
function loadEnv() {
26-
// Try @expo/env first
27-
try {
28-
require('@expo/env').load('.');
29-
} catch (_e) {
30-
// Fallback to dotenv if available
31-
try {
32-
const dotenvPath = path.join(process.cwd(), '.env');
33-
if (fs.existsSync(dotenvPath)) {
34-
const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8');
35-
const dotenv = require('dotenv');
36-
Object.assign(process.env, dotenv.parse(dotenvFile));
37-
}
38-
} catch (_e2) {
39-
// No dotenv available, continue with existing env vars
40-
}
41-
}
42-
43-
// Also load .env.sentry-build-plugin if it exists
44-
try {
45-
const sentryEnvPath = path.join(process.cwd(), '.env.sentry-build-plugin');
46-
if (fs.existsSync(sentryEnvPath)) {
47-
const dotenvFile = fs.readFileSync(sentryEnvPath, 'utf-8');
48-
const dotenv = require('dotenv');
49-
Object.assign(process.env, dotenv.parse(dotenvFile));
50-
}
51-
} catch (_e) {
52-
// Continue without .env.sentry-build-plugin
53-
}
54-
}
21+
const { loadEnv, loadHooksModule, parseBaseOptions, runHook } = require('./eas-build-utils');
5522

5623
async function main() {
5724
loadEnv();
5825

59-
// Dynamically import the hooks module (it's compiled to dist/)
60-
let captureEASBuildError;
61-
try {
62-
// Try the compiled output first
63-
const hooks = require('../dist/js/tools/easBuildHooks.js');
64-
captureEASBuildError = hooks.captureEASBuildError;
65-
} catch (_e) {
66-
console.error('[Sentry] Could not load EAS build hooks module. Make sure @sentry/react-native is properly installed.');
67-
process.exit(1);
68-
}
69-
70-
// Parse options from environment variables
26+
const hooks = loadHooksModule();
7127
const options = {
72-
dsn: process.env.SENTRY_DSN,
28+
...parseBaseOptions(),
7329
errorMessage: process.env.SENTRY_EAS_BUILD_ERROR_MESSAGE,
7430
};
7531

76-
// Parse additional tags if provided
77-
if (process.env.SENTRY_EAS_BUILD_TAGS) {
78-
try {
79-
options.tags = JSON.parse(process.env.SENTRY_EAS_BUILD_TAGS);
80-
} catch (_e) {
81-
console.warn('[Sentry] Could not parse SENTRY_EAS_BUILD_TAGS as JSON. Ignoring.');
82-
}
83-
}
84-
85-
try {
86-
await captureEASBuildError(options);
87-
console.log('[Sentry] EAS build error hook completed.');
88-
} catch (error) {
89-
console.error('[Sentry] Error in eas-build-on-error hook:', error);
90-
// Don't fail the build hook itself
91-
}
32+
await runHook('on-error', () => hooks.captureEASBuildError(options));
9233
}
9334

9435
main();

packages/core/scripts/eas-build-on-success.js

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,19 @@
1919
* @see https://docs.sentry.io/platforms/react-native/
2020
*/
2121

22-
const path = require('path');
23-
const fs = require('fs');
24-
25-
// Try to load environment variables
26-
function loadEnv() {
27-
// Try @expo/env first
28-
try {
29-
require('@expo/env').load('.');
30-
} catch (_e) {
31-
// Fallback to dotenv if available
32-
try {
33-
const dotenvPath = path.join(process.cwd(), '.env');
34-
if (fs.existsSync(dotenvPath)) {
35-
const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8');
36-
const dotenv = require('dotenv');
37-
Object.assign(process.env, dotenv.parse(dotenvFile));
38-
}
39-
} catch (_e2) {
40-
// No dotenv available, continue with existing env vars
41-
}
42-
}
43-
44-
// Also load .env.sentry-build-plugin if it exists
45-
try {
46-
const sentryEnvPath = path.join(process.cwd(), '.env.sentry-build-plugin');
47-
if (fs.existsSync(sentryEnvPath)) {
48-
const dotenvFile = fs.readFileSync(sentryEnvPath, 'utf-8');
49-
const dotenv = require('dotenv');
50-
Object.assign(process.env, dotenv.parse(dotenvFile));
51-
}
52-
} catch (_e) {
53-
// Continue without .env.sentry-build-plugin
54-
}
55-
}
22+
const { loadEnv, loadHooksModule, parseBaseOptions, runHook } = require('./eas-build-utils');
5623

5724
async function main() {
5825
loadEnv();
5926

60-
// Dynamically import the hooks module (it's compiled to dist/)
61-
let captureEASBuildSuccess;
62-
try {
63-
// Try the compiled output first
64-
const hooks = require('../dist/js/tools/easBuildHooks.js');
65-
captureEASBuildSuccess = hooks.captureEASBuildSuccess;
66-
} catch (_e) {
67-
console.error('[Sentry] Could not load EAS build hooks module. Make sure @sentry/react-native is properly installed.');
68-
process.exit(1);
69-
}
70-
71-
// Parse options from environment variables
27+
const hooks = loadHooksModule();
7228
const options = {
73-
dsn: process.env.SENTRY_DSN,
29+
...parseBaseOptions(),
7430
successMessage: process.env.SENTRY_EAS_BUILD_SUCCESS_MESSAGE,
7531
captureSuccessfulBuilds: process.env.SENTRY_EAS_BUILD_CAPTURE_SUCCESS === 'true',
7632
};
7733

78-
// Parse additional tags if provided
79-
if (process.env.SENTRY_EAS_BUILD_TAGS) {
80-
try {
81-
options.tags = JSON.parse(process.env.SENTRY_EAS_BUILD_TAGS);
82-
} catch (_e) {
83-
console.warn('[Sentry] Could not parse SENTRY_EAS_BUILD_TAGS as JSON. Ignoring.');
84-
}
85-
}
86-
87-
try {
88-
await captureEASBuildSuccess(options);
89-
console.log('[Sentry] EAS build success hook completed.');
90-
} catch (error) {
91-
console.error('[Sentry] Error in eas-build-on-success hook:', error);
92-
// Don't fail the build hook itself
93-
}
34+
await runHook('on-success', () => hooks.captureEASBuildSuccess(options));
9435
}
9536

9637
main();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Shared utilities for EAS Build Hook scripts.
3+
*
4+
* @see https://docs.expo.dev/build-reference/npm-hooks/
5+
*/
6+
7+
const path = require('path');
8+
const fs = require('fs');
9+
10+
/**
11+
* Loads environment variables from various sources:
12+
* - @expo/env (if available)
13+
* - .env file (via dotenv, if available)
14+
* - .env.sentry-build-plugin file
15+
*/
16+
function loadEnv() {
17+
// Try @expo/env first
18+
try {
19+
require('@expo/env').load('.');
20+
} catch (_e) {
21+
// Fallback to dotenv if available
22+
try {
23+
const dotenvPath = path.join(process.cwd(), '.env');
24+
if (fs.existsSync(dotenvPath)) {
25+
const dotenvFile = fs.readFileSync(dotenvPath, 'utf-8');
26+
const dotenv = require('dotenv');
27+
Object.assign(process.env, dotenv.parse(dotenvFile));
28+
}
29+
} catch (_e2) {
30+
// No dotenv available, continue with existing env vars
31+
}
32+
}
33+
34+
// Also load .env.sentry-build-plugin if it exists
35+
try {
36+
const sentryEnvPath = path.join(process.cwd(), '.env.sentry-build-plugin');
37+
if (fs.existsSync(sentryEnvPath)) {
38+
const dotenvFile = fs.readFileSync(sentryEnvPath, 'utf-8');
39+
const dotenv = require('dotenv');
40+
Object.assign(process.env, dotenv.parse(dotenvFile));
41+
}
42+
} catch (_e) {
43+
// Continue without .env.sentry-build-plugin
44+
}
45+
}
46+
47+
/**
48+
* Loads the EAS build hooks module from the compiled output.
49+
* @returns {object} The hooks module exports
50+
* @throws {Error} If the module cannot be loaded
51+
*/
52+
function loadHooksModule() {
53+
try {
54+
return require('../dist/js/tools/easBuildHooks.js');
55+
} catch (_e) {
56+
console.error('[Sentry] Could not load EAS build hooks module. Make sure @sentry/react-native is properly installed.');
57+
process.exit(1);
58+
}
59+
}
60+
61+
/**
62+
* Parses common options from environment variables.
63+
* @returns {object} Parsed options object
64+
*/
65+
function parseBaseOptions() {
66+
const options = {
67+
dsn: process.env.SENTRY_DSN,
68+
};
69+
70+
// Parse additional tags if provided
71+
if (process.env.SENTRY_EAS_BUILD_TAGS) {
72+
try {
73+
options.tags = JSON.parse(process.env.SENTRY_EAS_BUILD_TAGS);
74+
} catch (_e) {
75+
console.warn('[Sentry] Could not parse SENTRY_EAS_BUILD_TAGS as JSON. Ignoring.');
76+
}
77+
}
78+
79+
return options;
80+
}
81+
82+
/**
83+
* Wraps an async hook function with error handling.
84+
* @param {string} hookName - Name of the hook for logging
85+
* @param {Function} hookFn - Async function to execute
86+
*/
87+
async function runHook(hookName, hookFn) {
88+
try {
89+
await hookFn();
90+
console.log(`[Sentry] EAS build ${hookName} hook completed.`);
91+
} catch (error) {
92+
console.error(`[Sentry] Error in eas-build-${hookName} hook:`, error);
93+
// Don't fail the build hook itself
94+
}
95+
}
96+
97+
module.exports = {
98+
loadEnv,
99+
loadHooksModule,
100+
parseBaseOptions,
101+
runHook,
102+
};

0 commit comments

Comments
 (0)