Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I'm updating the Electron app in the background the main window pops up for a second. #446

Closed
lipis opened this issue Sep 24, 2015 · 6 comments

Comments

@lipis
Copy link

lipis commented Sep 24, 2015

I'm using Electron and the this Grunt task to make releases. Everything works perfectly fine and the updates are happening in the background, but during that update a second application's window pops up for a brief moment and then closes. Is there any way to avoid that somehow?

@lipis
Copy link
Author

lipis commented Sep 24, 2015

Here is in short what I have at the moment in my main.js:

function executeSquirrelCommand(args, done) {
  const updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
  fs.stat(updateDotExe, function(err, stats) {
    if (err) {
      return false;
    }
    var child = cp.spawn(updateDotExe, args, {detached: true});
    child.on('close', function(code) {
      done();
    });
  });
}

function updateSelfWin() {
  executeSquirrelCommand(['--update', config.RELEASES], function() {
    // On done :)
  });
}

function handleSquirrelEvent() {
  if (process.platform !== 'win32') {
    return false;
  }

  function install(done) {
    var target = path.basename(process.execPath);
    executeSquirrelCommand(['--createShortcut', target], done);
  }

  function uninstall(done) {
    var target = path.basename(process.execPath);
    executeSquirrelCommand(['--removeShortcut', target], done);
  }

  var squirrelEvent = process.argv[1];
  switch (squirrelEvent) {
    case '--squirrel-install':
      install(app.quit);
      return true;
    case '--squirrel-updated':
      install(app.quit);
      return true;
    case '--squirrel-obsolete':
      app.quit();
      return true;
    case '--squirrel-uninstall':
      uninstall(app.quit);
      return true;
  }
  updateSelfWin();
  return false;
};

handleSquirrelEvent();

Source

@anaisbetts
Copy link
Contributor

The problem is you can't start the app until handleSquirrelEvent returns, yours concurrently runs the app

@anaisbetts
Copy link
Contributor

Here's what Slack does:

// Private: When our app is installed, Squirrel (our app install/update framework)
// invokes our executable with specific parameters, usually of the form
// '--squirrel-$EVENT $VERSION' (i.e. '--squirrel-install 0.1.0'). This is our
// chance to do custom install / uninstall actions. Once these events are handled,
// we **must** exit imediately
//
// appStart - A callback to be invoked to start the application if there are no
//            Squirrel events to handle.
//
// Returns a {Promise} whose value is a Boolean - if 'true', start the app. If
// 'false', quit immediately.
async function handleSquirrelEvents() {
  let options = process.argv.slice(1);

  if (!(options && options.length >= 1)) return true;

  let m = options[0].match(/--squirrel-([a-z]+)/);
  if (!(m && m[1])) return true;

  if (m[1] === 'firstrun') return true;

  let defaultLocations = 'Desktop,StartMenu,Startup';

  // NB: Babel currently hates switch + await, /shrug
  if (m[1] === 'install') {
    await createShortcuts(defaultLocations);
  }

  if (m[1] === 'updated') {
    await updateShortcuts(defaultLocations);
  }

  if (m[1] === 'uninstall') {
    await removeShortcuts(defaultLocations);

    let taskKill = p`${'SYSTEMROOT'}/system32/taskkill.exe`;
    let args = ['/F', '/IM', 'slack.exe', '/T'];
    await spawn(taskKill, args);
  }

  return false;
}

// Go go go go go go go
handleSquirrelEvents()
  .then((shouldRun) => {
    if (!shouldRun) {
      app.quit();
      process.exit(0);
    }

    start();
  })
  .catch((e) => {
    console.log(`Inevitable Demise! ${e.message}`);
    console.log(e.stack);

    app.quit();
    process.exit(0);
  });

@Schrims
Copy link

Schrims commented Mar 8, 2016

@lipis can you tell me what your "config.RELEASES" is?

@lipis
Copy link
Author

lipis commented Mar 8, 2016

@Schrims It's the URL of where the packages and the RELEASES file are stored..

config.RELEASES = 'https://wire-app.wire.com/win/prod/'

(https://wire-app.wire.com/win/prod/RELEASES)

@lipis
Copy link
Author

lipis commented Jul 11, 2016

I think we can close that one.. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants