react-native-navigation的迁移库

appDelegateLinker.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // @ts-check
  2. var path = require('./path');
  3. var fs = require("fs");
  4. var {warnn, logn, infon, debugn} = require("./log");
  5. class ApplicationLinker {
  6. constructor() {
  7. this.appDelegatePath = path.appDelegate;
  8. }
  9. link() {
  10. if (this.appDelegatePath) {
  11. logn("Linking AppDelegate...");
  12. var appDelegateContents = fs.readFileSync(this.appDelegatePath, "utf8");
  13. if (this._doesBootstrapNavigation(appDelegateContents)) {
  14. infon("AppDelegate already linked\n");
  15. return;
  16. }
  17. appDelegateContents = this._removeUnneededImports(appDelegateContents);
  18. appDelegateContents = this._importNavigation(appDelegateContents);
  19. appDelegateContents = this._bootstrapNavigation(appDelegateContents);
  20. appDelegateContents = this._removeApplicationLaunchContent(appDelegateContents);
  21. fs.writeFileSync(this.appDelegatePath, appDelegateContents);
  22. infon("AppDelegate linked successfully!\n");
  23. }
  24. }
  25. _doesBootstrapNavigation(applicationContent) {
  26. return /ReactNativeNavigation\s+bootstrap/.test(applicationContent);
  27. }
  28. _removeUnneededImports(applicationContent) {
  29. const unneededImports = [/\#import\s+\<React\/RCTBridge.h>\s/, /#import\s+\<React\/RCTRootView.h>\s/];
  30. debugn(" Removing Unneeded imports");
  31. unneededImports.forEach(unneededImport => {
  32. if (unneededImport.test(applicationContent)) {
  33. applicationContent = applicationContent.replace(unneededImport, "");
  34. }
  35. });
  36. return applicationContent;
  37. }
  38. _importNavigation(applicationContent) {
  39. if (!this._doesImportNavigation(applicationContent)) {
  40. debugn(" ");
  41. return applicationContent
  42. .replace(/#import\s+"AppDelegate.h"/, "#import \"AppDelegate.h\"\n#import <ReactNativeNavigation/ReactNativeNavigation.h>")
  43. }
  44. warnn(" AppDelegate already imports Navigation");
  45. return applicationContent;
  46. }
  47. _removeApplicationLaunchContent(applicationContent) {
  48. const toRemove = [/RCTRootView\s+\*rootView((.|\r|\s)*?)];\s+/, /rootView.backgroundColor((.|\r)*)];\s+/,
  49. /self.window((.|\r)*)];\s+/, /UIViewController\s\*rootViewController((.|\r)*)];\s+/, /rootViewController\.view\s+=\s+rootView;\s+/,
  50. /self.window.rootViewController\s+=\s+rootViewController;\s+/, /\[self.window\s+makeKeyAndVisible];\s+/
  51. ]
  52. toRemove.forEach(element => {
  53. if (element.test(applicationContent)) {
  54. applicationContent = applicationContent.replace(element, "");
  55. }
  56. });
  57. return applicationContent;
  58. }
  59. _bootstrapNavigation(applicationContent) {
  60. return applicationContent.replace(/RCTBridge.*];/, "[ReactNativeNavigation bootstrapWithDelegate:self launchOptions:launchOptions];");
  61. }
  62. _doesImportNavigation(applicationContent) {
  63. return /#import\s+\<ReactNativeNavigation\/ReactNativeNavigation.h>/.test(applicationContent);
  64. }
  65. }
  66. module.exports = ApplicationLinker;