nwjs version update node WebKit updater scheme

 

 

Remember to follow these steps strictly

1. Execute the command in the project engineering directory

npm install node-webkit-updater

 

2. Edit your startup html file, such as app / index html file, add the following code

(1) Update script code

Note that the following code is prone to problems. Due to the problem of window permission, the default location of downloading the installation package may not have write permission, so the directory should be changed. As for the location to be changed, the code can be implemented by itself. Remember, don't use the default, be sure to change it manually.

var upd = new updater(pkg, {
    temporaryDirectory: 'C:\\Users\\Administrator',
});

 

var gui = require('nw.gui');
var pkg = require('../package.json'); // Insert your app's manifest here
var updater = require('node-webkit-updater');
var upd = new updater(pkg, {
	temporaryDirectory: 'C:\\Users\\Administrator',
});
var copyPath, execPath;

// Args passed when new app is launched from temp dir during update
if(gui.App.argv.length) {
    // ------------- Step 5 -------------
    copyPath = gui.App.argv[0];
    execPath = gui.App.argv[1];

    // Replace old app, Run updated app from original location and close temp instance
    upd.install(copyPath, function(err) {
        if(!err) {

            // ------------- Step 6 -------------
            upd.run(execPath, null);
            gui.App.quit();
        }
    });
}
else { // if no arguments were passed to the app

    // ------------- Step 1 -------------
    upd.checkNewVersion(function(error, newVersionExists, manifest) {
		console.log("checkNewVersion");
		console.log(error);
		console.log(newVersionExists);
		console.log(manifest);
        if (!error && newVersionExists) {

            // ------------- Step 2 -------------
            upd.download(function(error, filename) {
						console.log("checkdownload");
						console.log(error);
						console.log(filename);
                if (!error) {

                    // ------------- Step 3 -------------
                    upd.unpack(filename, function(error, newAppPath) {
                        if (!error) {

                            // ------------- Step 4 -------------
                            upd.runInstaller(newAppPath, [upd.getAppPath(), upd.getAppExec()],{});
                            gui.App.quit();
                        }
                    }, manifest);
                }
            }, manifest);
        }
    });
}

 (2)index.html complete example

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>

		<style type="text/css">
		</style>

		<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
		<script>
		/*
 1. Check the manifest for version (from your running "old" app).
 2. If the version is different from the running one, download new package to a temp directory.
 3. Unpack the package in temp.
 4. Run new app from temp and kill the old one (i.e. still all from the running app).
 5. The new app (in temp) will copy itself to the original folder, overwriting the old app.
 6. The new app will run itself from original folder and exit the process.
*/

var gui = require('nw.gui');
var pkg = require('../package.json'); // Insert your app's manifest here
var updater = require('node-webkit-updater');
var upd = new updater(pkg, {
	temporaryDirectory: 'C:\\Users\\Administrator',
});
var copyPath, execPath;

// Args passed when new app is launched from temp dir during update
if(gui.App.argv.length) {
    // ------------- Step 5 -------------
    copyPath = gui.App.argv[0];
    execPath = gui.App.argv[1];

    // Replace old app, Run updated app from original location and close temp instance
    upd.install(copyPath, function(err) {
        if(!err) {

            // ------------- Step 6 -------------
            upd.run(execPath, null);
            gui.App.quit();
        }
    });
}
else { // if no arguments were passed to the app

    // ------------- Step 1 -------------
    upd.checkNewVersion(function(error, newVersionExists, manifest) {
		console.log("checkNewVersion");
		console.log(error);
		console.log(newVersionExists);
		console.log(manifest);
        if (!error && newVersionExists) {

            // ------------- Step 2 -------------
            upd.download(function(error, filename) {
						console.log("checkdownload");
						console.log(error);
						console.log(filename);
                if (!error) {

                    // ------------- Step 3 -------------
                    upd.unpack(filename, function(error, newAppPath) {
                        if (!error) {

                            // ------------- Step 4 -------------
                            upd.runInstaller(newAppPath, [upd.getAppPath(), upd.getAppExec()],{});
                            gui.App.quit();
                        }
                    }, manifest);
                }
            }, manifest);
        }
    });
}
		</script>


		<script>
			
		</script>
	</head>
	<body>
		<button type="button">Refresh this page 10.0.0aaaaaaaaaa</button>
		<button type="button">open windows</button>
		<button type="button" resize window</button>
		<button type="button" >close window</button>
		<button type="button">display Cookie</button>
		<button type="button">display showID</button>=
	</body>
</html>

3. Suppose the latest version is 12.0.0 and the old version is 10.0.0

4. Assuming that the current project is the latest version, edit the package in the project The version field in JSON is the latest version number {12.0.0,

manifestUrl is the file address for checking the version update, which is created in step 4 below.

5. Create the version configuration file content corresponding to the manifestUrl field in step 4. The package The JSON file is as follows:,

Version: the latest version number

url: the download address of the compressed package of the corresponding platform

execPath: the url corresponds to the startup file in the compressed package (Note: please change to your program's own startup exe name)

 

App corresponding to url The compressed package corresponding to zip is made as follows:

(1) Very simply, the latest version of the current project, that is, the latest project configured in steps 1, 2 and 4, should be directly compressed into zip (it's not always important to learn, don't look at what is said elsewhere on the Internet, just as I said)

(2) Note that there is only one layer of directory in the compressed package. Don't come to the second layer. Double click the compressed package, and the direct effect is shown in the following figure (very important)

(3) Upload the compressed package to the network location shown in "url: the download address of the compressed package of the corresponding platform"

(4) Put the package created in this step JSON (note that it is either in the project or created in this step) files are uploaded to the package. JSP file in the project JSON (note that this is the package.json file in the project, that is, the file in step 4), in the network file shown in manifestUrl, that is, as shown in the following figure

6. Great achievements. Of course, the old version is also such a packaging process. You can try running the old version.

Keywords: node.js

Added by agent007 on Wed, 02 Feb 2022 02:34:19 +0200