android.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CapacitorAndroidManifest = exports.ANDROID_MANIFEST_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.ANDROID_MANIFEST_FILE = 'AndroidManifest.xml';
  8. class CapacitorAndroidManifest {
  9. constructor(manifestPath) {
  10. this.manifestPath = manifestPath;
  11. this.saving = false;
  12. }
  13. get origManifestPath() {
  14. return `${this.manifestPath}.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(manifestPath) {
  23. if (!manifestPath) {
  24. throw new Error(`Must supply file path for ${exports.ANDROID_MANIFEST_FILE}.`);
  25. }
  26. const conf = new CapacitorAndroidManifest(manifestPath);
  27. await conf.reload();
  28. return conf;
  29. }
  30. async reload() {
  31. this.origManifestContent = await (0, utils_fs_1.readFile)(this.manifestPath, { encoding: 'utf8' });
  32. try {
  33. this._doc = et.parse(this.origManifestContent);
  34. }
  35. catch (e) {
  36. throw new Error(`Cannot parse ${exports.ANDROID_MANIFEST_FILE} file: ${e.stack ?? e}`);
  37. }
  38. }
  39. enableCleartextTraffic() {
  40. const node = this.getApplicationNode();
  41. node.set('android:usesCleartextTraffic', 'true');
  42. }
  43. async reset() {
  44. const origManifestContent = await (0, utils_fs_1.readFile)(this.origManifestPath, { encoding: 'utf8' });
  45. if (!this.saving) {
  46. this.saving = true;
  47. await (0, utils_fs_1.writeFile)(this.manifestPath, origManifestContent, { encoding: 'utf8' });
  48. await (0, utils_fs_1.unlink)(this.origManifestPath);
  49. this.saving = false;
  50. }
  51. }
  52. async save() {
  53. if (!this.saving) {
  54. this.saving = true;
  55. if (this.origManifestContent) {
  56. await (0, utils_fs_1.writeFile)(this.origManifestPath, this.origManifestContent, { encoding: 'utf8' });
  57. this.origManifestContent = undefined;
  58. }
  59. await (0, utils_fs_1.writeFile)(this.manifestPath, this.write(), { encoding: 'utf8' });
  60. this.saving = false;
  61. }
  62. }
  63. getApplicationNode() {
  64. const root = this.doc.getroot();
  65. const applicationNode = root.find('application');
  66. if (!applicationNode) {
  67. throw new Error(`No <application> node in ${exports.ANDROID_MANIFEST_FILE}.`);
  68. }
  69. return applicationNode;
  70. }
  71. write() {
  72. const contents = this.doc.write({ indent: 4 });
  73. return contents;
  74. }
  75. }
  76. exports.CapacitorAndroidManifest = CapacitorAndroidManifest;