/ packages / about / lib / main.js
main.js
  1  const { CompositeDisposable } = require('atom');
  2  const semver = require('semver');
  3  const UpdateManager = require('./update-manager');
  4  const About = require('./about');
  5  const StatusBarView = require('./components/about-status-bar');
  6  let updateManager;
  7  
  8  // The local storage key for the available update version.
  9  const AvailableUpdateVersion = 'about:version-available';
 10  const AboutURI = 'atom://about';
 11  
 12  module.exports = {
 13    activate() {
 14      this.subscriptions = new CompositeDisposable();
 15  
 16      this.createModel();
 17  
 18      let availableVersion = window.localStorage.getItem(AvailableUpdateVersion);
 19      if (
 20        atom.getReleaseChannel() === 'dev' ||
 21        (availableVersion && semver.lte(availableVersion, atom.getVersion()))
 22      ) {
 23        this.clearUpdateState();
 24      }
 25  
 26      this.subscriptions.add(
 27        updateManager.onDidChange(() => {
 28          if (
 29            updateManager.getState() ===
 30            UpdateManager.State.UpdateAvailableToInstall
 31          ) {
 32            window.localStorage.setItem(
 33              AvailableUpdateVersion,
 34              updateManager.getAvailableVersion()
 35            );
 36            this.showStatusBarIfNeeded();
 37          }
 38        })
 39      );
 40  
 41      this.subscriptions.add(
 42        atom.commands.add('atom-workspace', 'about:clear-update-state', () => {
 43          this.clearUpdateState();
 44        })
 45      );
 46    },
 47  
 48    deactivate() {
 49      this.model.destroy();
 50      if (this.statusBarTile) this.statusBarTile.destroy();
 51  
 52      if (updateManager) {
 53        updateManager.dispose();
 54        updateManager = undefined;
 55      }
 56    },
 57  
 58    clearUpdateState() {
 59      window.localStorage.removeItem(AvailableUpdateVersion);
 60    },
 61  
 62    consumeStatusBar(statusBar) {
 63      this.statusBar = statusBar;
 64      this.showStatusBarIfNeeded();
 65    },
 66  
 67    deserializeAboutView(state) {
 68      if (!this.model) {
 69        this.createModel();
 70      }
 71  
 72      return this.model.deserialize(state);
 73    },
 74  
 75    createModel() {
 76      updateManager = updateManager || new UpdateManager();
 77  
 78      this.model = new About({
 79        uri: AboutURI,
 80        currentAtomVersion: atom.getVersion(),
 81        currentElectronVersion: process.versions.electron,
 82        currentChromeVersion: process.versions.chrome,
 83        currentNodeVersion: process.version,
 84        updateManager: updateManager
 85      });
 86    },
 87  
 88    isUpdateAvailable() {
 89      let availableVersion = window.localStorage.getItem(AvailableUpdateVersion);
 90      return availableVersion && semver.gt(availableVersion, atom.getVersion());
 91    },
 92  
 93    showStatusBarIfNeeded() {
 94      if (this.isUpdateAvailable() && this.statusBar) {
 95        let statusBarView = new StatusBarView();
 96  
 97        if (this.statusBarTile) {
 98          this.statusBarTile.destroy();
 99        }
100  
101        this.statusBarTile = this.statusBar.addRightTile({
102          item: statusBarView,
103          priority: -100
104        });
105  
106        return this.statusBarTile;
107      }
108    }
109  };