Skip to content

Commit

Permalink
fix: use object-treeify instead of ux (#848)
Browse files Browse the repository at this point in the history
* fix: use object-treeify instead of ux

* fix: properly construct display object
  • Loading branch information
mdonnalley committed Apr 26, 2024
1 parent 9870c54 commit 28b282b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"npm": "10.5.0",
"npm-package-arg": "^11.0.2",
"npm-run-path": "^5.3.0",
"object-treeify": "^4.0.1",
"semver": "^7.6.0",
"validate-npm-package-name": "^5.0.0",
"yarn": "^1.22.22"
Expand Down
14 changes: 9 additions & 5 deletions src/commands/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import {Command, Flags, Interfaces, Plugin, ux} from '@oclif/core'
import {Command, Flags, Interfaces, Plugin} from '@oclif/core'
import chalk from 'chalk'
// @ts-expect-error because object-treeify does not have types: https://github.com/blackflux/object-treeify/issues/1077
import treeify from 'object-treeify'

import Plugins from '../../plugins.js'
import {sortBy} from '../../util.js'

type JitPlugin = {name: string; type: string; version: string}
type PluginsJson = Array<Interfaces.Plugin | JitPlugin>
interface RecursiveTree {
[key: string]: RecursiveTree | string
}

export default class PluginsIndex extends Command {
static description = 'List installed plugins.'
Expand Down Expand Up @@ -54,10 +59,9 @@ export default class PluginsIndex extends Command {
}

private createTree(plugin: Plugin) {
const tree = ux.tree()
const tree: RecursiveTree = {}
for (const p of plugin.children) {
const name = this.formatPlugin(p)
tree.insert(name, this.createTree(p))
tree[this.formatPlugin(p)] = this.createTree(p)
}

return tree
Expand All @@ -68,7 +72,7 @@ export default class PluginsIndex extends Command {
this.log(this.formatPlugin(plugin))
if (plugin.children && plugin.children.length > 0) {
const tree = this.createTree(plugin)
tree.display()
this.log(treeify(tree))
}
}
}
Expand Down
42 changes: 20 additions & 22 deletions src/commands/plugins/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Args, Command, Flags, Plugin, ux} from '@oclif/core'
import {Args, Command, Flags, Plugin} from '@oclif/core'
import chalk from 'chalk'
import {readFile} from 'node:fs/promises'
import {dirname, join, sep} from 'node:path'
// @ts-expect-error because object-treeify does not have types: https://github.com/blackflux/object-treeify/issues/1077
import treeify from 'object-treeify'

import {determineLogLevel} from '../../log-level.js'
import Plugins from '../../plugins.js'
Expand All @@ -19,7 +21,7 @@ function trimUntil(fsPath: string, part: string): string {
return parts.slice(0, partIndex + 1).join(sep)
}

type Dependencies = Record<string, {from: string; version: string}>
type Dependencies = Record<string, {from: string | undefined; version: string}>
type PluginWithDeps = Omit<
Plugin,
| '_commandsDir'
Expand Down Expand Up @@ -103,37 +105,33 @@ export default class PluginsInspect extends Command {

async inspect(pluginName: string, verbose = false): Promise<PluginWithDeps> {
const plugin = this.findPlugin(pluginName)
const tree = ux.tree()
const pluginHeader = chalk.bold.cyan(plugin.name)
tree.insert(pluginHeader)
tree.nodes[pluginHeader].insert(`version ${plugin.version}`)
if (plugin.tag) tree.nodes[pluginHeader].insert(`tag ${plugin.tag}`)
if (plugin.pjson.homepage) tree.nodes[pluginHeader].insert(`homepage ${plugin.pjson.homepage}`)
tree.nodes[pluginHeader].insert(`location ${plugin.root}`)

tree.nodes[pluginHeader].insert('commands')
const commands = sortBy(plugin.commandIDs, (c) => c)
for (const cmd of commands) tree.nodes[pluginHeader].nodes.commands.insert(cmd)

const dependencies = {...plugin.pjson.dependencies}

tree.nodes[pluginHeader].insert('dependencies')
const deps = sortBy(Object.keys(dependencies), (d) => d)
const dependencies: Record<string, null> = {}
const depsJson: Dependencies = {}
for (const dep of deps) {
for (const dep of sortBy(Object.keys({...plugin.pjson.dependencies}), (d) => d)) {
// eslint-disable-next-line no-await-in-loop
const {pkgPath, version} = await this.findDep(plugin, dep)
if (!version) continue

const from = dependencies[dep] ?? null
const from = plugin.pjson.dependencies?.[dep]
const versionMsg = chalk.dim(from ? `${from} => ${version}` : version)
const msg = verbose ? `${dep} ${versionMsg} ${pkgPath}` : `${dep} ${versionMsg}`

tree.nodes[pluginHeader].nodes.dependencies.insert(msg)
dependencies[msg] = null
depsJson[dep] = {from, version}
}

if (!this.jsonEnabled()) tree.display()
const tree = {
[chalk.bold.cyan(plugin.name)]: {
[`version ${plugin.version}`]: null,
...(plugin.tag ? {[`tag ${plugin.tag}`]: null} : {}),
...(plugin.pjson.homepage ? {[`homepage ${plugin.pjson.homepage}`]: null} : {}),
[`location ${plugin.root}`]: null,
commands: Object.fromEntries(sortBy(plugin.commandIDs, (c) => c).map((id) => [id, null])),
dependencies,
},
}

this.log(treeify(tree))

return {...plugin, deps: depsJson}
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5657,6 +5657,11 @@ object-treeify@^1.1.33:
resolved "https://registry.npmjs.org/object-treeify/-/object-treeify-1.1.33.tgz"
integrity sha512-EFVjAYfzWqWsBMRHPMAXLCDIJnpMhdWAqR7xG6M6a2cs6PMFpl/+Z20w9zDW4vkxOFfddegBKq9Rehd0bxWE7A==

object-treeify@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/object-treeify/-/object-treeify-4.0.1.tgz#f91a7dec795d8275886e7f1bd78408f6975be825"
integrity sha512-Y6tg5rHfsefSkfKujv2SwHulInROy/rCL5F4w0QOWxut8AnxYxf0YmNhTh95Zfyxpsudo66uqkux0ACFnyMSgQ==

object.assign@^4.1.5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/object.assign/-/object.assign-4.1.5.tgz#3a833f9ab7fdb80fc9e8d2300c803d216d8fdbb0"
Expand Down

0 comments on commit 28b282b

Please sign in to comment.