fileUtil.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2023 Google Inc.
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  8. if (k2 === undefined) k2 = k;
  9. var desc = Object.getOwnPropertyDescriptor(m, k);
  10. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  11. desc = { enumerable: true, get: function() { return m[k]; } };
  12. }
  13. Object.defineProperty(o, k2, desc);
  14. }) : (function(o, m, k, k2) {
  15. if (k2 === undefined) k2 = k;
  16. o[k2] = m[k];
  17. }));
  18. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  19. Object.defineProperty(o, "default", { enumerable: true, value: v });
  20. }) : function(o, v) {
  21. o["default"] = v;
  22. });
  23. var __importStar = (this && this.__importStar) || function (mod) {
  24. if (mod && mod.__esModule) return mod;
  25. var result = {};
  26. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  27. __setModuleDefault(result, mod);
  28. return result;
  29. };
  30. var __importDefault = (this && this.__importDefault) || function (mod) {
  31. return (mod && mod.__esModule) ? mod : { "default": mod };
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. exports.unpackArchive = void 0;
  35. const child_process_1 = require("child_process");
  36. const fs_1 = require("fs");
  37. const promises_1 = require("fs/promises");
  38. const path = __importStar(require("path"));
  39. const util_1 = require("util");
  40. const extract_zip_1 = __importDefault(require("extract-zip"));
  41. const tar_fs_1 = __importDefault(require("tar-fs"));
  42. const unbzip2_stream_1 = __importDefault(require("unbzip2-stream"));
  43. const exec = (0, util_1.promisify)(child_process_1.exec);
  44. /**
  45. * @internal
  46. */
  47. async function unpackArchive(archivePath, folderPath) {
  48. if (archivePath.endsWith('.zip')) {
  49. await (0, extract_zip_1.default)(archivePath, { dir: folderPath });
  50. }
  51. else if (archivePath.endsWith('.tar.bz2')) {
  52. await extractTar(archivePath, folderPath);
  53. }
  54. else if (archivePath.endsWith('.dmg')) {
  55. await (0, promises_1.mkdir)(folderPath);
  56. await installDMG(archivePath, folderPath);
  57. }
  58. else if (archivePath.endsWith('.exe')) {
  59. // Firefox on Windows.
  60. const result = (0, child_process_1.spawnSync)(archivePath, [`/ExtractDir=${folderPath}`], {
  61. env: {
  62. __compat_layer: 'RunAsInvoker',
  63. },
  64. });
  65. if (result.status !== 0) {
  66. throw new Error(`Failed to extract ${archivePath} to ${folderPath}: ${result.output}`);
  67. }
  68. }
  69. else {
  70. throw new Error(`Unsupported archive format: ${archivePath}`);
  71. }
  72. }
  73. exports.unpackArchive = unpackArchive;
  74. /**
  75. * @internal
  76. */
  77. function extractTar(tarPath, folderPath) {
  78. return new Promise((fulfill, reject) => {
  79. const tarStream = tar_fs_1.default.extract(folderPath);
  80. tarStream.on('error', reject);
  81. tarStream.on('finish', fulfill);
  82. const readStream = (0, fs_1.createReadStream)(tarPath);
  83. readStream.pipe((0, unbzip2_stream_1.default)()).pipe(tarStream);
  84. });
  85. }
  86. /**
  87. * @internal
  88. */
  89. async function installDMG(dmgPath, folderPath) {
  90. const { stdout } = await exec(`hdiutil attach -nobrowse -noautoopen "${dmgPath}"`);
  91. const volumes = stdout.match(/\/Volumes\/(.*)/m);
  92. if (!volumes) {
  93. throw new Error(`Could not find volume path in ${stdout}`);
  94. }
  95. const mountPath = volumes[0];
  96. try {
  97. const fileNames = await (0, promises_1.readdir)(mountPath);
  98. const appName = fileNames.find(item => {
  99. return typeof item === 'string' && item.endsWith('.app');
  100. });
  101. if (!appName) {
  102. throw new Error(`Cannot find app in ${mountPath}`);
  103. }
  104. const mountedPath = path.join(mountPath, appName);
  105. await exec(`cp -R "${mountedPath}" "${folderPath}"`);
  106. }
  107. finally {
  108. await exec(`hdiutil detach "${mountPath}" -quiet`);
  109. }
  110. }
  111. //# sourceMappingURL=fileUtil.js.map