How to detect that the Electron app is running for the first time?
Asked Answered
V

4

18

I'm developing an App using the latest version of Electron-builder (using AutoUpadate).

Is there any way to know that the App is running for the first time after installation?

Ps: I have tried using electron-config but the user data files are not deleted after uninstall, and I needed to do some things with every installation (even if it's on the same machine).

Vani answered 30/4, 2017 at 21:53 Comment(0)
G
14

Check for the squirrel-firstrun flag:

var cmd = process.argv[1];

if (cmd == '--squirrel-firstrun') {
    // Running for the first time.
}

(you don't need to install anything for this to work)

Ref.

Graeme answered 19/5, 2017 at 14:8 Comment(3)
Yes i use it in my electron appGraeme
This seems like a Windows-only thing. Does this approach work under macOS and Linux?Onanism
@Onanism I never tried it on macOS or Linux but there's a good chance this is only for Windows sadly.Graeme
G
2

Your app could, in case that it doesn't exist, write a folder/file (with filename based on a timestamp?).

When app starts, always look for the file. If there is no file, means its first time, do what you want and write file.

If there is a file means it's not the first time.


Don't know if there is something at Electron API!

Gobi answered 12/5, 2017 at 9:55 Comment(2)
But if I uninstall the App, those file will not be delete, so, If the user reinstall the App, i will get a false check. (cause file will exists)Vani
I don't know, but you could install an app, check its folder structure, then uninstall and recheck the folder structure. Look which folder are missing! Then in such a folder write your file! Apply changes and check..Hope it helps!Gobi
B
0

Not recommended but working solution.

var fs = require('fs');
function checkForNewMacInstall( {
    //Need to get out from app.asar
    const filename = path.join(__dirname, '../../../installinfo.json');
    if (fs.existsSync(filename))
        return false;

    fs.writeFileSync(filename, JSON.stringify({first_launch: new Date().toUTCString()}));
    return true;
}

On MacOS and Windows NSIS installs this file will be removed on uninstall and reinstall but MSI install is not removing that file so you can check only the first install.

Bethea answered 15/9, 2023 at 7:25 Comment(0)
E
0

I believe the solution @oikonomopo pointed out would work since when you uninstall the app the whole exe folder gets wiped including the files you wrote after install. Although if the user using the app does not have proper write permission to Program files they could be unable to write to the app's exe folder. In this case save data to app.getPath('appData')/app.getPath('userData') and compare timestamps of files in exe and appData are within a few minutes otherwise wipe it and create it new.

When you install a Electron app on windows it goes into C:\Users\{username}\AppData\Local\Programs\{appname} so you would write to that path in Electron by using their built in function app.getPath('exe'). This will get you the full exe/DMG path of the app. On Windows it would be C:\Users\{username}\AppData\Local\Programs\{appname}\{appname}.exe. Then to write to that path you'd have to trim the exe/DMG bit to get the directory and you can use path npm package to do that like so:

getBasePath(): string {
  let basePath = app.getPath('exe');
  if (basePath.match(/node_modules/)) {
    basePath = __dirname;
  } else {
    if (basePath.match(/MacOS/)) {
      basePath = join(basePath, '..', '..');
    } else {
      basePath = join(basePath, '..');
    }
  }

  return basePath;
}

The above function is accounting for local serve of the application and for MacOs where you have to back up another directory. This is what I use and it works like a charm. Again if there are permission issues you have to write files to app.getPath('appData')/app.getPath('userData') and compare timestamps with exe files to know if it is the first run.

Electromechanical answered 25/9, 2023 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.