Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / dist / lib / common / NsisVersionInfo.js
root on 7 May 2019 10 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 crypto_1 = require("crypto");
  40. var fs_extra_1 = require("fs-extra");
  41. var semver = require("semver");
  42. var NsisVersionInfo = /** @class */ (function () {
  43. function NsisVersionInfo(path) {
  44. this.path = path;
  45. this.outputDir = path_1.dirname(path);
  46. }
  47. NsisVersionInfo.prototype.addVersion = function (version, changelog, source) {
  48. return __awaiter(this, void 0, void 0, function () {
  49. var data;
  50. return __generator(this, function (_a) {
  51. switch (_a.label) {
  52. case 0: return [4 /*yield*/, this.getData()];
  53. case 1:
  54. data = _a.sent();
  55. if (!data.versions.find(function (item) { return item.version == version; })) {
  56. data.versions.push({
  57. version: version,
  58. changelog: changelog,
  59. source: path_1.basename(source),
  60. installers: [],
  61. updaters: [],
  62. });
  63. }
  64. this.updateLatestVersion();
  65. return [2 /*return*/];
  66. }
  67. });
  68. });
  69. };
  70. NsisVersionInfo.prototype.getVersions = function () {
  71. return __awaiter(this, void 0, void 0, function () {
  72. var data;
  73. return __generator(this, function (_a) {
  74. switch (_a.label) {
  75. case 0: return [4 /*yield*/, this.getData()];
  76. case 1:
  77. data = _a.sent();
  78. return [2 /*return*/, data.versions.map(function (item) { return item.version; })];
  79. }
  80. });
  81. });
  82. };
  83. NsisVersionInfo.prototype.getVersion = function (version) {
  84. return __awaiter(this, void 0, void 0, function () {
  85. var data, item;
  86. return __generator(this, function (_a) {
  87. switch (_a.label) {
  88. case 0: return [4 /*yield*/, this.getData()];
  89. case 1:
  90. data = _a.sent();
  91. item = data.versions.find(function (item) { return item.version == version; });
  92. if (!item) {
  93. throw new Error('ERROR_VERSION_NOT_FOUND');
  94. }
  95. return [2 /*return*/, item];
  96. }
  97. });
  98. });
  99. };
  100. NsisVersionInfo.prototype.addInstaller = function (version, arch, path) {
  101. return __awaiter(this, void 0, void 0, function () {
  102. var data, versionItem, _a, _b, _c;
  103. return __generator(this, function (_d) {
  104. switch (_d.label) {
  105. case 0: return [4 /*yield*/, this.getData()];
  106. case 1:
  107. data = _d.sent();
  108. versionItem = data.versions.find(function (item) { return item.version == version; });
  109. if (!versionItem) {
  110. throw new Error('ERROR_VERSION_NOT_FOUND');
  111. }
  112. if (!!versionItem.installers.find(function (item) { return item.arch == arch; })) return [3 /*break*/, 3];
  113. _b = (_a = versionItem.installers).push;
  114. _c = {
  115. arch: arch,
  116. path: path_1.relative(this.outputDir, path)
  117. };
  118. return [4 /*yield*/, this.hashFile('sha256', path)];
  119. case 2:
  120. _b.apply(_a, [(_c.hash = _d.sent(),
  121. _c.created = Date.now(),
  122. _c)]);
  123. _d.label = 3;
  124. case 3: return [2 /*return*/];
  125. }
  126. });
  127. });
  128. };
  129. NsisVersionInfo.prototype.addUpdater = function (version, fromVersion, arch, path) {
  130. return __awaiter(this, void 0, void 0, function () {
  131. var data, versionItem, _a, _b, _c;
  132. return __generator(this, function (_d) {
  133. switch (_d.label) {
  134. case 0: return [4 /*yield*/, this.getData()];
  135. case 1:
  136. data = _d.sent();
  137. versionItem = data.versions.find(function (item) { return item.version == version; });
  138. if (!versionItem) {
  139. throw new Error('ERROR_VERSION_NOT_FOUND');
  140. }
  141. if (!!versionItem.updaters.find(function (item) { return item.fromVersion == fromVersion && item.arch == arch; })) return [3 /*break*/, 3];
  142. _b = (_a = versionItem.updaters).push;
  143. _c = {
  144. fromVersion: fromVersion,
  145. arch: arch,
  146. path: path_1.relative(this.outputDir, path)
  147. };
  148. return [4 /*yield*/, this.hashFile('sha256', path)];
  149. case 2:
  150. _b.apply(_a, [(_c.hash = _d.sent(),
  151. _c.created = Date.now(),
  152. _c)]);
  153. _d.label = 3;
  154. case 3: return [2 /*return*/];
  155. }
  156. });
  157. });
  158. };
  159. NsisVersionInfo.prototype.save = function () {
  160. return __awaiter(this, void 0, void 0, function () {
  161. return __generator(this, function (_a) {
  162. switch (_a.label) {
  163. case 0: return [4 /*yield*/, fs_extra_1.writeJson(this.path, this.data)];
  164. case 1:
  165. _a.sent();
  166. return [2 /*return*/];
  167. }
  168. });
  169. });
  170. };
  171. NsisVersionInfo.prototype.getData = function () {
  172. return __awaiter(this, void 0, void 0, function () {
  173. var _this = this;
  174. var _a, _b;
  175. return __generator(this, function (_c) {
  176. switch (_c.label) {
  177. case 0:
  178. if (!!this.data) return [3 /*break*/, 5];
  179. _a = this;
  180. return [4 /*yield*/, new Promise(function (resolve, reject) { return fs_extra_1.exists(_this.path, resolve); })];
  181. case 1:
  182. if (!(_c.sent())) return [3 /*break*/, 3];
  183. return [4 /*yield*/, fs_extra_1.readJson(this.path)];
  184. case 2:
  185. _b = _c.sent();
  186. return [3 /*break*/, 4];
  187. case 3:
  188. _b = {
  189. latest: undefined,
  190. versions: [],
  191. };
  192. _c.label = 4;
  193. case 4:
  194. _a.data = _b;
  195. _c.label = 5;
  196. case 5: return [2 /*return*/, this.data];
  197. }
  198. });
  199. });
  200. };
  201. NsisVersionInfo.prototype.updateLatestVersion = function () {
  202. if (this.data.versions.length == 0) {
  203. return;
  204. }
  205. var versions = this.data.versions.slice();
  206. versions.sort(function (a, b) { return semver.gt(a.version, b.version) ? -1 : 1; });
  207. this.data.latest = versions[0].version;
  208. };
  209. NsisVersionInfo.prototype.hashFile = function (type, path) {
  210. return new Promise(function (resolve, reject) {
  211. var hasher = crypto_1.createHash(type);
  212. hasher.on('error', reject);
  213. hasher.on('readable', function () {
  214. var data = hasher.read();
  215. if (data) {
  216. hasher.end();
  217. resolve(data.toString('hex'));
  218. }
  219. });
  220. fs_extra_1.createReadStream(path).pipe(hasher);
  221. });
  222. };
  223. return NsisVersionInfo;
  224. }());
  225. exports.NsisVersionInfo = NsisVersionInfo;
  226. //# sourceMappingURL=NsisVersionInfo.js.map
Buy Me A Coffee