Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / dist / lib / Builder.js
root on 7 May 2019 50 KB Initial commit
  1. "use strict";
  2. var __assign = (this && this.__assign) || Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  11. return new (P || (P = Promise))(function (resolve, reject) {
  12. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  13. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  14. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  15. step((generator = generator.apply(thisArg, _arguments || [])).next());
  16. });
  17. };
  18. var __generator = (this && this.__generator) || function (thisArg, body) {
  19. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  20. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  21. function verb(n) { return function (v) { return step([n, v]); }; }
  22. function step(op) {
  23. if (f) throw new TypeError("Generator is already executing.");
  24. while (_) try {
  25. if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
  26. if (y = 0, t) op = [0, t.value];
  27. switch (op[0]) {
  28. case 0: case 1: t = op; break;
  29. case 4: _.label++; return { value: op[1], done: false };
  30. case 5: _.label++; y = op[1]; op = [0]; continue;
  31. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  32. default:
  33. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  34. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  35. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  36. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  37. if (t[2]) _.ops.pop();
  38. _.trys.pop(); continue;
  39. }
  40. op = body.call(thisArg, _);
  41. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  42. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  43. }
  44. };
  45. Object.defineProperty(exports, "__esModule", { value: true });
  46. var path_1 = require("path");
  47. var semver = require("semver");
  48. var fs_extra_1 = require("fs-extra");
  49. var Bluebird = require("bluebird");
  50. var debug = require('debug')('build:builder');
  51. var globby = require('globby');
  52. var rcedit = require('rcedit');
  53. var plist = require('plist');
  54. var Downloader_1 = require("./Downloader");
  55. var FFmpegDownloader_1 = require("./FFmpegDownloader");
  56. var config_1 = require("./config");
  57. var common_1 = require("./common");
  58. var nsis_gen_1 = require("./nsis-gen");
  59. var util_1 = require("./util");
  60. var Builder = /** @class */ (function () {
  61. function Builder(options, dir) {
  62. if (options === void 0) { options = {}; }
  63. this.dir = dir;
  64. this.options = util_1.mergeOptions(Builder.DEFAULT_OPTIONS, options);
  65. debug('in constructor', 'dir', dir);
  66. debug('in constructor', 'options', this.options);
  67. }
  68. Builder.prototype.build = function () {
  69. return __awaiter(this, void 0, void 0, function () {
  70. var _this = this;
  71. var tasks, _i, _a, task, _b, platform, arch, pkg, config, _c, tasks_1, _d, platform, arch, started, err_1;
  72. return __generator(this, function (_e) {
  73. switch (_e.label) {
  74. case 0:
  75. tasks = [];
  76. ['win', 'mac', 'linux'].map(function (platform) {
  77. ['x86', 'x64'].map(function (arch) {
  78. if (_this.options[platform] && _this.options[arch]) {
  79. tasks.push([platform, arch]);
  80. }
  81. });
  82. });
  83. for (_i = 0, _a = this.options.tasks; _i < _a.length; _i++) {
  84. task = _a[_i];
  85. _b = task.split('-'), platform = _b[0], arch = _b[1];
  86. if (['win', 'mac', 'linux'].indexOf(platform) >= 0) {
  87. if (['x86', 'x64'].indexOf(arch) >= 0) {
  88. tasks.push([platform, arch]);
  89. }
  90. }
  91. }
  92. if (!this.options.mute) {
  93. console.info('Starting building tasks...', {
  94. tasks: tasks,
  95. concurrent: this.options.concurrent,
  96. });
  97. }
  98. if (tasks.length == 0) {
  99. throw new Error('ERROR_NO_TASK');
  100. }
  101. if (!this.options.concurrent) return [3 /*break*/, 2];
  102. return [4 /*yield*/, Bluebird.map(tasks, function (_a) {
  103. var platform = _a[0], arch = _a[1];
  104. return __awaiter(_this, void 0, void 0, function () {
  105. var options, builder, started;
  106. return __generator(this, function (_a) {
  107. switch (_a.label) {
  108. case 0:
  109. options = {};
  110. options[platform] = true;
  111. options[arch] = true;
  112. options.mirror = this.options.mirror;
  113. options.concurrent = false;
  114. options.mute = true;
  115. builder = new Builder(options, this.dir);
  116. started = Date.now();
  117. if (!this.options.mute) {
  118. console.info("Building for " + platform + ", " + arch + " starts...");
  119. }
  120. return [4 /*yield*/, builder.build()];
  121. case 1:
  122. _a.sent();
  123. if (!this.options.mute) {
  124. console.info("Building for " + platform + ", " + arch + " ends within " + this.getTimeDiff(started) + "s.");
  125. }
  126. return [2 /*return*/];
  127. }
  128. });
  129. });
  130. })];
  131. case 1:
  132. _e.sent();
  133. return [3 /*break*/, 10];
  134. case 2: return [4 /*yield*/, fs_extra_1.readJson(path_1.resolve(this.dir, this.options.chromeApp ? 'manifest.json' : 'package.json'))];
  135. case 3:
  136. pkg = _e.sent();
  137. config = new config_1.BuildConfig(pkg);
  138. debug('in build', 'config', config);
  139. _c = 0, tasks_1 = tasks;
  140. _e.label = 4;
  141. case 4:
  142. if (!(_c < tasks_1.length)) return [3 /*break*/, 10];
  143. _d = tasks_1[_c], platform = _d[0], arch = _d[1];
  144. started = Date.now();
  145. if (!this.options.mute) {
  146. console.info("Building for " + platform + ", " + arch + " starts...");
  147. }
  148. _e.label = 5;
  149. case 5:
  150. _e.trys.push([5, 7, , 8]);
  151. return [4 /*yield*/, this.buildTask(platform, arch, pkg, config)];
  152. case 6:
  153. _e.sent();
  154. return [3 /*break*/, 8];
  155. case 7:
  156. err_1 = _e.sent();
  157. console.warn(err_1);
  158. return [3 /*break*/, 8];
  159. case 8:
  160. if (!this.options.mute) {
  161. console.info("Building for " + platform + ", " + arch + " ends within " + this.getTimeDiff(started) + "s.");
  162. }
  163. _e.label = 9;
  164. case 9:
  165. _c++;
  166. return [3 /*break*/, 4];
  167. case 10: return [2 /*return*/];
  168. }
  169. });
  170. });
  171. };
  172. Builder.prototype.getTimeDiff = function (started) {
  173. return ((Date.now() - started) / 1000).toFixed(2);
  174. };
  175. Builder.prototype.writeStrippedManifest = function (path, pkg, config) {
  176. return __awaiter(this, void 0, void 0, function () {
  177. var json, key;
  178. return __generator(this, function (_a) {
  179. switch (_a.label) {
  180. case 0:
  181. json = {};
  182. for (key in pkg) {
  183. if (pkg.hasOwnProperty(key) && config.strippedProperties.indexOf(key) === -1) {
  184. json[key] = pkg[key];
  185. }
  186. }
  187. return [4 /*yield*/, fs_extra_1.writeFile(path, JSON.stringify(json))];
  188. case 1:
  189. _a.sent();
  190. return [2 /*return*/];
  191. }
  192. });
  193. });
  194. };
  195. Builder.prototype.parseOutputPattern = function (pattern, options, pkg, config) {
  196. return pattern.replace(/\$\{\s*(\w+)\s*\}/g, function (match, key) {
  197. switch (key.toLowerCase()) {
  198. case 'name':
  199. return options.name;
  200. case 'version':
  201. return options.version;
  202. case 'platform':
  203. return options.platform;
  204. case 'arch':
  205. return options.arch;
  206. default:
  207. throw new Error('ERROR_KEY_UNKNOWN');
  208. }
  209. });
  210. };
  211. Builder.prototype.combineExecutable = function (executable, nwFile) {
  212. return new Promise(function (resolve, reject) {
  213. var nwStream = fs_extra_1.createReadStream(nwFile);
  214. var stream = fs_extra_1.createWriteStream(executable, {
  215. flags: 'a',
  216. });
  217. nwStream.on('error', reject);
  218. stream.on('error', reject);
  219. stream.on('finish', resolve);
  220. nwStream.pipe(stream);
  221. });
  222. };
  223. Builder.prototype.readPlist = function (path) {
  224. return fs_extra_1.readFile(path, {
  225. encoding: 'utf-8',
  226. })
  227. .then(function (data) { return plist.parse(data); });
  228. };
  229. Builder.prototype.writePlist = function (path, p) {
  230. return fs_extra_1.writeFile(path, plist.build(p));
  231. };
  232. Builder.prototype.updateWinResources = function (targetDir, appRoot, pkg, config) {
  233. var _this = this;
  234. var pathResolve = path_1.resolve;
  235. return new Promise(function (resolve, reject) {
  236. var path = pathResolve(targetDir, 'nw.exe');
  237. var rc = {
  238. 'product-version': util_1.fixWindowsVersion(config.win.productVersion),
  239. 'file-version': util_1.fixWindowsVersion(config.win.fileVersion),
  240. 'version-string': __assign({ ProductName: config.win.productName, CompanyName: config.win.companyName, FileDescription: config.win.fileDescription, LegalCopyright: config.win.copyright }, config.win.versionStrings),
  241. 'icon': config.win.icon ? pathResolve(_this.dir, config.win.icon) : undefined,
  242. };
  243. rcedit(path, rc, function (err) { return err ? reject(err) : resolve(); });
  244. });
  245. };
  246. Builder.prototype.renameWinApp = function (targetDir, appRoot, pkg, config) {
  247. var src = path_1.resolve(targetDir, 'nw.exe');
  248. var dest = path_1.resolve(targetDir, config.win.productName + ".exe");
  249. return fs_extra_1.rename(src, dest);
  250. };
  251. Builder.prototype.updatePlist = function (targetDir, appRoot, pkg, config) {
  252. return __awaiter(this, void 0, void 0, function () {
  253. var path, plist, key;
  254. return __generator(this, function (_a) {
  255. switch (_a.label) {
  256. case 0:
  257. path = path_1.resolve(targetDir, './nwjs.app/Contents/Info.plist');
  258. return [4 /*yield*/, this.readPlist(path)];
  259. case 1:
  260. plist = _a.sent();
  261. plist.CFBundleIdentifier = config.appId;
  262. plist.CFBundleName = config.mac.name;
  263. plist.CFBundleDisplayName = config.mac.displayName;
  264. plist.CFBundleVersion = config.mac.version;
  265. plist.CFBundleShortVersionString = config.mac.version;
  266. for (key in config.mac.plistStrings) {
  267. if (config.mac.plistStrings.hasOwnProperty(key)) {
  268. plist[key] = config.mac.plistStrings[key];
  269. }
  270. }
  271. return [4 /*yield*/, this.writePlist(path, plist)];
  272. case 2:
  273. _a.sent();
  274. return [2 /*return*/];
  275. }
  276. });
  277. });
  278. };
  279. Builder.prototype.updateMacIcon = function (targetDir, appRoot, pkg, config) {
  280. return __awaiter(this, void 0, void 0, function () {
  281. var path;
  282. return __generator(this, function (_a) {
  283. switch (_a.label) {
  284. case 0:
  285. path = path_1.resolve(targetDir, './nwjs.app/Contents/Resources/app.icns');
  286. if (!config.mac.icon) {
  287. return [2 /*return*/];
  288. }
  289. return [4 /*yield*/, fs_extra_1.copy(path_1.resolve(this.dir, config.mac.icon), path)];
  290. case 1:
  291. _a.sent();
  292. return [2 /*return*/];
  293. }
  294. });
  295. });
  296. };
  297. Builder.prototype.fixMacMeta = function (targetDir, appRoot, pkg, config) {
  298. return __awaiter(this, void 0, void 0, function () {
  299. var files, _i, files_1, file, path, data, encoding, strings, newStrings;
  300. return __generator(this, function (_a) {
  301. switch (_a.label) {
  302. case 0: return [4 /*yield*/, globby(['**/InfoPlist.strings'], {
  303. cwd: targetDir,
  304. })];
  305. case 1:
  306. files = _a.sent();
  307. _i = 0, files_1 = files;
  308. _a.label = 2;
  309. case 2:
  310. if (!(_i < files_1.length)) return [3 /*break*/, 6];
  311. file = files_1[_i];
  312. path = path_1.resolve(targetDir, file);
  313. return [4 /*yield*/, fs_extra_1.readFile(path)];
  314. case 3:
  315. data = _a.sent();
  316. encoding = data.indexOf(Buffer.from('43004600', 'hex')) >= 0
  317. ? 'ucs2' : 'utf-8';
  318. strings = data.toString(encoding);
  319. newStrings = strings.replace(/([A-Za-z]+)\s+=\s+"(.+?)";/g, function (match, key, value) {
  320. switch (key) {
  321. case 'CFBundleName':
  322. return key + " = \"" + config.mac.name + "\";";
  323. case 'CFBundleDisplayName':
  324. return key + " = \"" + config.mac.displayName + "\";";
  325. case 'CFBundleGetInfoString':
  326. return key + " = \"" + config.mac.version + "\";";
  327. case 'NSContactsUsageDescription':
  328. return key + " = \"" + config.mac.description + "\";";
  329. case 'NSHumanReadableCopyright':
  330. return key + " = \"" + config.mac.copyright + "\";";
  331. default:
  332. return key + " = \"" + value + "\";";
  333. }
  334. });
  335. return [4 /*yield*/, fs_extra_1.writeFile(path, Buffer.from(newStrings, encoding))];
  336. case 4:
  337. _a.sent();
  338. _a.label = 5;
  339. case 5:
  340. _i++;
  341. return [3 /*break*/, 2];
  342. case 6: return [2 /*return*/];
  343. }
  344. });
  345. });
  346. };
  347. Builder.prototype.renameMacApp = function (targetDir, appRoot, pkg, config) {
  348. var src = path_1.resolve(targetDir, 'nwjs.app');
  349. var dest = path_1.resolve(targetDir, config.mac.displayName + ".app");
  350. return fs_extra_1.rename(src, dest);
  351. };
  352. Builder.prototype.fixLinuxMode = function (targetDir, appRoot, pkg, config) {
  353. return __awaiter(this, void 0, void 0, function () {
  354. var path;
  355. return __generator(this, function (_a) {
  356. switch (_a.label) {
  357. case 0:
  358. path = path_1.resolve(targetDir, 'nw');
  359. return [4 /*yield*/, fs_extra_1.chmod(path, 484)];
  360. case 1:
  361. _a.sent();
  362. return [2 /*return*/];
  363. }
  364. });
  365. });
  366. };
  367. Builder.prototype.renameLinuxApp = function (targetDir, appRoot, pkg, config) {
  368. var src = path_1.resolve(targetDir, 'nw');
  369. var dest = path_1.resolve(targetDir, "" + pkg.name);
  370. return fs_extra_1.rename(src, dest);
  371. };
  372. Builder.prototype.prepareWinBuild = function (targetDir, appRoot, pkg, config) {
  373. return __awaiter(this, void 0, void 0, function () {
  374. return __generator(this, function (_a) {
  375. switch (_a.label) {
  376. case 0: return [4 /*yield*/, this.updateWinResources(targetDir, appRoot, pkg, config)];
  377. case 1:
  378. _a.sent();
  379. return [2 /*return*/];
  380. }
  381. });
  382. });
  383. };
  384. Builder.prototype.prepareMacBuild = function (targetDir, appRoot, pkg, config) {
  385. return __awaiter(this, void 0, void 0, function () {
  386. return __generator(this, function (_a) {
  387. switch (_a.label) {
  388. case 0: return [4 /*yield*/, this.updatePlist(targetDir, appRoot, pkg, config)];
  389. case 1:
  390. _a.sent();
  391. return [4 /*yield*/, this.updateMacIcon(targetDir, appRoot, pkg, config)];
  392. case 2:
  393. _a.sent();
  394. return [4 /*yield*/, this.fixMacMeta(targetDir, appRoot, pkg, config)];
  395. case 3:
  396. _a.sent();
  397. return [2 /*return*/];
  398. }
  399. });
  400. });
  401. };
  402. Builder.prototype.prepareLinuxBuild = function (targetDir, appRoot, pkg, config) {
  403. return __awaiter(this, void 0, void 0, function () {
  404. return __generator(this, function (_a) {
  405. switch (_a.label) {
  406. case 0: return [4 /*yield*/, this.fixLinuxMode(targetDir, appRoot, pkg, config)];
  407. case 1:
  408. _a.sent();
  409. return [2 /*return*/];
  410. }
  411. });
  412. });
  413. };
  414. Builder.prototype.copyFiles = function (platform, targetDir, appRoot, pkg, config) {
  415. return __awaiter(this, void 0, void 0, function () {
  416. var generalExcludes, dependenciesExcludes, ignore, files, _a, nwFile, tempDir, executable, _i, files_2, file, _b, files_3, file;
  417. return __generator(this, function (_c) {
  418. switch (_c.label) {
  419. case 0:
  420. generalExcludes = [
  421. '**/node_modules/.bin',
  422. '**/node_modules/*/{ example, examples, test, tests }',
  423. '**/{ .DS_Store, .git, .hg, .svn, *.log }',
  424. ];
  425. return [4 /*yield*/, util_1.findExcludableDependencies(this.dir, pkg)
  426. .then(function (excludable) {
  427. return excludable.map(function (excludable) { return [excludable, excludable + "/**/*"]; });
  428. })
  429. .then(function (excludes) {
  430. return Array.prototype.concat.apply([], excludes);
  431. })];
  432. case 1:
  433. dependenciesExcludes = _c.sent();
  434. debug('in copyFiles', 'dependenciesExcludes', dependenciesExcludes);
  435. ignore = config.excludes.concat(generalExcludes, dependenciesExcludes, [config.output, config.output + "/**/*"]);
  436. debug('in copyFiles', 'ignore', ignore);
  437. return [4 /*yield*/, globby(config.files, {
  438. cwd: this.dir,
  439. // TODO: https://github.com/isaacs/node-glob#options, warn for cyclic links.
  440. follow: true,
  441. mark: true,
  442. ignore: ignore,
  443. })];
  444. case 2:
  445. files = _c.sent();
  446. debug('in copyFiles', 'config.files', config.files);
  447. debug('in copyFiles', 'files', files);
  448. if (!config.packed) return [3 /*break*/, 21];
  449. _a = platform;
  450. switch (_a) {
  451. case 'win32': return [3 /*break*/, 3];
  452. case 'win': return [3 /*break*/, 3];
  453. case 'linux': return [3 /*break*/, 3];
  454. case 'darwin': return [3 /*break*/, 13];
  455. case 'osx': return [3 /*break*/, 13];
  456. case 'mac': return [3 /*break*/, 13];
  457. }
  458. return [3 /*break*/, 19];
  459. case 3: return [4 /*yield*/, util_1.tmpName({
  460. postfix: '.zip',
  461. })];
  462. case 4:
  463. nwFile = _c.sent();
  464. return [4 /*yield*/, util_1.compress(this.dir, files.filter(function (file) { return !file.endsWith('/'); }), 'zip', nwFile)];
  465. case 5:
  466. _c.sent();
  467. return [4 /*yield*/, util_1.tmpDir()];
  468. case 6:
  469. tempDir = (_c.sent()).path;
  470. return [4 /*yield*/, this.writeStrippedManifest(path_1.resolve(tempDir, 'package.json'), pkg, config)];
  471. case 7:
  472. _c.sent();
  473. return [4 /*yield*/, util_1.compress(tempDir, ['./package.json'], 'zip', nwFile)];
  474. case 8:
  475. _c.sent();
  476. return [4 /*yield*/, fs_extra_1.remove(tempDir)];
  477. case 9:
  478. _c.sent();
  479. return [4 /*yield*/, util_1.findExecutable(platform, targetDir)];
  480. case 10:
  481. executable = _c.sent();
  482. return [4 /*yield*/, this.combineExecutable(executable, nwFile)];
  483. case 11:
  484. _c.sent();
  485. return [4 /*yield*/, fs_extra_1.remove(nwFile)];
  486. case 12:
  487. _c.sent();
  488. return [3 /*break*/, 20];
  489. case 13:
  490. _i = 0, files_2 = files;
  491. _c.label = 14;
  492. case 14:
  493. if (!(_i < files_2.length)) return [3 /*break*/, 17];
  494. file = files_2[_i];
  495. return [4 /*yield*/, util_1.copyFileAsync(path_1.resolve(this.dir, file), path_1.resolve(appRoot, file))];
  496. case 15:
  497. _c.sent();
  498. _c.label = 16;
  499. case 16:
  500. _i++;
  501. return [3 /*break*/, 14];
  502. case 17: return [4 /*yield*/, this.writeStrippedManifest(path_1.resolve(appRoot, 'package.json'), pkg, config)];
  503. case 18:
  504. _c.sent();
  505. return [3 /*break*/, 20];
  506. case 19: throw new Error('ERROR_UNKNOWN_PLATFORM');
  507. case 20: return [3 /*break*/, 27];
  508. case 21:
  509. _b = 0, files_3 = files;
  510. _c.label = 22;
  511. case 22:
  512. if (!(_b < files_3.length)) return [3 /*break*/, 25];
  513. file = files_3[_b];
  514. return [4 /*yield*/, util_1.copyFileAsync(path_1.resolve(this.dir, file), path_1.resolve(appRoot, file))];
  515. case 23:
  516. _c.sent();
  517. _c.label = 24;
  518. case 24:
  519. _b++;
  520. return [3 /*break*/, 22];
  521. case 25: return [4 /*yield*/, this.writeStrippedManifest(path_1.resolve(appRoot, 'package.json'), pkg, config)];
  522. case 26:
  523. _c.sent();
  524. _c.label = 27;
  525. case 27: return [2 /*return*/];
  526. }
  527. });
  528. });
  529. };
  530. Builder.prototype.integrateFFmpeg = function (platform, arch, targetDir, pkg, config) {
  531. return __awaiter(this, void 0, void 0, function () {
  532. var downloader, ffmpegDir, src, dest;
  533. return __generator(this, function (_a) {
  534. switch (_a.label) {
  535. case 0:
  536. downloader = new FFmpegDownloader_1.FFmpegDownloader({
  537. platform: platform, arch: arch,
  538. version: config.nwVersion,
  539. useCaches: true,
  540. showProgress: this.options.mute ? false : true,
  541. });
  542. if (!this.options.mute) {
  543. console.info('Fetching FFmpeg prebuilt...', {
  544. platform: downloader.options.platform,
  545. arch: downloader.options.arch,
  546. version: downloader.options.version,
  547. });
  548. }
  549. return [4 /*yield*/, downloader.fetchAndExtract()];
  550. case 1:
  551. ffmpegDir = _a.sent();
  552. return [4 /*yield*/, util_1.findFFmpeg(platform, ffmpegDir)];
  553. case 2:
  554. src = _a.sent();
  555. return [4 /*yield*/, util_1.findFFmpeg(platform, targetDir)];
  556. case 3:
  557. dest = _a.sent();
  558. return [4 /*yield*/, fs_extra_1.copy(src, dest)];
  559. case 4:
  560. _a.sent();
  561. return [2 /*return*/];
  562. }
  563. });
  564. });
  565. };
  566. Builder.prototype.buildNsisDiffUpdater = function (platform, arch, versionInfo, fromVersion, toVersion, pkg, config) {
  567. return __awaiter(this, void 0, void 0, function () {
  568. var diffNsis, fromDir, _a, _b, toDir, _c, _d, data, script;
  569. return __generator(this, function (_e) {
  570. switch (_e.label) {
  571. case 0:
  572. diffNsis = path_1.resolve(this.dir, config.output, pkg.name + "-" + toVersion + "-from-" + fromVersion + "-" + platform + "-" + arch + "-Update.exe");
  573. _a = path_1.resolve;
  574. _b = [this.dir, config.output];
  575. return [4 /*yield*/, versionInfo.getVersion(fromVersion)];
  576. case 1:
  577. fromDir = _a.apply(void 0, _b.concat([(_e.sent()).source]));
  578. _c = path_1.resolve;
  579. _d = [this.dir, config.output];
  580. return [4 /*yield*/, versionInfo.getVersion(toVersion)];
  581. case 2:
  582. toDir = _c.apply(void 0, _d.concat([(_e.sent()).source]));
  583. return [4 /*yield*/, (new nsis_gen_1.NsisDiffer(fromDir, toDir, {
  584. // Basic.
  585. appName: config.win.productName,
  586. companyName: config.win.companyName,
  587. description: config.win.fileDescription,
  588. version: util_1.fixWindowsVersion(config.win.productVersion),
  589. copyright: config.win.copyright,
  590. icon: config.nsis.icon ? path_1.resolve(this.dir, config.nsis.icon) : undefined,
  591. unIcon: config.nsis.unIcon ? path_1.resolve(this.dir, config.nsis.unIcon) : undefined,
  592. // Compression.
  593. compression: 'lzma',
  594. solid: true,
  595. languages: config.nsis.languages,
  596. installDirectory: config.nsis.installDirectory,
  597. // Output.
  598. output: diffNsis,
  599. })).make()];
  600. case 3:
  601. data = _e.sent();
  602. return [4 /*yield*/, util_1.tmpName()];
  603. case 4:
  604. script = _e.sent();
  605. return [4 /*yield*/, fs_extra_1.writeFile(script, data)];
  606. case 5:
  607. _e.sent();
  608. return [4 /*yield*/, nsis_gen_1.nsisBuild(toDir, script, {
  609. mute: this.options.mute,
  610. })];
  611. case 6:
  612. _e.sent();
  613. return [4 /*yield*/, fs_extra_1.remove(script)];
  614. case 7:
  615. _e.sent();
  616. return [4 /*yield*/, versionInfo.addUpdater(toVersion, fromVersion, arch, diffNsis)];
  617. case 8:
  618. _e.sent();
  619. return [2 /*return*/];
  620. }
  621. });
  622. });
  623. };
  624. Builder.prototype.buildDirTarget = function (platform, arch, runtimeDir, pkg, config) {
  625. return __awaiter(this, void 0, void 0, function () {
  626. var targetDir, runtimeRoot, appRoot, _a;
  627. return __generator(this, function (_b) {
  628. switch (_b.label) {
  629. case 0:
  630. targetDir = path_1.resolve(this.dir, config.output, this.parseOutputPattern(config.outputPattern, {
  631. name: pkg.name,
  632. version: pkg.version,
  633. platform: platform, arch: arch,
  634. }, pkg, config));
  635. return [4 /*yield*/, util_1.findRuntimeRoot(platform, runtimeDir)];
  636. case 1:
  637. runtimeRoot = _b.sent();
  638. appRoot = path_1.resolve(targetDir, (function () {
  639. switch (platform) {
  640. case 'win32':
  641. case 'win':
  642. case 'linux':
  643. return './';
  644. case 'darwin':
  645. case 'osx':
  646. case 'mac':
  647. return './nwjs.app/Contents/Resources/app.nw/';
  648. default:
  649. throw new Error('ERROR_UNKNOWN_PLATFORM');
  650. }
  651. })());
  652. return [4 /*yield*/, fs_extra_1.emptyDir(targetDir)];
  653. case 2:
  654. _b.sent();
  655. return [4 /*yield*/, fs_extra_1.copy(runtimeRoot, targetDir, {})];
  656. case 3:
  657. _b.sent();
  658. if (!config.ffmpegIntegration) return [3 /*break*/, 5];
  659. return [4 /*yield*/, this.integrateFFmpeg(platform, arch, targetDir, pkg, config)];
  660. case 4:
  661. _b.sent();
  662. _b.label = 5;
  663. case 5: return [4 /*yield*/, fs_extra_1.ensureDir(appRoot)];
  664. case 6:
  665. _b.sent();
  666. _a = platform;
  667. switch (_a) {
  668. case 'win32': return [3 /*break*/, 7];
  669. case 'win': return [3 /*break*/, 7];
  670. case 'darwin': return [3 /*break*/, 11];
  671. case 'osx': return [3 /*break*/, 11];
  672. case 'mac': return [3 /*break*/, 11];
  673. case 'linux': return [3 /*break*/, 15];
  674. }
  675. return [3 /*break*/, 19];
  676. case 7: return [4 /*yield*/, this.prepareWinBuild(targetDir, appRoot, pkg, config)];
  677. case 8:
  678. _b.sent();
  679. return [4 /*yield*/, this.copyFiles(platform, targetDir, appRoot, pkg, config)];
  680. case 9:
  681. _b.sent();
  682. return [4 /*yield*/, this.renameWinApp(targetDir, appRoot, pkg, config)];
  683. case 10:
  684. _b.sent();
  685. return [3 /*break*/, 20];
  686. case 11: return [4 /*yield*/, this.prepareMacBuild(targetDir, appRoot, pkg, config)];
  687. case 12:
  688. _b.sent();
  689. return [4 /*yield*/, this.copyFiles(platform, targetDir, appRoot, pkg, config)];
  690. case 13:
  691. _b.sent();
  692. return [4 /*yield*/, this.renameMacApp(targetDir, appRoot, pkg, config)];
  693. case 14:
  694. _b.sent();
  695. return [3 /*break*/, 20];
  696. case 15: return [4 /*yield*/, this.prepareLinuxBuild(targetDir, appRoot, pkg, config)];
  697. case 16:
  698. _b.sent();
  699. return [4 /*yield*/, this.copyFiles(platform, targetDir, appRoot, pkg, config)];
  700. case 17:
  701. _b.sent();
  702. return [4 /*yield*/, this.renameLinuxApp(targetDir, appRoot, pkg, config)];
  703. case 18:
  704. _b.sent();
  705. return [3 /*break*/, 20];
  706. case 19: throw new Error('ERROR_UNKNOWN_PLATFORM');
  707. case 20: return [2 /*return*/, targetDir];
  708. }
  709. });
  710. });
  711. };
  712. Builder.prototype.buildArchiveTarget = function (type, sourceDir) {
  713. return __awaiter(this, void 0, void 0, function () {
  714. var targetArchive, files;
  715. return __generator(this, function (_a) {
  716. switch (_a.label) {
  717. case 0:
  718. targetArchive = path_1.resolve(path_1.dirname(sourceDir), path_1.basename(sourceDir) + "." + type);
  719. return [4 /*yield*/, fs_extra_1.remove(targetArchive)];
  720. case 1:
  721. _a.sent();
  722. return [4 /*yield*/, globby(['**/*'], {
  723. cwd: sourceDir,
  724. })];
  725. case 2:
  726. files = _a.sent();
  727. return [4 /*yield*/, util_1.compress(sourceDir, files, type, targetArchive)];
  728. case 3:
  729. _a.sent();
  730. return [2 /*return*/, targetArchive];
  731. }
  732. });
  733. });
  734. };
  735. Builder.prototype.buildNsisTarget = function (platform, arch, sourceDir, pkg, config) {
  736. return __awaiter(this, void 0, void 0, function () {
  737. var versionInfo, targetNsis, data, script, _i, _a, version;
  738. return __generator(this, function (_b) {
  739. switch (_b.label) {
  740. case 0:
  741. if (platform != 'win') {
  742. if (!this.options.mute) {
  743. console.info("Skip building nsis target for " + platform + ".");
  744. }
  745. return [2 /*return*/];
  746. }
  747. versionInfo = new common_1.NsisVersionInfo(path_1.resolve(this.dir, config.output, 'versions.nsis.json'));
  748. targetNsis = path_1.resolve(path_1.dirname(sourceDir), path_1.basename(sourceDir) + "-Setup.exe");
  749. return [4 /*yield*/, (new nsis_gen_1.NsisComposer({
  750. // Basic.
  751. appName: config.win.productName,
  752. companyName: config.win.companyName,
  753. description: config.win.fileDescription,
  754. version: util_1.fixWindowsVersion(config.win.productVersion),
  755. copyright: config.win.copyright,
  756. icon: config.nsis.icon ? path_1.resolve(this.dir, config.nsis.icon) : undefined,
  757. unIcon: config.nsis.unIcon ? path_1.resolve(this.dir, config.nsis.unIcon) : undefined,
  758. // Compression.
  759. compression: 'lzma',
  760. solid: true,
  761. languages: config.nsis.languages,
  762. installDirectory: config.nsis.installDirectory,
  763. // Output.
  764. output: targetNsis,
  765. })).make()];
  766. case 1:
  767. data = _b.sent();
  768. return [4 /*yield*/, util_1.tmpName()];
  769. case 2:
  770. script = _b.sent();
  771. return [4 /*yield*/, fs_extra_1.writeFile(script, data)];
  772. case 3:
  773. _b.sent();
  774. return [4 /*yield*/, nsis_gen_1.nsisBuild(sourceDir, script, {
  775. mute: this.options.mute,
  776. })];
  777. case 4:
  778. _b.sent();
  779. return [4 /*yield*/, fs_extra_1.remove(script)];
  780. case 5:
  781. _b.sent();
  782. return [4 /*yield*/, versionInfo.addVersion(pkg.version, '', sourceDir)];
  783. case 6:
  784. _b.sent();
  785. return [4 /*yield*/, versionInfo.addInstaller(pkg.version, arch, targetNsis)];
  786. case 7:
  787. _b.sent();
  788. if (!config.nsis.diffUpdaters) return [3 /*break*/, 12];
  789. _i = 0;
  790. return [4 /*yield*/, versionInfo.getVersions()];
  791. case 8:
  792. _a = _b.sent();
  793. _b.label = 9;
  794. case 9:
  795. if (!(_i < _a.length)) return [3 /*break*/, 12];
  796. version = _a[_i];
  797. if (!semver.gt(pkg.version, version)) return [3 /*break*/, 11];
  798. return [4 /*yield*/, this.buildNsisDiffUpdater(platform, arch, versionInfo, version, pkg.version, pkg, config)];
  799. case 10:
  800. _b.sent();
  801. _b.label = 11;
  802. case 11:
  803. _i++;
  804. return [3 /*break*/, 9];
  805. case 12: return [4 /*yield*/, versionInfo.save()];
  806. case 13:
  807. _b.sent();
  808. return [2 /*return*/];
  809. }
  810. });
  811. });
  812. };
  813. Builder.prototype.buildNsis7zTarget = function (platform, arch, sourceDir, pkg, config) {
  814. return __awaiter(this, void 0, void 0, function () {
  815. var sourceArchive, versionInfo, targetNsis, data, script, _i, _a, version;
  816. return __generator(this, function (_b) {
  817. switch (_b.label) {
  818. case 0:
  819. if (platform != 'win') {
  820. if (!this.options.mute) {
  821. console.info("Skip building nsis7z target for " + platform + ".");
  822. }
  823. return [2 /*return*/];
  824. }
  825. return [4 /*yield*/, this.buildArchiveTarget('7z', sourceDir)];
  826. case 1:
  827. sourceArchive = _b.sent();
  828. versionInfo = new common_1.NsisVersionInfo(path_1.resolve(this.dir, config.output, 'versions.nsis.json'));
  829. targetNsis = path_1.resolve(path_1.dirname(sourceDir), path_1.basename(sourceDir) + "-Setup.exe");
  830. return [4 /*yield*/, (new nsis_gen_1.Nsis7Zipper(sourceArchive, {
  831. // Basic.
  832. appName: config.win.productName,
  833. companyName: config.win.companyName,
  834. description: config.win.fileDescription,
  835. version: util_1.fixWindowsVersion(config.win.productVersion),
  836. copyright: config.win.copyright,
  837. icon: config.nsis.icon ? path_1.resolve(this.dir, config.nsis.icon) : undefined,
  838. unIcon: config.nsis.unIcon ? path_1.resolve(this.dir, config.nsis.unIcon) : undefined,
  839. // Compression.
  840. compression: 'lzma',
  841. solid: true,
  842. languages: config.nsis.languages,
  843. installDirectory: config.nsis.installDirectory,
  844. // Output.
  845. output: targetNsis,
  846. })).make()];
  847. case 2:
  848. data = _b.sent();
  849. return [4 /*yield*/, util_1.tmpName()];
  850. case 3:
  851. script = _b.sent();
  852. return [4 /*yield*/, fs_extra_1.writeFile(script, data)];
  853. case 4:
  854. _b.sent();
  855. return [4 /*yield*/, nsis_gen_1.nsisBuild(sourceDir, script, {
  856. mute: this.options.mute,
  857. })];
  858. case 5:
  859. _b.sent();
  860. return [4 /*yield*/, fs_extra_1.remove(script)];
  861. case 6:
  862. _b.sent();
  863. return [4 /*yield*/, versionInfo.addVersion(pkg.version, '', sourceDir)];
  864. case 7:
  865. _b.sent();
  866. return [4 /*yield*/, versionInfo.addInstaller(pkg.version, arch, targetNsis)];
  867. case 8:
  868. _b.sent();
  869. if (!config.nsis.diffUpdaters) return [3 /*break*/, 13];
  870. _i = 0;
  871. return [4 /*yield*/, versionInfo.getVersions()];
  872. case 9:
  873. _a = _b.sent();
  874. _b.label = 10;
  875. case 10:
  876. if (!(_i < _a.length)) return [3 /*break*/, 13];
  877. version = _a[_i];
  878. if (!semver.gt(pkg.version, version)) return [3 /*break*/, 12];
  879. return [4 /*yield*/, this.buildNsisDiffUpdater(platform, arch, versionInfo, version, pkg.version, pkg, config)];
  880. case 11:
  881. _b.sent();
  882. _b.label = 12;
  883. case 12:
  884. _i++;
  885. return [3 /*break*/, 10];
  886. case 13: return [4 /*yield*/, versionInfo.save()];
  887. case 14:
  888. _b.sent();
  889. return [2 /*return*/];
  890. }
  891. });
  892. });
  893. };
  894. Builder.prototype.buildTask = function (platform, arch, pkg, config) {
  895. return __awaiter(this, void 0, void 0, function () {
  896. var downloader, runtimeDir, started, targetDir, _i, _a, target, started_1, _b;
  897. return __generator(this, function (_c) {
  898. switch (_c.label) {
  899. case 0:
  900. if (platform === 'mac' && arch === 'x86' && !config.nwVersion.includes('0.12.3')) {
  901. if (!this.options.mute) {
  902. console.info("The NW.js binary for " + platform + ", " + arch + " isn't available for " + config.nwVersion + ", skipped.");
  903. }
  904. throw new Error('ERROR_TASK_MAC_X86_SKIPPED');
  905. }
  906. downloader = new Downloader_1.Downloader({
  907. platform: platform, arch: arch,
  908. version: config.nwVersion,
  909. flavor: config.nwFlavor,
  910. mirror: this.options.mirror,
  911. useCaches: true,
  912. showProgress: this.options.mute ? false : true,
  913. });
  914. if (!this.options.mute) {
  915. console.info('Fetching NW.js binary...', {
  916. platform: downloader.options.platform,
  917. arch: downloader.options.arch,
  918. version: downloader.options.version,
  919. flavor: downloader.options.flavor,
  920. });
  921. }
  922. return [4 /*yield*/, downloader.fetchAndExtract()];
  923. case 1:
  924. runtimeDir = _c.sent();
  925. if (!this.options.mute) {
  926. console.info('Building targets...');
  927. }
  928. started = Date.now();
  929. if (!this.options.mute) {
  930. console.info("Building directory target starts...");
  931. }
  932. return [4 /*yield*/, this.buildDirTarget(platform, arch, runtimeDir, pkg, config)];
  933. case 2:
  934. targetDir = _c.sent();
  935. if (!this.options.mute) {
  936. console.info("Building directory target ends within " + this.getTimeDiff(started) + "s.");
  937. }
  938. _i = 0, _a = config.targets;
  939. _c.label = 3;
  940. case 3:
  941. if (!(_i < _a.length)) return [3 /*break*/, 12];
  942. target = _a[_i];
  943. started_1 = Date.now();
  944. _b = target;
  945. switch (_b) {
  946. case 'zip': return [3 /*break*/, 4];
  947. case '7z': return [3 /*break*/, 4];
  948. case 'nsis': return [3 /*break*/, 6];
  949. case 'nsis7z': return [3 /*break*/, 8];
  950. }
  951. return [3 /*break*/, 10];
  952. case 4:
  953. if (!this.options.mute) {
  954. console.info("Building " + target + " archive target starts...");
  955. }
  956. return [4 /*yield*/, this.buildArchiveTarget(target, targetDir)];
  957. case 5:
  958. _c.sent();
  959. if (!this.options.mute) {
  960. console.info("Building " + target + " archive target ends within " + this.getTimeDiff(started_1) + "s.");
  961. }
  962. return [3 /*break*/, 11];
  963. case 6:
  964. if (!this.options.mute) {
  965. console.info("Building nsis target starts...");
  966. }
  967. return [4 /*yield*/, this.buildNsisTarget(platform, arch, targetDir, pkg, config)];
  968. case 7:
  969. _c.sent();
  970. if (!this.options.mute) {
  971. console.info("Building nsis target ends within " + this.getTimeDiff(started_1) + "s.");
  972. }
  973. return [3 /*break*/, 11];
  974. case 8:
  975. if (!this.options.mute) {
  976. console.info("Building nsis7z target starts...");
  977. }
  978. return [4 /*yield*/, this.buildNsis7zTarget(platform, arch, targetDir, pkg, config)];
  979. case 9:
  980. _c.sent();
  981. if (!this.options.mute) {
  982. console.info("Building nsis7z target ends within " + this.getTimeDiff(started_1) + "s.");
  983. }
  984. return [3 /*break*/, 11];
  985. case 10: throw new Error('ERROR_UNKNOWN_TARGET');
  986. case 11:
  987. _i++;
  988. return [3 /*break*/, 3];
  989. case 12: return [2 /*return*/];
  990. }
  991. });
  992. });
  993. };
  994. Builder.DEFAULT_OPTIONS = {
  995. win: false,
  996. mac: false,
  997. linux: false,
  998. x86: false,
  999. x64: false,
  1000. tasks: [],
  1001. chromeApp: false,
  1002. mirror: Downloader_1.Downloader.DEFAULT_OPTIONS.mirror,
  1003. concurrent: false,
  1004. mute: true,
  1005. };
  1006. return Builder;
  1007. }());
  1008. exports.Builder = Builder;
  1009. //# sourceMappingURL=Builder.js.map
Buy Me A Coffee