fileUtil.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. /**
  3. * Copyright 2023 Google Inc. All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  18. if (k2 === undefined) k2 = k;
  19. var desc = Object.getOwnPropertyDescriptor(m, k);
  20. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  21. desc = { enumerable: true, get: function() { return m[k]; } };
  22. }
  23. Object.defineProperty(o, k2, desc);
  24. }) : (function(o, m, k, k2) {
  25. if (k2 === undefined) k2 = k;
  26. o[k2] = m[k];
  27. }));
  28. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  29. Object.defineProperty(o, "default", { enumerable: true, value: v });
  30. }) : function(o, v) {
  31. o["default"] = v;
  32. });
  33. var __importStar = (this && this.__importStar) || function (mod) {
  34. if (mod && mod.__esModule) return mod;
  35. var result = {};
  36. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  37. __setModuleDefault(result, mod);
  38. return result;
  39. };
  40. var __importDefault = (this && this.__importDefault) || function (mod) {
  41. return (mod && mod.__esModule) ? mod : { "default": mod };
  42. };
  43. Object.defineProperty(exports, "__esModule", { value: true });
  44. exports.unpackArchive = void 0;
  45. const child_process_1 = require("child_process");
  46. const fs_1 = require("fs");
  47. const promises_1 = require("fs/promises");
  48. const path = __importStar(require("path"));
  49. const util_1 = require("util");
  50. const extract_zip_1 = __importDefault(require("extract-zip"));
  51. const tar_fs_1 = __importDefault(require("tar-fs"));
  52. const unbzip2_stream_1 = __importDefault(require("unbzip2-stream"));
  53. const exec = (0, util_1.promisify)(child_process_1.exec);
  54. /**
  55. * @internal
  56. */
  57. async function unpackArchive(archivePath, folderPath) {
  58. if (archivePath.endsWith('.zip')) {
  59. await (0, extract_zip_1.default)(archivePath, { dir: folderPath });
  60. }
  61. else if (archivePath.endsWith('.tar.bz2')) {
  62. await extractTar(archivePath, folderPath);
  63. }
  64. else if (archivePath.endsWith('.dmg')) {
  65. await (0, promises_1.mkdir)(folderPath);
  66. await installDMG(archivePath, folderPath);
  67. }
  68. else {
  69. throw new Error(`Unsupported archive format: ${archivePath}`);
  70. }
  71. }
  72. exports.unpackArchive = unpackArchive;
  73. /**
  74. * @internal
  75. */
  76. function extractTar(tarPath, folderPath) {
  77. return new Promise((fulfill, reject) => {
  78. const tarStream = tar_fs_1.default.extract(folderPath);
  79. tarStream.on('error', reject);
  80. tarStream.on('finish', fulfill);
  81. const readStream = (0, fs_1.createReadStream)(tarPath);
  82. readStream.pipe((0, unbzip2_stream_1.default)()).pipe(tarStream);
  83. });
  84. }
  85. /**
  86. * @internal
  87. */
  88. async function installDMG(dmgPath, folderPath) {
  89. const { stdout } = await exec(`hdiutil attach -nobrowse -noautoopen "${dmgPath}"`);
  90. const volumes = stdout.match(/\/Volumes\/(.*)/m);
  91. if (!volumes) {
  92. throw new Error(`Could not find volume path in ${stdout}`);
  93. }
  94. const mountPath = volumes[0];
  95. try {
  96. const fileNames = await (0, promises_1.readdir)(mountPath);
  97. const appName = fileNames.find(item => {
  98. return typeof item === 'string' && item.endsWith('.app');
  99. });
  100. if (!appName) {
  101. throw new Error(`Cannot find app in ${mountPath}`);
  102. }
  103. const mountedPath = path.join(mountPath, appName);
  104. await exec(`cp -R "${mountedPath}" "${folderPath}"`);
  105. }
  106. finally {
  107. await exec(`hdiutil detach "${mountPath}" -quiet`);
  108. }
  109. }
  110. //# sourceMappingURL=fileUtil.js.map