ElectronJS Installer

Set up

root folder
|---application folder
|-------package.json
|-------main.js
|-------index.html
|-------node_modules
|---exe folder
|---setup folder
|---node_modules

Install

  1. At root folder, install electron-winstaller
    npm install electron-winstaller
    
  2. At application folder, install squirrel
    npm install electron-squirrel-startup
    
  3. Open main.js, at the top, add the code
    // squirrel event handled and app will exit in 1000ms, so don't do anything else
    if (handleSquirrelEvent(app)) {return;}
    
  4. Next line of end main.js, add code
    function handleSquirrelEvent(application) {
    if (process.argv.length === 1) {
    return false;
    }
    const ChildProcess = require('child_process');
    const path = require('path');
    const appFolder = path.resolve(process.execPath, '..');
    const rootAtomFolder = path.resolve(appFolder, '..');
    const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
    const exeName = path.basename(process.execPath);
    const spawn = function(command, args) {
    let spawnedProcess, error;
    try {
    spawnedProcess = ChildProcess.spawn(command, args, {
    detached: true
    });
    } catch (error) {}
    return spawnedProcess;
    };
    const spawnUpdate = function(args) {
    return spawn(updateDotExe, args);
    };
    const squirrelEvent = process.argv[1];
    switch (squirrelEvent) {
    case '--squirrel-install':
    case '--squirrel-updated':
    // Optionally do things such as:
    // - Add your .exe to the PATH
    // - Write to the registry for things like file associations and
    //   explorer context menus
    // Install desktop and start menu shortcuts
    spawnUpdate(['--createShortcut', exeName]);
    setTimeout(application.quit, 1000);
    return true;
    case '--squirrel-uninstall':
    // Undo anything you did in the --squirrel-install and
    // --squirrel-updated handlers
    // Remove desktop and start menu shortcuts
    spawnUpdate(['--removeShortcut', exeName]);
    setTimeout(application.quit, 1000);
    return true;
    case '--squirrel-obsolete':
    // This is called on the outgoing version of your app before
    // we update to the new version - it's the opposite of
    // --squirrel-updated
    application.quit();
    return true;
    }
    };
    
  5. Back to the root folder
    electron-packager ./electron-aloha --electron-version=8.2.0 --asar --overwrite
    
  6. Root folder, create build.js, this will create installer-folder too!
    const electronInstaller = require('electron-winstaller');
    var settings = {
         appDirectory: 'electron-aloha-win32-x64',
         outputDirectory: 'aloha-installer',
         setupExe: 'aloha-installer.exe',
         noMsi: true,
         noDelta: true,
         authors: 'Phan Quang Dai',
         exe: 'electron-aloha.exe',
         description: 'fangdai.tk'
    }
    resultPromise = electronInstaller.createWindowsInstaller(settings);
    resultPromise.then(() => {
     console.log("The installers of your application were succesfully created !");
    }, (e) => {
     console.log(`Well, sometimes you are not so lucky: ${e.message}`)
    });
    
  7. Back to root folder, let’s build it!
    node build.js
    

    Ok, that’s all the instructions how to make a setup file!