-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathrn.patch.app.js
More file actions
executable file
·63 lines (53 loc) · 1.96 KB
/
rn.patch.app.js
File metadata and controls
executable file
·63 lines (53 loc) · 1.96 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { argv, env } = require('process');
const parseArgs = require('minimist');
const { debug } = require('@sentry/core');
debug.enable();
const SENTRY_RELEASE = env.SENTRY_RELEASE;
const SENTRY_DIST = env.SENTRY_DIST;
const args = parseArgs(argv.slice(2));
if (!args.app) {
throw new Error('Missing --app');
}
debug.log('Patching RN App.(js|tsx)', args.app);
const initPatch = `
import * as Sentry from '@sentry/react-native';
import { EndToEndTestsScreen } from 'sentry-react-native-e2e-tests';
import { LaunchArguments } from "react-native-launch-arguments";
Sentry.init({
release: '${SENTRY_RELEASE}',
dist: '${SENTRY_DIST}',
dsn: 'https://1df17bd4e543fdb31351dee1768bb679@o447951.ingest.sentry.io/5428561',
replaysOnErrorSampleRate: LaunchArguments.value().replaysOnErrorSampleRate,
integrations: [
Sentry.mobileReplayIntegration(),
Sentry.feedbackIntegration({
enableTakeScreenshot: true,
}),
],
});
`;
const e2eComponentPatch = '<EndToEndTestsScreen />';
const lastImportRex = /^([^]*)(import\s+[^;]*?;$)/m;
const patchRex = '@sentry/react-native';
// Support both older RN versions with ScrollView and newer versions with NewAppScreen
const headerComponentRex = /(<ScrollView|<NewAppScreen)/gm;
const exportDefaultRex = /export\s+default\s+App;/m;
const jsPath = path.join(args.app, 'App.js');
const tsxPath = path.join(args.app, 'App.tsx');
const jsExists = fs.existsSync(jsPath);
const appPath = jsExists ? jsPath : tsxPath;
const app = fs.readFileSync(appPath, 'utf8');
const isPatched = app.match(patchRex);
if (!isPatched) {
const patched = app
.replace(lastImportRex, m => m + initPatch)
.replace(headerComponentRex, m => e2eComponentPatch + m)
.replace(exportDefaultRex, 'export default Sentry.wrap(App);');
fs.writeFileSync(appPath, patched);
debug.log('Patched RN App.(js|tsx) successfully!');
} else {
debug.log('App.(js|tsx) already patched!');
}