From ff54c2070d78cb34ab312a5b56627c5ef7acd0f8 Mon Sep 17 00:00:00 2001 From: antelle Date: Sat, 9 Jan 2021 21:34:59 +0100 Subject: [PATCH] fixed the macos updater --- package/osx/installer.js | 46 ++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/package/osx/installer.js b/package/osx/installer.js index f120c539..9efd28c1 100644 --- a/package/osx/installer.js +++ b/package/osx/installer.js @@ -15,7 +15,17 @@ if (args.update) { var target = checkFilePath(args.app, 'app'); var targetOwner = app.doShellScript("stat -f '%Su' " + target); - var setAdminRights = targetOwner === 'root'; + var setOwnerToRoot = targetOwner === 'root'; + var runAsAdmin = setOwnerToRoot; + if (!runAsAdmin) { + try { + app.doShellScript('test -w ' + target); + var targetDir = target.replace(/[^\/]*$/, ''); + app.doShellScript('test -w ' + targetDir); + } catch (e) { + runAsAdmin = true; + } + } var tmpDir = dmg + '.mount'; @@ -31,15 +41,25 @@ if (args.update) { 'rm -rf ' + tmpDir ]; var scriptOptions = {}; - if (setAdminRights) { - script.push('chown -R 0 ' + target); + if (runAsAdmin) { scriptOptions.administratorPrivileges = true; + if (setOwnerToRoot) { + script.push('chown -R 0 ' + target); + } } script.push('open ' + target); script = script.join('\n'); - app.doShellScript(script, scriptOptions); + try { + runScriptOrDie(script, scriptOptions); + } catch (e) { + try { + app.doShellScript('hdiutil detach ' + tmpDir); + app.doShellScript('rm -rf ' + tmpDir); + } catch (e) {} + throw e; + } } else if (args.install) { - app.doShellScript('chown -R 0 /Applications/KeeWeb.app', { administratorPrivileges: true }); + runScriptOrDie('chown -R 0 /Applications/KeeWeb.app', { administratorPrivileges: true }); } else { throw 'Unknown operation'; } @@ -70,7 +90,9 @@ function waitForExitOrKill(pid) { } delay(1); } - app.doShellScript('kill ' + pid); + try { + app.doShellScript('kill ' + pid); + } catch (e) {} } function checkFilePath(path, ext) { @@ -86,3 +108,15 @@ function checkFilePath(path, ext) { } return file.posixPath().replace(/ /g, '\\ '); } + +function runScriptOrDie(script, options) { + try { + return app.doShellScript(script, options || {}); + } catch (e) { + if (e.message === 'User canceled.') { + $.exit(1); + } else { + throw e; + } + } +}