2018-03-09 19:49:35 +02:00
|
|
|
const { dialog } = require("electron");
|
|
|
|
const { autoUpdater } = require("electron-updater");
|
|
|
|
const { Logger } = require("lib/logger.js");
|
|
|
|
const { _ } = require("lib/locale.js");
|
2018-01-31 00:35:50 +02:00
|
|
|
|
|
|
|
let autoUpdateLogger_ = new Logger();
|
|
|
|
let checkInBackground_ = false;
|
2018-02-16 01:05:04 +02:00
|
|
|
let isCheckingForUpdate_ = false;
|
|
|
|
let parentWindow_ = null;
|
2018-01-31 00:35:50 +02:00
|
|
|
|
2018-02-06 15:11:59 +02:00
|
|
|
// Note: Electron Builder's autoUpdater is incredibly buggy so currently it's only used
|
|
|
|
// to detect if a new version is present. If it is, the download link is simply opened
|
|
|
|
// in a new browser window.
|
2018-01-31 00:35:50 +02:00
|
|
|
autoUpdater.autoDownload = false;
|
|
|
|
|
2018-02-06 15:11:59 +02:00
|
|
|
function htmlToText_(html) {
|
2018-03-09 19:49:35 +02:00
|
|
|
let output = html.replace(/\n/g, "");
|
|
|
|
output = output.replace(/<li>/g, "- ");
|
|
|
|
output = output.replace(/<p>/g, "");
|
|
|
|
output = output.replace(/<\/p>/g, "\n");
|
|
|
|
output = output.replace(/<\/li>/g, "\n");
|
|
|
|
output = output.replace(/<ul>/g, "");
|
|
|
|
output = output.replace(/<\/ul>/g, "");
|
|
|
|
output = output.replace(/<.*?>/g, "");
|
|
|
|
output = output.replace(/<\/.*?>/g, "");
|
2018-02-06 15:11:59 +02:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-02-16 01:05:04 +02:00
|
|
|
function showErrorMessageBox(message) {
|
|
|
|
return dialog.showMessageBox(parentWindow_, {
|
2018-03-09 19:49:35 +02:00
|
|
|
type: "error",
|
2018-02-16 01:05:04 +02:00
|
|
|
message: message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onCheckStarted() {
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdateLogger_.info("checkForUpdates: Starting...");
|
2018-02-16 01:05:04 +02:00
|
|
|
isCheckingForUpdate_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onCheckEnded() {
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdateLogger_.info("checkForUpdates: Done.");
|
2018-02-16 01:05:04 +02:00
|
|
|
isCheckingForUpdate_ = false;
|
|
|
|
}
|
|
|
|
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdater.on("error", error => {
|
2018-02-16 01:05:04 +02:00
|
|
|
autoUpdateLogger_.error(error);
|
|
|
|
if (checkInBackground_) return onCheckEnded();
|
2018-02-20 20:07:31 +02:00
|
|
|
|
|
|
|
let msg = error == null ? "unknown" : (error.stack || error).toString();
|
2018-03-09 19:49:35 +02:00
|
|
|
// Error messages can be very long even without stack trace so shorten
|
2018-02-20 20:07:31 +02:00
|
|
|
// then so that the dialog box doesn't take the whole screen.
|
2018-03-09 19:49:35 +02:00
|
|
|
msg = msg.substr(0, 512).replace(/\\n/g, "\n");
|
|
|
|
showErrorMessageBox(msg);
|
|
|
|
|
2018-02-16 01:05:04 +02:00
|
|
|
onCheckEnded();
|
2018-03-09 19:49:35 +02:00
|
|
|
});
|
2018-02-16 01:05:04 +02:00
|
|
|
|
2018-02-16 20:08:02 +02:00
|
|
|
function findDownloadFilename_(info) {
|
|
|
|
// { version: '1.0.64',
|
2018-03-09 19:49:35 +02:00
|
|
|
// files:
|
2018-02-16 20:08:02 +02:00
|
|
|
// [ { url: 'Joplin-1.0.64-mac.zip',
|
|
|
|
// sha512: 'OlemXqhq/fSifx7EutvMzfoCI/1kGNl10i8nkvACEDfVXwP8hankDBXEC0+GxSArsZuxOh3U1+C+4j72SfIUew==' },
|
|
|
|
// { url: 'Joplin-1.0.64.dmg',
|
|
|
|
// sha512: 'jAewQQoJ3nCaOj8hWDgf0sc3LBbAWQtiKqfTflK8Hc3Dh7fAy9jRHfFAZKFUZ9ll95Bun0DVsLq8wLSUrdsMXw==',
|
|
|
|
// size: 77104485 } ],
|
|
|
|
// path: 'Joplin-1.0.64-mac.zip',
|
|
|
|
// sha512: 'OlemXqhq/fSifx7EutvMzfoCI/1kGNl10i8nkvACEDfVXwP8hankDBXEC0+GxSArsZuxOh3U1+C+4j72SfIUew==',
|
|
|
|
// releaseDate: '2018-02-16T00:13:01.634Z',
|
|
|
|
// releaseName: 'v1.0.64',
|
|
|
|
// releaseNotes: '<p>Still more fixes and im...' }
|
|
|
|
|
|
|
|
if (!info) return null;
|
|
|
|
|
|
|
|
if (!info.files) {
|
|
|
|
// info.path seems to contain a default, though not a good one,
|
|
|
|
// so the loop below if preferable to find the right file.
|
|
|
|
return info.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < info.files.length; i++) {
|
|
|
|
const f = info.files[i].url; // Annoyingly this is called "url" but it's obviously not a url, so hopefully it won't change later on and become one.
|
2018-03-09 19:49:35 +02:00
|
|
|
if (f.indexOf(".exe") >= 0) return f;
|
|
|
|
if (f.indexOf(".dmg") >= 0) return f;
|
2018-02-16 20:08:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return info.path;
|
|
|
|
}
|
|
|
|
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdater.on("update-available", info => {
|
2018-02-16 20:08:02 +02:00
|
|
|
const filename = findDownloadFilename_(info);
|
|
|
|
|
|
|
|
if (!info.version || !filename) {
|
2018-02-16 01:05:04 +02:00
|
|
|
if (checkInBackground_) return onCheckEnded();
|
2018-03-09 19:49:35 +02:00
|
|
|
showErrorMessageBox("Could not get version info: " + JSON.stringify(info));
|
2018-02-16 01:05:04 +02:00
|
|
|
return onCheckEnded();
|
2018-02-06 15:11:59 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 19:49:35 +02:00
|
|
|
const downloadUrl = "https://github.com/laurent22/joplin/releases/download/v" + info.version + "/" + filename;
|
2018-02-06 15:11:59 +02:00
|
|
|
|
2018-03-09 19:49:35 +02:00
|
|
|
let releaseNotes = info.releaseNotes + "";
|
|
|
|
if (releaseNotes) releaseNotes = "\n\n" + _("Release notes:\n\n%s", htmlToText_(releaseNotes));
|
2018-02-06 15:11:59 +02:00
|
|
|
|
2018-02-16 01:05:04 +02:00
|
|
|
const buttonIndex = dialog.showMessageBox(parentWindow_, {
|
2018-03-09 19:49:35 +02:00
|
|
|
type: "info",
|
|
|
|
message: _("An update is available, do you want to download it now?" + releaseNotes),
|
|
|
|
buttons: [_("Yes"), _("No")],
|
2018-02-16 01:05:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
onCheckEnded();
|
|
|
|
|
2018-03-09 19:49:35 +02:00
|
|
|
if (buttonIndex === 0) require("electron").shell.openExternal(downloadUrl);
|
|
|
|
});
|
2018-01-31 00:35:50 +02:00
|
|
|
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdater.on("update-not-available", () => {
|
2018-02-16 01:05:04 +02:00
|
|
|
if (checkInBackground_) return onCheckEnded();
|
2018-03-09 19:49:35 +02:00
|
|
|
dialog.showMessageBox({ message: _("Current version is up-to-date.") });
|
2018-02-16 01:05:04 +02:00
|
|
|
onCheckEnded();
|
2018-03-09 19:49:35 +02:00
|
|
|
});
|
2018-01-31 00:35:50 +02:00
|
|
|
|
2018-02-16 01:05:04 +02:00
|
|
|
function checkForUpdates(inBackground, window, logFilePath) {
|
|
|
|
if (isCheckingForUpdate_) {
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdateLogger_.info("checkForUpdates: Skipping check because it is already running");
|
2018-02-16 01:05:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentWindow_ = window;
|
|
|
|
|
|
|
|
onCheckStarted();
|
|
|
|
|
2018-01-31 00:35:50 +02:00
|
|
|
if (logFilePath && !autoUpdateLogger_.targets().length) {
|
|
|
|
autoUpdateLogger_ = new Logger();
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdateLogger_.addTarget("file", { path: logFilePath });
|
2018-01-31 00:35:50 +02:00
|
|
|
autoUpdateLogger_.setLevel(Logger.LEVEL_DEBUG);
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdateLogger_.info("checkForUpdates: Initializing...");
|
2018-01-31 00:35:50 +02:00
|
|
|
autoUpdater.logger = autoUpdateLogger_;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkInBackground_ = inBackground;
|
|
|
|
|
2018-01-31 21:10:45 +02:00
|
|
|
try {
|
2018-03-09 19:49:35 +02:00
|
|
|
autoUpdater.checkForUpdates();
|
2018-01-31 21:10:45 +02:00
|
|
|
} catch (error) {
|
|
|
|
autoUpdateLogger_.error(error);
|
2018-02-16 01:05:04 +02:00
|
|
|
if (!checkInBackground_) showErrorMessageBox(error.message);
|
|
|
|
onCheckEnded();
|
2018-01-31 21:10:45 +02:00
|
|
|
}
|
2018-01-31 00:35:50 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 19:49:35 +02:00
|
|
|
module.exports.checkForUpdates = checkForUpdates;
|