Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / dist / lib / util / archive.js
root on 7 May 2019 6 KB Initial commit
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [0, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. var path_1 = require("path");
  39. var fs_extra_1 = require("fs-extra");
  40. var _7zip_bin_1 = require("7zip-bin");
  41. var debug = require('debug')('build:archive');
  42. var _1 = require("./");
  43. function extract(archive, dest, options) {
  44. if (dest === void 0) { dest = path_1.dirname(archive); }
  45. if (options === void 0) { options = {
  46. overwrite: false,
  47. }; }
  48. return __awaiter(this, void 0, void 0, function () {
  49. var _a, code, signal;
  50. return __generator(this, function (_b) {
  51. switch (_b.label) {
  52. case 0:
  53. debug('in extract', 'archive', archive);
  54. debug('in extract', 'dest', dest);
  55. return [4 /*yield*/, _1.spawnAsync(_7zip_bin_1.path7za, ['x', '-y', "-ao" + (options.overwrite ? 'a' : 's'), "-o" + path_1.resolve(dest), path_1.resolve(archive)])];
  56. case 1:
  57. _a = _b.sent(), code = _a.code, signal = _a.signal;
  58. if (code != 0) {
  59. throw new Error("ERROR_EXTRACTING path = " + archive);
  60. }
  61. return [2 /*return*/, dest];
  62. }
  63. });
  64. });
  65. }
  66. function extractTarGz(archive, dest, options) {
  67. if (dest === void 0) { dest = path_1.dirname(archive); }
  68. if (options === void 0) { options = {
  69. overwrite: false,
  70. }; }
  71. return __awaiter(this, void 0, void 0, function () {
  72. var tar;
  73. return __generator(this, function (_a) {
  74. switch (_a.label) {
  75. case 0:
  76. tar = path_1.join(dest, path_1.basename(archive.slice(0, -3)));
  77. return [4 /*yield*/, extract(archive, dest, {
  78. overwrite: true,
  79. })];
  80. case 1:
  81. _a.sent();
  82. return [4 /*yield*/, extract(tar, dest)];
  83. case 2:
  84. _a.sent();
  85. return [4 /*yield*/, fs_extra_1.remove(tar)];
  86. case 3:
  87. _a.sent();
  88. return [2 /*return*/, dest];
  89. }
  90. });
  91. });
  92. }
  93. function extractGeneric(archive, dest, options) {
  94. if (dest === void 0) { dest = path_1.dirname(archive); }
  95. if (options === void 0) { options = {
  96. overwrite: false,
  97. }; }
  98. return __awaiter(this, void 0, void 0, function () {
  99. return __generator(this, function (_a) {
  100. switch (_a.label) {
  101. case 0:
  102. if (!archive.endsWith('.zip')) return [3 /*break*/, 2];
  103. return [4 /*yield*/, extract(archive, dest, options)];
  104. case 1:
  105. _a.sent();
  106. return [3 /*break*/, 5];
  107. case 2:
  108. if (!archive.endsWith('tar.gz')) return [3 /*break*/, 4];
  109. return [4 /*yield*/, extractTarGz(archive, dest, options)];
  110. case 3:
  111. _a.sent();
  112. return [3 /*break*/, 5];
  113. case 4: throw new Error('ERROR_UNKNOWN_EXTENSION');
  114. case 5: return [2 /*return*/, dest];
  115. }
  116. });
  117. });
  118. }
  119. exports.extractGeneric = extractGeneric;
  120. function compress(dir, files, type, archive) {
  121. return __awaiter(this, void 0, void 0, function () {
  122. var listfiles, _a, code, signal;
  123. return __generator(this, function (_b) {
  124. switch (_b.label) {
  125. case 0:
  126. debug('in compress', 'dir', dir);
  127. debug('in compress', 'files', files);
  128. debug('in compress', 'type', type);
  129. debug('in compress', 'archive', archive);
  130. return [4 /*yield*/, _1.tmpFile({
  131. discardDescriptor: true,
  132. })];
  133. case 1:
  134. listfiles = (_b.sent()).path;
  135. debug('in compress', 'listfiles', listfiles);
  136. return [4 /*yield*/, fs_extra_1.writeFile(listfiles, files.map(function (file) { return path_1.normalize(file); }).join('\r\n'))];
  137. case 2:
  138. _b.sent();
  139. return [4 /*yield*/, _1.spawnAsync(_7zip_bin_1.path7za, ['a', "-t" + type, path_1.resolve(archive), "@" + path_1.resolve(listfiles)], {
  140. cwd: dir,
  141. })];
  142. case 3:
  143. _a = _b.sent(), code = _a.code, signal = _a.signal;
  144. return [2 /*return*/, code];
  145. }
  146. });
  147. });
  148. }
  149. exports.compress = compress;
  150. //# sourceMappingURL=archive.js.map
Buy Me A Coffee