Skip to content

Commit

Permalink
Added "update" action for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
KELiON committed Jan 22, 2017
1 parent 655e331 commit e1ff0e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
5 changes: 4 additions & 1 deletion app/lib/plugins/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -121,6 +121,9 @@ export default (dir) => {
console.groupEnd()
})
},
update(name) {
return this.uninstall(name).then(this.install(name))
},
/**
* Uninstall npm package
*
Expand Down
20 changes: 16 additions & 4 deletions app/main/plugins/core/cerebro/plugins/Preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 (
Expand All @@ -85,21 +89,29 @@ export default class Preview extends Component {
<KeyboardNav>
<div className={styles.header}>
{
!installed &&
!isInstalled &&
<ActionButton
action={this.pluginAction(name, 'install')}
text="Install"
loadingText="Installing"
/>
}
{
installed &&
isInstalled &&
<ActionButton
action={this.pluginAction(name, 'uninstall')}
text="Uninstall"
loadingText="Uninstalling"
/>
}
{
isUpdateAvailable &&
<ActionButton
action={this.pluginAction(name, 'update')}
text={`Update (${installedVersion}${version})`}
loadingText="Updating"
/>
}
{
match &&
<KeyboardNavItem onSelect={this.onShowDescription}>Details</KeyboardNavItem>
Expand Down
39 changes: 28 additions & 11 deletions app/main/plugins/core/cerebro/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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: () => <Preview {...plugin} installed={installed.includes(plugin.name)} />
}))
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: () => (
<Preview
{...plugin}
installedVersion={installedVersion}
isInstalled={isInstalled}
isUpdateAvailable={isUpdateAvailable}
/>
)
}
})
hide('loading')
display(results)
})
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit e1ff0e1

Please sign in to comment.