-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathinspect.ts
165 lines (139 loc) · 4.98 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as path from 'path'
import {Command, flags} from '@oclif/command'
import {Plugin} from '@oclif/config'
import * as chalk from 'chalk'
import {exec} from 'child_process'
import * as fs from 'fs-extra'
import Plugins from '../../plugins'
import {sortBy} from '../../util'
const TAB = ' '
type Dependencies = {
[key: string]: unknown;
from?: string;
version?: string;
name?: string;
dependencies: {
[key: string]: 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 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);
allDeps: Dependencies = {dependencies: {}};
// 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() {
this.allDeps = await this.npmList(this.config.root, 3)
const {flags, argv} = 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)
} 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) {
const plugin = this.findPlugin(pluginName)
this.log(chalk.bold.cyan(plugin.name))
this.log(`${TAB}version: ${plugin.version}`)
if (plugin.tag) this.log(`${TAB}tag: ${plugin.tag}`)
if (plugin.pjson.homepage) this.log(`${TAB}homepage: ${plugin.pjson.homepage}`)
this.log(`${TAB}location: ${plugin.root}`)
this.log(`${TAB}commands:`)
const commands = sortBy(plugin.commandIDs, c => c)
commands.forEach(cmd => this.log(`${TAB.repeat(2)}${cmd}`))
const dependencies = plugin.root.includes(this.config.root) ?
this.findDepInTree(plugin).dependencies :
(await this.npmList(plugin.root)).dependencies
this.log(`${TAB}dependencies:`)
const deps = sortBy(Object.keys(dependencies), d => d)
for (const dep of deps) {
// eslint-disable-next-line no-await-in-loop
const version = dependencies[dep].version || await this.findDepInSharedModules(plugin, dep)
const from = dependencies[dep].from ?
dependencies[dep].from!.split('@').reverse()[0] :
null
if (from) this.log(`${TAB.repeat(2)}${dep}: ${from} => ${version}`)
else this.log(`${TAB.repeat(2)}${dep}: ${version}`)
}
}
async findDepInSharedModules(plugin: Plugin, dependency: string): Promise<string> {
const sharedModulePath = path.join(plugin.root.replace(plugin.name, ''), ...dependency.split('/'), 'package.json')
const pkgJson = JSON.parse(await fs.readFile(sharedModulePath, 'utf-8'))
return pkgJson.version as string
}
findDepInTree(plugin: Plugin): Dependencies {
if (plugin.name === this.allDeps.name) return this.allDeps
const plugins = [plugin.name]
let p = plugin
while (p.parent) {
plugins.push(p.parent.name)
p = p.parent
}
let dependencies = this.allDeps
for (const plg of plugins.reverse()) {
dependencies = dependencies.dependencies[plg]
}
return dependencies
}
async npmList(cwd: string, depth = 0): Promise<Dependencies> {
return new Promise((resolve, reject) => {
exec(`npm list --json --depth ${depth}`, {
cwd,
encoding: 'utf-8',
maxBuffer: 2048 * 2048,
}, (error, stdout) => {
if (error) {
try {
const parsed = JSON.parse(stdout)
if (parsed) resolve(parsed)
} catch {
reject(new Error(`Could not get dependencies for ${cwd}`))
}
} else {
resolve(JSON.parse(stdout))
}
})
})
}
}