1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.showUpdateDialog = exports.checkForUpdates = exports.setUpdateNotification = exports.defaultOptions = void 0;
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const axios_1 = __importDefault(require("axios"));
|
9 | const electron_1 = __importDefault(require("electron"));
|
10 | const compare_versions_1 = require("compare-versions");
|
11 | const github_url_to_object_1 = __importDefault(require("github-url-to-object"));
|
12 | exports.defaultOptions = {
|
13 | debug: false,
|
14 | silent: true,
|
15 | };
|
16 | function setUpdateNotification(options = exports.defaultOptions) {
|
17 | const withDefaults = Object.assign(exports.defaultOptions, options);
|
18 | if (electron_1.default.app.isReady()) {
|
19 | checkForUpdates(withDefaults);
|
20 | }
|
21 | else {
|
22 | electron_1.default.app.on('ready', () => {
|
23 | checkForUpdates(withDefaults);
|
24 | });
|
25 | }
|
26 | }
|
27 | exports.setUpdateNotification = setUpdateNotification;
|
28 | async function checkForUpdates({ repository, token, debug, silent } = exports.defaultOptions) {
|
29 | if (!electron_1.default.app.isPackaged && !debug)
|
30 | return;
|
31 | if (!repository) {
|
32 | const pkg = require(path_1.default.join(electron_1.default.app.getAppPath(), 'package.json'));
|
33 | const ghObj = (0, github_url_to_object_1.default)(pkg.repository);
|
34 | if (!ghObj) {
|
35 | throw new Error('Repository URL not found in package.json file.');
|
36 | }
|
37 | repository = ghObj.user + '/' + ghObj.repo;
|
38 | }
|
39 | let latestRelease = null;
|
40 | try {
|
41 | const { data: releases } = await axios_1.default.get(`https://api.github.com/repos/${repository}/releases`, {
|
42 | headers: token ? { authorization: `token ${token}` } : {},
|
43 | });
|
44 | latestRelease = releases[0];
|
45 | }
|
46 | catch (error) {
|
47 | console.error(error);
|
48 | if (!silent) {
|
49 | showDialog('Unable to check for updates at this moment. Try again.', 'error');
|
50 | }
|
51 | }
|
52 | if (!latestRelease)
|
53 | return;
|
54 | if ((0, compare_versions_1.compare)(latestRelease.tag_name, electron_1.default.app.getVersion(), '>')) {
|
55 | showUpdateDialog(latestRelease);
|
56 | }
|
57 | else {
|
58 | if (!silent) {
|
59 | showDialog(`You are already running the latest version.`);
|
60 | }
|
61 | }
|
62 | }
|
63 | exports.checkForUpdates = checkForUpdates;
|
64 | function showUpdateDialog(release) {
|
65 | electron_1.default.dialog.showMessageBox({
|
66 | title: electron_1.default.app.getName(),
|
67 | type: 'info',
|
68 | message: `New release available`,
|
69 | detail: `Installed Version: ${electron_1.default.app.getVersion()}\nLatest Version: ${release.tag_name}\n\n${release.body}`.trim(),
|
70 | buttons: ['Download', 'Later'],
|
71 | defaultId: 0,
|
72 | cancelId: 1,
|
73 | })
|
74 | .then(({ response }) => {
|
75 | if (response === 0) {
|
76 | setImmediate(() => {
|
77 | electron_1.default.shell.openExternal(release.html_url);
|
78 | });
|
79 | }
|
80 | })
|
81 | .catch((error) => {
|
82 | throw new Error(error);
|
83 | });
|
84 | }
|
85 | exports.showUpdateDialog = showUpdateDialog;
|
86 | const showDialog = (detail, type = 'info') => {
|
87 | electron_1.default.dialog.showMessageBox({
|
88 | title: electron_1.default.app.getName(),
|
89 | message: 'Update checker',
|
90 | buttons: ['Close'],
|
91 | defaultId: 0,
|
92 | cancelId: 0,
|
93 | type,
|
94 | detail,
|
95 | });
|
96 | };
|