Reputation: 183
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).
Upvotes: 18
Views: 6255
Reputation: 51
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.
Upvotes: 0
Reputation: 46
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.
Upvotes: 0
Reputation: 5322
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)
Upvotes: 14
Reputation: 4065
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!
Upvotes: 2