ios.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CapacitorIosInfo = exports.IOS_INFO_FILE = void 0;
  4. const tslib_1 = require("tslib");
  5. const utils_fs_1 = require("@ionic/utils-fs");
  6. const et = tslib_1.__importStar(require("elementtree"));
  7. exports.IOS_INFO_FILE = "Info.plist";
  8. class CapacitorIosInfo {
  9. constructor(plistPath) {
  10. this.plistPath = plistPath;
  11. this.saving = false;
  12. }
  13. get origPlistPath() {
  14. return `${this.plistPath}.orig`;
  15. }
  16. get doc() {
  17. if (!this._doc) {
  18. throw new Error("No doc loaded.");
  19. }
  20. return this._doc;
  21. }
  22. static async load(plistPath) {
  23. if (!plistPath) {
  24. throw new Error(`Must supply file path for ${exports.IOS_INFO_FILE}.`);
  25. }
  26. const conf = new CapacitorIosInfo(plistPath);
  27. await conf.reload();
  28. return conf;
  29. }
  30. disableAppTransportSecurity() {
  31. const rootDict = this.getDictRoot();
  32. let valueDict = this.getValueForKey(rootDict, "NSAppTransportSecurity");
  33. if (valueDict) {
  34. const value = this.getValueForKey(valueDict, "NSAllowsArbitraryLoads");
  35. if (value) {
  36. value.tag = "true";
  37. }
  38. else {
  39. et.SubElement(valueDict, "true");
  40. }
  41. }
  42. else {
  43. const newKey = et.SubElement(rootDict, "key");
  44. newKey.text = "NSAppTransportSecurity";
  45. const newDict = et.SubElement(rootDict, "dict");
  46. const newDictKey = et.SubElement(newDict, "key");
  47. newDictKey.text = "NSAllowsArbitraryLoads";
  48. et.SubElement(newDict, "true");
  49. }
  50. }
  51. getValueForKey(root, key) {
  52. const children = root.getchildren();
  53. let keyFound = false;
  54. for (const element of children) {
  55. if (keyFound) {
  56. keyFound = false;
  57. return element;
  58. }
  59. if ((element.tag === 'key') && element.text === key) {
  60. keyFound = true;
  61. }
  62. }
  63. return null;
  64. }
  65. getDictRoot() {
  66. const root = this.doc.getroot();
  67. if (root.tag !== "plist") {
  68. throw new Error(`Info.plist is not a valid plist file because the root is not a <plist> tag`);
  69. }
  70. const rootDict = root.find('./dict');
  71. if (!rootDict) {
  72. throw new Error(`Info.plist is not a valid plist file because the first child is not a <dict> tag`);
  73. }
  74. return rootDict;
  75. }
  76. async reset() {
  77. const origInfoPlistContent = await (0, utils_fs_1.readFile)(this.origPlistPath, {
  78. encoding: "utf8",
  79. });
  80. if (!this.saving) {
  81. this.saving = true;
  82. await (0, utils_fs_1.writeFile)(this.plistPath, origInfoPlistContent, {
  83. encoding: "utf8",
  84. });
  85. await (0, utils_fs_1.unlink)(this.origPlistPath);
  86. this.saving = false;
  87. }
  88. }
  89. async save() {
  90. if (!this.saving) {
  91. this.saving = true;
  92. if (this.origInfoPlistContent) {
  93. await (0, utils_fs_1.writeFile)(this.origPlistPath, this.origInfoPlistContent, {
  94. encoding: "utf8",
  95. });
  96. this.origInfoPlistContent = undefined;
  97. }
  98. await (0, utils_fs_1.writeFile)(this.plistPath, this.write(), { encoding: "utf8" });
  99. this.saving = false;
  100. }
  101. }
  102. async reload() {
  103. this.origInfoPlistContent = await (0, utils_fs_1.readFile)(this.plistPath, {
  104. encoding: "utf8",
  105. });
  106. try {
  107. this._doc = et.parse(this.origInfoPlistContent);
  108. }
  109. catch (e) {
  110. throw new Error(`Cannot parse ${exports.IOS_INFO_FILE} file: ${e.stack ?? e}`);
  111. }
  112. }
  113. write() {
  114. const contents = this.doc.write({ indent: 4 });
  115. return contents;
  116. }
  117. }
  118. exports.CapacitorIosInfo = CapacitorIosInfo;