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

[QUESTION] How to create windows installer with auto-updating #1339

Closed
ghost opened this issue Dec 13, 2017 · 2 comments
Closed

[QUESTION] How to create windows installer with auto-updating #1339

ghost opened this issue Dec 13, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 13, 2017

Has anyone an example of how I can package a windows installer with auto-updater? If I run package-win I get a win unpacked but have no idea where to go from there. Most electron-builder examples are for basic index.html and main.js projects which I can’t make work here. Any help greatly appreciated!

I realise that my question is more in relation to the general case of the title, however this project has some unique considerations due to its complexity and my experience level

@amilajack
Copy link
Member

amilajack commented Dec 13, 2017

Have you seen #1289?
This also seems to be a good tutorial

@Kronenberg
Copy link

Kronenberg commented Dec 27, 2017

This code I am using in my app

import { app, ipcMain, BrowserWindow, dialog } from 'electron';
import { autoUpdater } from 'electron-updater';
import jetpack from 'fs-jetpack';

const installDir = jetpack.cwd(app.getAppPath());
const userDataDir = jetpack.cwd(app.getPath('userData'));
const updateStoreFile = 'update.json';
let checkForUpdatesEvent;

autoUpdater.autoDownload = false;

let updateFile = {};
try {
    const installUpdateFile = installDir.read(updateStoreFile, 'json');
    const userUpdateFile = userDataDir.read(updateStoreFile, 'json');
    updateFile = Object.assign({}, installUpdateFile, userUpdateFile);
} catch (err) {
    console.log(err);
}

function updateDownloaded () {
    dialog.showMessageBox({
        title: 'Update Ready to Install',
        message: 'Update has been downloaded',
        buttons: [
            'Install Later',
            'Install Now'
        ],
        defaultId: 1
    }, (response) => {
        if (response === 0) {
            dialog.showMessageBox({
                title: 'Installing Later',
                message: 'Update will be installed when you exit the app'
            });
        } else {
            autoUpdater.quitAndInstall();
            setTimeout(() => app.quit(), 1000);
        }
    });
}

function updateNotAvailable () {
    if (checkForUpdatesEvent) {
        checkForUpdatesEvent.sender.send('update-result', false);
        checkForUpdatesEvent = null;
    }
}

function updateAvailable ({version}) {
    if (checkForUpdatesEvent) {
        checkForUpdatesEvent.sender.send('update-result', true);
        checkForUpdatesEvent = null;
    } else if (updateFile.skip === version) {
        console.log(`Skipping version: ${version}`);
        return;
    }

    let window = new BrowserWindow({
        title: 'Update Available',
        width: 600,
        height: 330,
        show : false,
        center: true,
        resizable: false,
        maximizable: false,
        minimizable: false
    });

    window.loadURL(`file://${__dirname}/updater.html`);
    window.setMenuBarVisibility(false);

    window.webContents.on('did-finish-load', () => {
        window.webContents.send('new-version', version);
        window.show();
    });

    ipcMain.once('update-response', (e, type) => {
        switch (type) {
            case 'skip':
                updateFile.skip = version;
                userDataDir.write(updateStoreFile, updateFile, { atomic: true });
                dialog.showMessageBox({
                    title: 'Skip Update',
                    message: 'We will notify you when the next update is available\n' +
                        'If you change your mind you can check for updates from the About menu.'
                }, () => window.close());
                break;
            case 'remind':
                dialog.showMessageBox({
                    title: 'Remind Later',
                    message: 'We will remind you next time you start the app'
                }, () => window.close());
                break;
            case 'update':
                dialog.showMessageBox({
                    title: 'Downloading Update',
                    message: 'You will be notified when the update is ready to be installed'
                }, () => window.close());
                autoUpdater.downloadUpdate();
                break;
        }
    });

    window.on('closed', () => {
        window = null;
        ipcMain.removeAllListeners('update-response');
    });
}

function checkForUpdates () {
    autoUpdater.on('update-available', updateAvailable);
    autoUpdater.on('update-not-available', updateNotAvailable);

    autoUpdater.on('download-progress', ({percent}) => {
        console.log(`Update progress: ${percent}`);
    });

    autoUpdater.on('update-downloaded', updateDownloaded);

    // Event from about window
    ipcMain.on('check-for-updates', (e, autoUpdate) => {
        if (autoUpdate === true || autoUpdate === false) {
            updateFile.autoUpdate = autoUpdate;
            userDataDir.write(updateStoreFile, updateFile, { atomic: true });
        } else if (autoUpdate === 'auto') {
            e.returnValue = updateFile.autoUpdate !== false;
        } else {
            checkForUpdatesEvent = e;
            autoUpdater.checkForUpdates();
        }
    });
    autoUpdater.setFeedURL({
      owner: 'blablaApp',
      provider: 'generic',
      url: 'http://'+'localhost:3000', // remote server with new dmg file and YML config
    });
    if (updateFile.autoUpdate !== false) {
        autoUpdater.checkForUpdates();
    }
}

export {
    checkForUpdates
};

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

No branches or pull requests

2 participants