|
| 1 | +let glob = require('glob'); |
| 2 | +let fs = require('fs'); |
| 3 | +let inquirer = require('inquirer'); |
| 4 | + |
| 5 | +let OBJC_HEADER = '\ |
| 6 | +#if __has_include(<React/RNSentry.h>)\n\ |
| 7 | +#import <React/RNSentry.h> // This is used for versions of react >= 0.40\n\ |
| 8 | +#else\n\ |
| 9 | +#import "RNSentry.h" // This is used for versions of react < 0.40\n\ |
| 10 | +#endif'; |
| 11 | + |
| 12 | +let cachedDsn = null; |
| 13 | + |
| 14 | +function getDsn() { |
| 15 | + if (cachedDsn !== null) { |
| 16 | + return Promise.resolve(cachedDsn); |
| 17 | + } |
| 18 | + |
| 19 | + return inquirer.prompt([{ |
| 20 | + type: 'input', |
| 21 | + default: 'YOUR_DSN_HERE', |
| 22 | + message: 'The DSN for your mobile application', |
| 23 | + name: 'dsn', |
| 24 | + }]).then(function(answers) { |
| 25 | + cachedDsn = answers.dsn; |
| 26 | + return Promise.resolve(answers.dsn); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +function patchAppDelegate(contents) { |
| 31 | + // add the header if it's not there yet. |
| 32 | + if (!contents.match(/#import "RNSentry.h"/)) { |
| 33 | + contents = contents.replace( |
| 34 | + /(#import <React\/RCTRootView.h>)/, |
| 35 | + '$1\n' + OBJC_HEADER |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + // add root view init. |
| 40 | + let rootViewMatch = contents.match(/RCTRootView\s*\*\s*([^\s=]+)\s*=\s*\[/); |
| 41 | + if (rootViewMatch) { |
| 42 | + let rootViewInit = '[RNSentry installWithRootView:' + rootViewMatch[1] + '];'; |
| 43 | + if (contents.indexOf(rootViewInit) < 0) { |
| 44 | + contents = contents.replace( |
| 45 | + /^(\s*)RCTRootView\s*\*\s*[^\s=]+\s*=\s*\[([^]*?\s*\]\s*;\s*$)/m, |
| 46 | + function(match, indent) { |
| 47 | + return match.trimRight() + '\n' + indent + rootViewInit + '\n'; |
| 48 | + } |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return Promise.resolve(contents); |
| 54 | +} |
| 55 | + |
| 56 | +function patchIndexJs(contents) { |
| 57 | + if (contents.match(/Sentry.config\(/)) { |
| 58 | + return Promise.resolve(contents); |
| 59 | + } |
| 60 | + |
| 61 | + return getDsn().then(function(dsn) { |
| 62 | + return Promise.resolve(contents.replace(/^([^]*)(import\s+[^;]*?;$)/m, function(match) { |
| 63 | + return match + '\n\nimport { Sentry } from \'react-native-sentry\';\n\n' + |
| 64 | + 'Sentry.config(' + JSON.stringify(dsn) + ').install();\n'; |
| 65 | + })); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +function patchBuildGradle(contents) { |
| 70 | + let applyFrom = 'apply from: "../../node_modules/react-native-sentry/sentry.gradle"'; |
| 71 | + if (contents.indexOf(applyFrom) >= 0) { |
| 72 | + return Promise.resolve(contents); |
| 73 | + } |
| 74 | + |
| 75 | + return Promise.resolve(contents.replace( |
| 76 | + /^apply from: "..\/..\/node_modules\/react-native\/react.gradle"/m, |
| 77 | + function(match) { |
| 78 | + return match + '\n' + applyFrom; |
| 79 | + } |
| 80 | + )); |
| 81 | +} |
| 82 | + |
| 83 | +function patchMatchingFile(pattern, func) { |
| 84 | + let matches = glob.sync(pattern, { |
| 85 | + ignore: 'node_modules/**' |
| 86 | + }); |
| 87 | + let rv = Promise.resolve(); |
| 88 | + matches.forEach(function(match) { |
| 89 | + let contents = fs.readFileSync(match, { |
| 90 | + encoding: 'utf-8' |
| 91 | + }); |
| 92 | + rv = rv.then(() => func(contents)).then(function(newContents) { |
| 93 | + if (contents != newContents) { |
| 94 | + fs.writeFileSync(match, newContents); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + return rv; |
| 99 | +} |
| 100 | + |
| 101 | +Promise.resolve() |
| 102 | + .then(() => patchMatchingFile('**/AppDelegate.m', patchAppDelegate)) |
| 103 | + .then(() => patchMatchingFile('index.*.js', patchIndexJs)) |
| 104 | + .then(() => patchMatchingFile('**/app/build.gradle', patchBuildGradle)) |
| 105 | + .catch(function(e) { |
| 106 | + console.log('Could not link react-native-sentry: ' + e); |
| 107 | + return Promise.resolve(); |
| 108 | + }); |
0 commit comments