-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathinspect.ts
More file actions
162 lines (132 loc) · 5.45 KB
/
Copy pathinspect.ts
File metadata and controls
162 lines (132 loc) · 5.45 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import * as path from 'path'
import {Args, Command, Flags, Plugin, ux} from '@oclif/core'
import * as chalk from 'chalk'
import {readFile} from 'fs/promises'
import Plugins from '../../plugins'
import {sortBy} from '../../util'
function trimUntil(fsPath: string, part: string): string {
const parts = fsPath.split(path.sep)
const indices = parts.reduce((a, e, i) => (e === part) ? a.concat([i]) : a, [] as number[])
const partIndex = Math.max(...indices)
if (partIndex === -1) return fsPath
return parts.slice(0, partIndex + 1).join(path.sep)
}
type Dependencies = Record<string, {from: string, version: string}>
type PluginWithDeps = Plugin & {deps: Dependencies}
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 enableJsonFlag = true;
static args = {
plugin: Args.string({
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 ignored
/* eslint-disable no-await-in-loop */
async run(): Promise<PluginWithDeps[]> {
const {flags, argv} = await this.parse(PluginsInspect)
if (flags.verbose) this.plugins.verbose = true
const aliases = this.config.pjson.oclif.aliases || {}
const plugins: PluginWithDeps[] = []
for (let name of argv as string[]) {
if (name === '.') {
const pkgJson = JSON.parse(await 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 {
plugins.push(await this.inspect(pluginName, flags.verbose))
} catch (error) {
this.log(chalk.bold.red('failed'))
throw error
}
}
return plugins
}
/* 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.getPluginsList().find(plg => plg.name === pluginName)
if (pluginConfig) return pluginConfig as Plugin
if (this.config.pjson.oclif.jitPlugins?.[pluginName]) {
this.warn(`Plugin ${pluginName} is a JIT plugin. It will be installed the first time you run one of it's commands.`)
}
throw new Error(`${pluginName} not installed`)
}
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)
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)
const depsJson: Dependencies = {}
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)
depsJson[dep] = {from, version}
}
if (!this.jsonEnabled()) tree.display()
return {...plugin, deps: depsJson} as PluginWithDeps
}
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)
}
// TODO: use promise.any to check the paths in parallel
// requires node >= 16
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 readFile(pkgJsonPath, 'utf-8'))
return {version: pkgJson.version as string, pkgPath: fullPath}
} catch {
// try the next path
}
}
return {version: null, pkgPath: null}
}
}