react-native-navigation的迁移库

activityLinker.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // @ts-check
  2. var path = require('./path');
  3. var fs = require("fs");
  4. var { warnn, logn, infon, debugn } = require("./log");
  5. class ActivityLinker {
  6. constructor() {
  7. this.activityPath = path.mainActivityJava;
  8. }
  9. link() {
  10. logn("Linking MainActivity...");
  11. if (this.activityPath) {
  12. var activityContent = fs.readFileSync(this.activityPath, "utf8");
  13. activityContent = this._extendNavigationActivity(activityContent);
  14. activityContent = this._removeGetMainComponentName(activityContent);
  15. fs.writeFileSync(this.activityPath, activityContent);
  16. infon("MainActivity linked successfully!\n");
  17. } else {
  18. warnn(" MainActivity not found!");
  19. }
  20. }
  21. _removeGetMainComponentName(contents) {
  22. var match = /\/\*\*\s*\n([^\*]|(\*(?!\/)))*\*\/\s*@Override\s*protected\s*String\s*getMainComponentName\s*\(\)\s*{\s*return.+\s*\}/.exec(contents);
  23. if (match) {
  24. debugn(" Removing getMainComponentName function");
  25. return contents.replace(/\/\*\*\s*\n([^\*]|(\*(?!\/)))*\*\/\s*@Override\s*protected\s*String\s*getMainComponentName\s*\(\)\s*{\s*return.+\s*\}/, "");
  26. }
  27. return contents;
  28. }
  29. _extendNavigationActivity(activityContent) {
  30. if (this._doesActivityExtendReactActivity(activityContent)) {
  31. debugn(" Extending NavigationActivity")
  32. return activityContent
  33. .replace(/extends\s+ReactActivity\s*/, "extends NavigationActivity ")
  34. .replace("import com.facebook.react.ReactActivity;", "import com.reactnativenavigation.NavigationActivity;")
  35. } else {
  36. warnn(" MainActivity already extends NavigationActivity")
  37. }
  38. return activityContent;
  39. }
  40. _doesActivityExtendReactActivity(activityContent) {
  41. return /public\s+class\s+MainActivity\s+extends\s+ReactActivity\s*/.test(activityContent);
  42. }
  43. }
  44. module.exports = ActivityLinker;