Skip to content

Commit

Permalink
fix: display nested plugins as a tree (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Jan 2, 2019
1 parent 70c3578 commit fca242f
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 167 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@oclif/color": "^0.0.0",
"@oclif/command": "^1.5.4",
"chalk": "^2.4.1",
"cli-ux": "^4.9.3",
"cli-ux": "^5.0.0",
"debug": "^4.1.0",
"fs-extra": "^7.0.1",
"http-call": "^5.2.2",
Expand All @@ -19,7 +19,7 @@
"yarn": "^1.12.3"
},
"devDependencies": {
"@oclif/config": "^1.9.0",
"@oclif/config": "^1.10.4",
"@oclif/dev-cli": "^1.19.4",
"@oclif/errors": "^1.2.2",
"@oclif/plugin-help": "^2.1.4",
Expand Down
38 changes: 32 additions & 6 deletions src/commands/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import color from '@oclif/color'
import {Command, flags} from '@oclif/command'
import {Plugin} from '@oclif/config'
import {cli} from 'cli-ux'

import Plugins from '../../plugins'
import {sortBy} from '../../util'
Expand All @@ -24,12 +26,36 @@ export default class PluginsIndex extends Command {
this.log('no plugins installed')
return
}
for (let plugin of plugins) {
let output = `${this.plugins.friendlyName(plugin.name)} ${color.dim(plugin.version)}`
if (plugin.type !== 'user') output += color.dim(` (${plugin.type})`)
if (plugin.type === 'link') output += ` ${plugin.root}`
else if (plugin.tag && plugin.tag !== 'latest') output += color.dim(` (${String(plugin.tag)})`)
this.log(output)
this.display(plugins as Plugin[])
}

private display(plugins: Plugin[]) {
for (let plugin of plugins.filter((p: Plugin) => !p.parent)) {
this.log(this.formatPlugin(plugin))
if (plugin.children.length) {
let tree = this.createTree(plugin)
tree.display(this.log)
}
}
}

private createTree(plugin: Plugin) {
let tree = cli.tree()
for (let p of plugin.children) {
const name = this.formatPlugin(p)
tree.insert(name, this.createTree(p))
}
return tree
}

private formatPlugin(plugin: any): string {
let output = `${this.plugins.friendlyName(plugin.name)} ${color.dim(plugin.version)}`
if (plugin.type !== 'user')
output += color.dim(` (${plugin.type})`)
if (plugin.type === 'link')
output += ` ${plugin.root}`
else if (plugin.tag && plugin.tag !== 'latest')
output += color.dim(` (${String(plugin.tag)})`)
return output
}
}
5 changes: 5 additions & 0 deletions src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Command, flags} from '@oclif/command'
import {Plugin} from '@oclif/config'
import cli from 'cli-ux'

import Plugins from '../../plugins'
Expand Down Expand Up @@ -30,6 +31,10 @@ export default class PluginsUninstall extends Command {
cli.action.start(`Uninstalling ${friendly}`)
const unfriendly = await this.plugins.hasPlugin(plugin)
if (!unfriendly) {
let p = this.config.plugins.find(p => p.name === plugin) as Plugin | undefined
if (p) {
if (p && p.parent) return this.error(`${friendly} is installed via plugin ${p.parent!.name}, uninstall ${p.parent!.name} instead`)
}
return this.error(`${friendly} is not installed`)
}
await this.plugins.uninstall(unfriendly.name)
Expand Down
Loading

0 comments on commit fca242f

Please sign in to comment.