From 479205c2c651db2c29a9252a3c454317a089512d Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 19 Nov 2015 13:31:45 -0400 Subject: [PATCH] osx: Fix first time elevation failure Currently we fire and forget `sudoPrompt.exec()` and run a timeout for `process.exit(0)` right away, however this means that the timeout will start as soon as the elevation dialog is shown when no sudo cache exists and therefore the timeout will finish before the user can type his password and submit. The fix is to keep the parent alive until the children dies, but call `app.dock.hide()` to make the parent completely invisible to the operating system. Fixes: https://github.com/resin-io/herostratus/issues/31 --- lib/elevate.js | 20 +++++++++++++++----- lib/herostratus.js | 2 +- package.json | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/elevate.js b/lib/elevate.js index 788e39e5..2d510d93 100644 --- a/lib/elevate.js +++ b/lib/elevate.js @@ -29,7 +29,7 @@ var windosu = require('windosu'); var os = require('os'); var platform = os.platform(); -exports.require = function(callback) { +exports.require = function(app, callback) { 'use strict'; isElevated(function(error, elevated) { @@ -44,11 +44,21 @@ exports.require = function(callback) { if (!elevated) { if (platform === 'darwin') { - sudoPrompt.setName('Herostratus'); - sudoPrompt.exec(process.argv.join(' ')); - setTimeout(function() { + + // Keep parent process hidden + app.dock.hide(); + + sudoPrompt.exec(process.argv.join(' '), { + name: 'Herostratus' + }, function(error) { + if (error) { + console.error(error.message); + process.exit(1); + } + + // Don't keep the original parent process alive process.exit(0); - }, 500); + }); } else if (platform === 'win32') { var command = _.map(process.argv, function(word) { diff --git a/lib/herostratus.js b/lib/herostratus.js index 3e2cf47e..17418349 100644 --- a/lib/herostratus.js +++ b/lib/herostratus.js @@ -35,7 +35,7 @@ app.on('ready', function() { // No menu bar Menu.setApplicationMenu(null); - elevate.require(function(error) { + elevate.require(app, function(error) { if (error) { throw error; diff --git a/package.json b/package.json index 887db92a..e89ea012 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "is-elevated": "^1.0.0", "lodash": "^3.10.1", "resin-image-write": "^2.0.5", - "sudo-prompt": "^1.1.8", + "sudo-prompt": "^2.0.2", "umount": "^1.1.1", "windosu": "^0.1.3" },