dsheiko / nw-autoupdater

⛔️ [DEPRECATED] Library provides low-level API to control NW.js app auto-updates
MIT License
75 stars 36 forks source link

Example on pure js ? #8

Closed Arti3DPlayer closed 7 years ago

Arti3DPlayer commented 7 years ago

Please provide pure javascript example... Is this possible ?

dsheiko commented 7 years ago

But I do not use any library or framework. It's pure / Vanilla JavaScript. Async/await functions are part of JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

But if you peffer Promises, that is supposed to work either:

<!DOCTYPE html>
<html>
  <head>
    <title>Autoupdater Example (Script Strategy)</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <pre id="output"></pre>
    <script>
      const AutoUpdater = require( "nw-autoupdater" ),
            updater = new AutoUpdater( require( "./package.json" ), {
              strategy: "ScriptSwap"
            }),
            output =  document.querySelector( "#output" );

      output.innerHTML = `Application ver. ${nw.App.manifest.version}\n`;

      // Download/unpack update if any available
      updater.readRemoteManifest().then(( rManifest ) => {
        updater.checkNewVersion( rManifest ).then(( needsUpdate ) => {
          if ( !needsUpdate ) {
            output.innerHTML += `\nApp is up to date...`;
            return;
          }
          if ( !confirm( "New release is available. Do you want to upgrade?" ) ) {
            return;
          }
        });

         updater.download( rManifest ).then(( updateFile ) => {
           updater.unpack( updateFile ).then(() => {
             alert( `The application will automatically restart to finish installing the update` );
             updater.restartToSwap();
           });
         });

      }); 

      // Subscribe for progress events
      updater.on( "download", ( downloadSize, totalSize ) => {
        output.innerHTML = `Downloading...`;
        console.log( "download progress", Math.floor( downloadSize / totalSize * 100 ), "%" );
      });
      updater.on( "install", ( installFiles, totalFiles ) => {
        output.innerHTML = `Installing...\n`;
        console.log( "install progress", Math.floor( installFiles / totalFiles * 100 ), "%" );
      });

    </script>
  </body>
</html>
Arti3DPlayer commented 7 years ago

cool, thanks. Will try your updater in our app:) Seems I'm not keeping pace with progress