From e1ff0e1a02f7bbfe64bffa4cb820c9d0910e274e Mon Sep 17 00:00:00 2001 From: Alexandr Subbotin Date: Sun, 22 Jan 2017 19:10:55 +0100 Subject: [PATCH] Added "update" action for plugins --- app/lib/plugins/npm.js | 5 ++- .../core/cerebro/plugins/Preview/index.js | 20 ++++++++-- .../plugins/core/cerebro/plugins/index.js | 39 +++++++++++++------ app/package.json | 1 + 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/app/lib/plugins/npm.js b/app/lib/plugins/npm.js index dc1e243c..eb71c593 100644 --- a/app/lib/plugins/npm.js +++ b/app/lib/plugins/npm.js @@ -110,7 +110,7 @@ export default (dir) => { }) .then(() => { const json = getConfig() - json.dependencies[name] = `^${versionToInstall}` + json.dependencies[name] = versionToInstall console.log('Add package to dependencies') setConfig(json) console.groupEnd() @@ -121,6 +121,9 @@ export default (dir) => { console.groupEnd() }) }, + update(name) { + return this.uninstall(name).then(this.install(name)) + }, /** * Uninstall npm package * diff --git a/app/main/plugins/core/cerebro/plugins/Preview/index.js b/app/main/plugins/core/cerebro/plugins/Preview/index.js index 5d7a84e7..cce7a1bb 100644 --- a/app/main/plugins/core/cerebro/plugins/Preview/index.js +++ b/app/main/plugins/core/cerebro/plugins/Preview/index.js @@ -23,7 +23,9 @@ export default class Preview extends Component { version: PropTypes.string.isRequired, description: PropTypes.string, repo: PropTypes.string, - installed: PropTypes.bool.isRequired, + installedVersion: PropTypes.string, + isInstalled: PropTypes.bool.isRequired, + isUpdateAvailable: PropTypes.bool.isRequired, } constructor(props) { @@ -75,7 +77,9 @@ export default class Preview extends Component { version, description, repo, - installed + isInstalled, + installedVersion, + isUpdateAvailable } = this.props const match = repo.match(/^.+github.com\/([^\/]+\/[^\/]+).*?/) return ( @@ -85,7 +89,7 @@ export default class Preview extends Component {
{ - !installed && + !isInstalled && } { - installed && + isInstalled && } + { + isUpdateAvailable && + + } { match && Details diff --git a/app/main/plugins/core/cerebro/plugins/index.js b/app/main/plugins/core/cerebro/plugins/index.js index 2375952f..7cccc6b3 100644 --- a/app/main/plugins/core/cerebro/plugins/index.js +++ b/app/main/plugins/core/cerebro/plugins/index.js @@ -4,14 +4,17 @@ import { search, memoize } from 'cerebro-tools' import availablePlugins from './getAvailablePlugins' import installedPlugins from './getInstalledPlugins' import icon from '../icon.png' +import semver from 'semver' const getAvailablePlugins = memoize(availablePlugins) -const getInstalledPlugins = memoize(() => ( - installedPlugins().then(plugins => Object.keys(plugins)) -)) +const getInstalledPlugins = memoize(installedPlugins) const toString = ({ name, description }) => [name, description].join(' ') +const parseVersion = (version) => ( + semver.valid((version || '').replace(/^\^/, '')) || '0.0.0' +) + const fn = ({ term, display, hide, actions }) => { const match = term.match(/^plugins?\s*(.+)?$/i) if (match) { @@ -26,14 +29,28 @@ const fn = ({ term, display, hide, actions }) => { getInstalledPlugins() ]).then(plugins => { const [available, installed] = plugins - let results = search(available, pluginSearch, toString) - results = results.map(plugin => ({ - icon, - title: `${plugin.name} (${plugin.version})`, - subtitle: plugin.description, - onSelect: () => actions.open(plugin.repo), - getPreview: () => - })) + const results = search(available, pluginSearch, toString).map(plugin => { + const installedVersion = parseVersion(installed[plugin.name]) + const isInstalled = !!installed[plugin.name] + const isUpdateAvailable = isInstalled && semver.gt(plugin.version, installedVersion) + const displayVersion = isUpdateAvailable ? + `${installedVersion} → ${plugin.version}` : + plugin.version + return { + icon, + title: `${plugin.name} (${displayVersion})`, + subtitle: plugin.description, + onSelect: () => actions.open(plugin.repo), + getPreview: () => ( + + ) + } + }) hide('loading') display(results) }) diff --git a/app/package.json b/app/package.json index c2c510be..404640ec 100644 --- a/app/package.json +++ b/app/package.json @@ -16,6 +16,7 @@ "dependencies": { "nodobjc": "^2.1.0", "rmdir": "^1.2.0", + "semver": "^5.3.0", "tar.gz": "^1.0.5", "universal-analytics": "^0.4.8" },