-
Notifications
You must be signed in to change notification settings - Fork 22
/
inspect.ts
141 lines (115 loc) · 4.74 KB
/
inspect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as path from 'path'
import {Command, Flags, Plugin, CliUx} from '@oclif/core'
import * as chalk from 'chalk'
import * as fs from 'fs-extra'
import Plugins from '../../plugins'
import {sortBy} from '../../util'
function trimUntil(fsPath: string, part: string): string {
const parts = fsPath.split(path.sep)
const indicies = parts.reduce((a, e, i) => (e === part) ? a.concat([i]) : a, [] as number[])
const partIndex = Math.max(...indicies)
if (partIndex === -1) return fsPath
return parts.slice(0, partIndex + 1).join(path.sep)
}
export default class PluginsInspect extends Command {
static description = 'Displays installation properties of a plugin.';
static usage = 'plugins:inspect PLUGIN...';
static examples = [
'$ <%= config.bin %> plugins:inspect <%- config.pjson.oclif.examplePlugin || "myplugin" %> ',
];
static strict = false;
static args = [
{name: 'plugin', description: 'Plugin to inspect.', required: true, default: '.'},
];
static flags = {
help: Flags.help({char: 'h'}),
verbose: Flags.boolean({char: 'v'}),
};
plugins = new Plugins(this.config);
// In this case we want these operations to happen
// sequentially so the `no-await-in-loop` rule is ugnored
/* eslint-disable no-await-in-loop */
async run(): Promise<void> {
const {flags, argv} = await this.parse(PluginsInspect)
if (flags.verbose) this.plugins.verbose = true
const aliases = this.config.pjson.oclif.aliases || {}
for (let name of argv) {
if (name === '.') {
const pkgJson = JSON.parse(await fs.readFile('package.json', 'utf-8'))
name = pkgJson.name
}
if (aliases[name] === null) this.error(`${name} is blocked`)
name = aliases[name] || name
const pluginName = await this.parsePluginName(name)
try {
await this.inspect(pluginName, flags.verbose)
} catch (error) {
this.log(chalk.bold.red('failed'))
throw error
}
}
}
/* eslint-enable no-await-in-loop */
async parsePluginName(input: string): Promise<string> {
if (input.includes('@') && input.includes('/')) {
input = input.slice(1)
const [name] = input.split('@')
return '@' + name
}
const [splitName] = input.split('@')
const name = await this.plugins.maybeUnfriendlyName(splitName)
return name
}
findPlugin(pluginName: string): Plugin {
const pluginConfig = this.config.plugins.find(plg => plg.name === pluginName)
if (pluginConfig) return pluginConfig as Plugin
throw new Error(`${pluginName} not installed`)
}
async inspect(pluginName: string, verbose = false): Promise<void> {
const plugin = this.findPlugin(pluginName)
const tree = CliUx.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)
commands.forEach(cmd => tree.nodes[pluginHeader].nodes.commands.insert(cmd))
const dependencies = Object.assign({}, plugin.pjson.dependencies)
tree.nodes[pluginHeader].insert('dependencies')
const deps = sortBy(Object.keys(dependencies), d => d)
for (const dep of deps) {
// eslint-disable-next-line no-await-in-loop
const {version, pkgPath} = await this.findDep(plugin, dep)
if (!version) continue
const from = dependencies[dep] ?? null
const versionMsg = chalk.dim(from ? `${from} => ${version}` : version)
const msg = verbose ? `${dep} ${versionMsg} ${pkgPath}` : `${dep} ${versionMsg}`
tree.nodes[pluginHeader].nodes.dependencies.insert(msg)
}
tree.display()
}
async findDep(plugin: Plugin, dependency: string): Promise<{ version: string | null; pkgPath: string | null}> {
const dependencyPath = path.join(...dependency.split('/'))
let start = path.join(plugin.root, 'node_modules')
const paths = [start]
while ((start.match(/node_modules/g) || []).length > 1) {
start = trimUntil(path.dirname(start), 'node_modules')
paths.push(start)
}
for (const p of paths) {
const fullPath = path.join(p, dependencyPath)
const pkgJsonPath = path.join(fullPath, 'package.json')
try {
// eslint-disable-next-line no-await-in-loop
const pkgJson = JSON.parse(await fs.readFile(pkgJsonPath, 'utf-8'))
return {version: pkgJson.version as string, pkgPath: fullPath}
} catch {
// try the next path
}
}
return {version: null, pkgPath: null}
}
}