You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  26. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  27. return new (P || (P = Promise))(function (resolve, reject) {
  28. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  29. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  30. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  31. step((generator = generator.apply(thisArg, _arguments || [])).next());
  32. });
  33. };
  34. var __generator = (this && this.__generator) || function (thisArg, body) {
  35. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  36. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  37. function verb(n) { return function (v) { return step([n, v]); }; }
  38. function step(op) {
  39. if (f) throw new TypeError("Generator is already executing.");
  40. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  41. 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;
  42. if (y = 0, t) op = [op[0] & 2, t.value];
  43. switch (op[0]) {
  44. case 0: case 1: t = op; break;
  45. case 4: _.label++; return { value: op[1], done: false };
  46. case 5: _.label++; y = op[1]; op = [0]; continue;
  47. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  48. default:
  49. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  50. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  51. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  52. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  53. if (t[2]) _.ops.pop();
  54. _.trys.pop(); continue;
  55. }
  56. op = body.call(thisArg, _);
  57. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  58. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  59. }
  60. };
  61. var __importDefault = (this && this.__importDefault) || function (mod) {
  62. return (mod && mod.__esModule) ? mod : { "default": mod };
  63. };
  64. Object.defineProperty(exports, "__esModule", { value: true });
  65. exports.createWindowsInstaller = exports.convertVersion = void 0;
  66. var asar = __importStar(require("@electron/asar"));
  67. var temp_utils_1 = require("./temp-utils");
  68. var fs = __importStar(require("fs-extra"));
  69. var path = __importStar(require("path"));
  70. var os = __importStar(require("os"));
  71. var child_process_1 = require("child_process");
  72. var spawn_promise_1 = __importDefault(require("./spawn-promise"));
  73. var lodash_1 = require("lodash");
  74. var sign_1 = require("./sign");
  75. var log = require('debug')('electron-windows-installer:main');
  76. function convertVersion(version) {
  77. var parts = version.split('+')[0].split('-');
  78. var mainVersion = parts.shift();
  79. if (parts.length > 0) {
  80. return [mainVersion, parts.join('-').replace(/\./g, '')].join('-');
  81. }
  82. else {
  83. return mainVersion;
  84. }
  85. }
  86. exports.convertVersion = convertVersion;
  87. function checkIfCommandExists(command) {
  88. var checkCommand = os.platform() === 'win32' ? 'where' : 'which';
  89. return new Promise(function (resolve) {
  90. (0, child_process_1.exec)("".concat(checkCommand, " ").concat(command), function (error) {
  91. resolve(error ? false : true);
  92. });
  93. });
  94. }
  95. function createWindowsInstaller(options) {
  96. return __awaiter(this, void 0, void 0, function () {
  97. var useMono, monoExe, wineExe, _a, hasWine, hasMono, appDirectory, outputDirectory, loadingGif, vendorPath, vendorUpdate, appUpdate, cmd_1, args_1, defaultLoadingGif, certificateFile, certificatePassword, remoteReleases, signWithParams, remoteToken, windowsSign, metadata, appResources, asarFile, appMetadata, templatePath, templateData, _i, _b, f, nuspecContent, nugetOutput, targetNuspecPath, cmd, args, _c, nupkgPath, _d, _e, setupPath, unfixedSetupPath, msiPath, unfixedMsiPath;
  98. return __generator(this, function (_f) {
  99. switch (_f.label) {
  100. case 0:
  101. useMono = false;
  102. monoExe = 'mono';
  103. wineExe = ['arm64', 'x64'].includes(process.arch) ? 'wine64' : 'wine';
  104. if (!(process.platform !== 'win32')) return [3 /*break*/, 2];
  105. useMono = true;
  106. return [4 /*yield*/, Promise.all([
  107. checkIfCommandExists(wineExe),
  108. checkIfCommandExists(monoExe)
  109. ])];
  110. case 1:
  111. _a = _f.sent(), hasWine = _a[0], hasMono = _a[1];
  112. if (!hasWine || !hasMono) {
  113. throw new Error('You must install both Mono and Wine on non-Windows');
  114. }
  115. log("Using Mono: '".concat(monoExe, "'"));
  116. log("Using Wine: '".concat(wineExe, "'"));
  117. _f.label = 2;
  118. case 2:
  119. appDirectory = options.appDirectory, outputDirectory = options.outputDirectory, loadingGif = options.loadingGif;
  120. outputDirectory = path.resolve(outputDirectory || 'installer');
  121. vendorPath = path.join(__dirname, '..', 'vendor');
  122. vendorUpdate = path.join(vendorPath, 'Squirrel.exe');
  123. appUpdate = path.join(appDirectory, 'Squirrel.exe');
  124. return [4 /*yield*/, fs.copy(vendorUpdate, appUpdate)];
  125. case 3:
  126. _f.sent();
  127. if (!(options.setupIcon && (options.skipUpdateIcon !== true))) return [3 /*break*/, 5];
  128. cmd_1 = path.join(vendorPath, 'rcedit.exe');
  129. args_1 = [
  130. appUpdate,
  131. '--set-icon', options.setupIcon
  132. ];
  133. if (useMono) {
  134. args_1.unshift(cmd_1);
  135. cmd_1 = wineExe;
  136. }
  137. return [4 /*yield*/, (0, spawn_promise_1.default)(cmd_1, args_1)];
  138. case 4:
  139. _f.sent();
  140. _f.label = 5;
  141. case 5:
  142. defaultLoadingGif = path.join(__dirname, '..', 'resources', 'install-spinner.gif');
  143. loadingGif = loadingGif ? path.resolve(loadingGif) : defaultLoadingGif;
  144. certificateFile = options.certificateFile, certificatePassword = options.certificatePassword, remoteReleases = options.remoteReleases, signWithParams = options.signWithParams, remoteToken = options.remoteToken, windowsSign = options.windowsSign;
  145. metadata = {
  146. description: '',
  147. iconUrl: 'https://raw.githubusercontent.com/electron/electron/main/shell/browser/resources/win/electron.ico'
  148. };
  149. if (!(options.usePackageJson !== false)) return [3 /*break*/, 10];
  150. appResources = path.join(appDirectory, 'resources');
  151. asarFile = path.join(appResources, 'app.asar');
  152. appMetadata = void 0;
  153. return [4 /*yield*/, fs.pathExists(asarFile)];
  154. case 6:
  155. if (!_f.sent()) return [3 /*break*/, 7];
  156. appMetadata = JSON.parse(asar.extractFile(asarFile, 'package.json').toString());
  157. return [3 /*break*/, 9];
  158. case 7: return [4 /*yield*/, fs.readJson(path.join(appResources, 'app', 'package.json'))];
  159. case 8:
  160. appMetadata = _f.sent();
  161. _f.label = 9;
  162. case 9:
  163. Object.assign(metadata, {
  164. exe: "".concat(appMetadata.name, ".exe"),
  165. title: appMetadata.productName || appMetadata.name
  166. }, appMetadata);
  167. _f.label = 10;
  168. case 10:
  169. Object.assign(metadata, options);
  170. if (!metadata.authors) {
  171. if (typeof (metadata.author) === 'string') {
  172. metadata.authors = metadata.author;
  173. }
  174. else {
  175. metadata.authors = (metadata.author || {}).name || '';
  176. }
  177. }
  178. metadata.owners = metadata.owners || metadata.authors;
  179. metadata.version = convertVersion(metadata.version);
  180. metadata.copyright = metadata.copyright ||
  181. "Copyright \u00A9 ".concat(new Date().getFullYear(), " ").concat(metadata.authors || metadata.owners);
  182. metadata.additionalFiles = metadata.additionalFiles || [];
  183. return [4 /*yield*/, fs.pathExists(path.join(appDirectory, 'swiftshader'))];
  184. case 11:
  185. if (_f.sent()) {
  186. metadata.additionalFiles.push({ src: 'swiftshader\\**', target: 'lib\\net45\\swiftshader' });
  187. }
  188. return [4 /*yield*/, fs.pathExists(path.join(appDirectory, 'vk_swiftshader_icd.json'))];
  189. case 12:
  190. if (_f.sent()) {
  191. metadata.additionalFiles.push({ src: 'vk_swiftshader_icd.json', target: 'lib\\net45' });
  192. }
  193. templatePath = options.nuspecTemplate || path.join(__dirname, '..', 'template.nuspectemplate');
  194. return [4 /*yield*/, fs.readFile(templatePath, 'utf8')];
  195. case 13:
  196. templateData = _f.sent();
  197. if (path.sep === '/') {
  198. templateData = templateData.replace(/\\/g, '/');
  199. for (_i = 0, _b = metadata.additionalFiles; _i < _b.length; _i++) {
  200. f = _b[_i];
  201. f.src = f.src.replace(/\\/g, '/');
  202. f.target = f.target.replace(/\\/g, '/');
  203. }
  204. }
  205. nuspecContent = (0, lodash_1.template)(templateData)(metadata);
  206. log("Created NuSpec file:\n".concat(nuspecContent));
  207. return [4 /*yield*/, (0, temp_utils_1.createTempDir)('si-')];
  208. case 14:
  209. nugetOutput = _f.sent();
  210. targetNuspecPath = path.join(nugetOutput, metadata.name + '.nuspec');
  211. return [4 /*yield*/, fs.writeFile(targetNuspecPath, nuspecContent)];
  212. case 15:
  213. _f.sent();
  214. cmd = path.join(vendorPath, 'nuget.exe');
  215. args = [
  216. 'pack', targetNuspecPath,
  217. '-BasePath', appDirectory,
  218. '-OutputDirectory', nugetOutput,
  219. '-NoDefaultExcludes'
  220. ];
  221. if (useMono) {
  222. args.unshift(cmd);
  223. cmd = monoExe;
  224. }
  225. // Call NuGet to create our package
  226. _c = log;
  227. return [4 /*yield*/, (0, spawn_promise_1.default)(cmd, args)];
  228. case 16:
  229. // Call NuGet to create our package
  230. _c.apply(void 0, [_f.sent()]);
  231. nupkgPath = path.join(nugetOutput, "".concat(metadata.name, ".").concat(metadata.version, ".nupkg"));
  232. if (!remoteReleases) return [3 /*break*/, 18];
  233. cmd = path.join(vendorPath, 'SyncReleases.exe');
  234. args = ['-u', remoteReleases, '-r', outputDirectory];
  235. if (useMono) {
  236. args.unshift(cmd);
  237. cmd = monoExe;
  238. }
  239. if (remoteToken) {
  240. args.push('-t', remoteToken);
  241. }
  242. _d = log;
  243. return [4 /*yield*/, (0, spawn_promise_1.default)(cmd, args)];
  244. case 17:
  245. _d.apply(void 0, [_f.sent()]);
  246. _f.label = 18;
  247. case 18:
  248. cmd = path.join(vendorPath, 'Squirrel.exe');
  249. args = [
  250. '--releasify', nupkgPath,
  251. '--releaseDir', outputDirectory,
  252. '--loadingGif', loadingGif
  253. ];
  254. if (useMono) {
  255. args.unshift(path.join(vendorPath, 'Squirrel-Mono.exe'));
  256. cmd = monoExe;
  257. }
  258. // Legacy codesign options
  259. return [4 /*yield*/, (0, sign_1.resetSignTool)()];
  260. case 19:
  261. // Legacy codesign options
  262. _f.sent();
  263. if (!signWithParams) return [3 /*break*/, 20];
  264. args.push('--signWithParams');
  265. if (!signWithParams.includes('/f') && !signWithParams.includes('/p') && certificateFile && certificatePassword) {
  266. args.push("".concat(signWithParams, " /a /f \"").concat(path.resolve(certificateFile), "\" /p \"").concat(certificatePassword, "\""));
  267. }
  268. else {
  269. args.push(signWithParams);
  270. }
  271. return [3 /*break*/, 23];
  272. case 20:
  273. if (!(certificateFile && certificatePassword)) return [3 /*break*/, 21];
  274. args.push('--signWithParams');
  275. args.push("/a /f \"".concat(path.resolve(certificateFile), "\" /p \"").concat(certificatePassword, "\""));
  276. return [3 /*break*/, 23];
  277. case 21:
  278. if (!windowsSign) return [3 /*break*/, 23];
  279. args.push('--signWithParams');
  280. args.push('windows-sign');
  281. return [4 /*yield*/, (0, sign_1.createSignTool)(options)];
  282. case 22:
  283. _f.sent();
  284. _f.label = 23;
  285. case 23:
  286. if (options.setupIcon) {
  287. args.push('--setupIcon');
  288. args.push(path.resolve(options.setupIcon));
  289. }
  290. if (options.noMsi) {
  291. args.push('--no-msi');
  292. }
  293. if (options.noDelta) {
  294. args.push('--no-delta');
  295. }
  296. if (options.frameworkVersion) {
  297. args.push('--framework-version');
  298. args.push(options.frameworkVersion);
  299. }
  300. _e = log;
  301. return [4 /*yield*/, (0, spawn_promise_1.default)(cmd, args)];
  302. case 24:
  303. _e.apply(void 0, [_f.sent()]);
  304. if (!(options.fixUpPaths !== false)) return [3 /*break*/, 29];
  305. log('Fixing up paths');
  306. if (!(metadata.productName || options.setupExe)) return [3 /*break*/, 26];
  307. setupPath = path.join(outputDirectory, options.setupExe || "".concat(metadata.productName, "Setup.exe"));
  308. unfixedSetupPath = path.join(outputDirectory, 'Setup.exe');
  309. log("Renaming ".concat(unfixedSetupPath, " => ").concat(setupPath));
  310. return [4 /*yield*/, fs.rename(unfixedSetupPath, setupPath)];
  311. case 25:
  312. _f.sent();
  313. _f.label = 26;
  314. case 26:
  315. if (!(metadata.productName || options.setupMsi)) return [3 /*break*/, 29];
  316. msiPath = path.join(outputDirectory, options.setupMsi || "".concat(metadata.productName, "Setup.msi"));
  317. unfixedMsiPath = path.join(outputDirectory, 'Setup.msi');
  318. return [4 /*yield*/, fs.pathExists(unfixedMsiPath)];
  319. case 27:
  320. if (!_f.sent()) return [3 /*break*/, 29];
  321. log("Renaming ".concat(unfixedMsiPath, " => ").concat(msiPath));
  322. return [4 /*yield*/, fs.rename(unfixedMsiPath, msiPath)];
  323. case 28:
  324. _f.sent();
  325. _f.label = 29;
  326. case 29: return [4 /*yield*/, (0, sign_1.resetSignTool)()];
  327. case 30:
  328. _f.sent();
  329. return [2 /*return*/];
  330. }
  331. });
  332. });
  333. }
  334. exports.createWindowsInstaller = createWindowsInstaller;
  335. //# sourceMappingURL=index.js.map