DownloaderBase.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [op[0] & 2, 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 request = require("request");
  40. var ProgressBar = require("progress");
  41. var fs_extra_1 = require("fs-extra");
  42. var debug = require('debug')('build:downloader');
  43. var progress = require('request-progress');
  44. var Event_1 = require("./Event");
  45. var util_1 = require("../util");
  46. var DIR_CACHES = path_1.resolve(path_1.dirname(module.filename), '..', '..', '..', 'caches');
  47. fs_extra_1.ensureDirSync(DIR_CACHES);
  48. var DownloaderBase = /** @class */ (function () {
  49. function DownloaderBase() {
  50. this.onProgress = new Event_1.Event('progress');
  51. this.destination = DownloaderBase.DEFAULT_DESTINATION;
  52. }
  53. DownloaderBase.prototype.fetchAndExtract = function () {
  54. return __awaiter(this, void 0, void 0, function () {
  55. var archive, dest;
  56. return __generator(this, function (_a) {
  57. switch (_a.label) {
  58. case 0: return [4 /*yield*/, this.fetch()];
  59. case 1:
  60. archive = _a.sent();
  61. dest = archive + "-extracted";
  62. return [4 /*yield*/, util_1.extractGeneric(archive, dest)];
  63. case 2:
  64. _a.sent();
  65. return [2 /*return*/, dest];
  66. }
  67. });
  68. });
  69. };
  70. DownloaderBase.prototype.getVersions = function () {
  71. return new Promise(function (resolve, reject) {
  72. request('https://nwjs.io/versions.json', function (err, res, body) {
  73. if (err) {
  74. return reject(err);
  75. }
  76. var json = JSON.parse(body);
  77. resolve(json);
  78. });
  79. });
  80. };
  81. DownloaderBase.prototype.setDestination = function (destination) {
  82. this.destination = destination;
  83. };
  84. DownloaderBase.prototype.handlePlatform = function (platform) {
  85. switch (platform) {
  86. case 'win32':
  87. case 'win':
  88. return 'win';
  89. case 'darwin':
  90. case 'osx':
  91. case 'mac':
  92. return 'osx';
  93. case 'linux':
  94. return 'linux';
  95. default:
  96. throw new Error('ERROR_UNKNOWN_PLATFORM');
  97. }
  98. };
  99. DownloaderBase.prototype.handleArch = function (arch) {
  100. switch (arch) {
  101. case 'x86':
  102. case 'ia32':
  103. return 'ia32';
  104. case 'x64':
  105. return 'x64';
  106. default:
  107. throw new Error('ERROR_UNKNOWN_PLATFORM');
  108. }
  109. };
  110. DownloaderBase.prototype.getLocalSize = function (path) {
  111. return fs_extra_1.lstat(path)
  112. .then(function (stat) { return stat.size; });
  113. };
  114. DownloaderBase.prototype.getRemoteSize = function (url) {
  115. return new Promise(function (resolve, reject) {
  116. request.head(url, {
  117. followAllRedirects: true,
  118. })
  119. .on('error', reject)
  120. .on('response', function (res) { return resolve(parseInt((res.headers['content-length']), 10)); });
  121. });
  122. };
  123. DownloaderBase.prototype.isFileExists = function (path) {
  124. return new Promise(function (resolve, reject) {
  125. fs_extra_1.exists(path, resolve);
  126. });
  127. };
  128. DownloaderBase.prototype.isFileSynced = function (url, path) {
  129. return __awaiter(this, void 0, void 0, function () {
  130. var localSize, remoteSize;
  131. return __generator(this, function (_a) {
  132. switch (_a.label) {
  133. case 0: return [4 /*yield*/, this.getLocalSize(path)];
  134. case 1:
  135. localSize = _a.sent();
  136. return [4 /*yield*/, this.getRemoteSize(url)];
  137. case 2:
  138. remoteSize = _a.sent();
  139. debug('in isFileSynced', 'localSize', localSize);
  140. debug('in isFileSynced', 'remoteSize', remoteSize);
  141. return [2 /*return*/, localSize == remoteSize];
  142. }
  143. });
  144. });
  145. };
  146. DownloaderBase.prototype.download = function (url, filename, path, showProgress) {
  147. return __awaiter(this, void 0, void 0, function () {
  148. var bar, onProgress;
  149. var _this = this;
  150. return __generator(this, function (_a) {
  151. switch (_a.label) {
  152. case 0:
  153. bar = null;
  154. onProgress = function (state) {
  155. if (!state.size.total) {
  156. return;
  157. }
  158. if (!bar) {
  159. bar = new ProgressBar('[:bar] :speedKB/s :etas', {
  160. width: 50,
  161. total: state.size.total,
  162. });
  163. console.info('');
  164. }
  165. bar.update(state.size.transferred / state.size.total, {
  166. speed: (state.speed / 1000).toFixed(2),
  167. });
  168. };
  169. if (showProgress) {
  170. this.onProgress.subscribe(onProgress);
  171. }
  172. debug('in download', 'start downloading', filename);
  173. return [4 /*yield*/, new Promise(function (resolve, reject) {
  174. progress(request(url, {
  175. encoding: null,
  176. }, function (err, res, data) {
  177. if (err) {
  178. return reject(err);
  179. }
  180. if (res.statusCode != 200) {
  181. var e = new Error("ERROR_STATUS_CODE statusCode = " + res.statusCode);
  182. return reject(e);
  183. }
  184. fs_extra_1.writeFile(path, data, function (err) { return err ? reject(err) : resolve(); });
  185. }))
  186. .on('progress', function (state) {
  187. _this.onProgress.trigger(state);
  188. });
  189. })];
  190. case 1:
  191. _a.sent();
  192. debug('in fetch', 'end downloading', filename);
  193. if (showProgress) {
  194. this.onProgress.unsubscribe(onProgress);
  195. if (bar) {
  196. console.info('');
  197. bar.terminate();
  198. }
  199. }
  200. return [2 /*return*/, path];
  201. }
  202. });
  203. });
  204. };
  205. DownloaderBase.DEFAULT_DESTINATION = DIR_CACHES;
  206. return DownloaderBase;
  207. }());
  208. exports.DownloaderBase = DownloaderBase;
  209. //# sourceMappingURL=DownloaderBase.js.map