-
Notifications
You must be signed in to change notification settings - Fork 22
/
install.ts
213 lines (177 loc) · 7.27 KB
/
install.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* eslint-disable no-await-in-loop */
import {Args, Command, Errors, Flags, Interfaces, Plugin, ux} from '@oclif/core'
import {bold, cyan} from 'ansis'
import validate from 'validate-npm-package-name'
import {determineLogLevel} from '../../log-level.js'
import Plugins from '../../plugins.js'
export default class PluginsInstall extends Command {
static aliases = ['plugins:add']
static args = {
plugin: Args.string({description: 'Plugin to install.', required: true}),
}
static description = `Uses npm to install plugins.
Installation of a user-installed plugin will override a core plugin.
Use the <%= config.scopedEnvVarKey('NPM_LOG_LEVEL') %> environment variable to set the npm loglevel.
Use the <%= config.scopedEnvVarKey('NPM_REGISTRY') %> environment variable to set the npm registry.`
public static enableJsonFlag = true
static examples = [
{
command: '<%= config.bin %> <%= command.id %> <%- config.pjson.oclif.examplePlugin || "myplugin" %> ',
description: 'Install a plugin from npm registry.',
},
{
command: '<%= config.bin %> <%= command.id %> https://github.com/someuser/someplugin',
description: 'Install a plugin from a github url.',
},
{
command: '<%= config.bin %> <%= command.id %> someuser/someplugin',
description: 'Install a plugin from a github slug.',
},
]
static flags = {
force: Flags.boolean({
char: 'f',
description: 'Force npm to fetch remote resources even if a local copy exists on disk.',
}),
help: Flags.help({char: 'h'}),
jit: Flags.boolean({
hidden: true,
async parse(input, ctx) {
if (input === false || input === undefined) return input
const requestedPlugins = ctx.argv.filter((a) => !a.startsWith('-'))
if (requestedPlugins.length === 0) return input
const jitPluginsConfig = ctx.config.pjson.oclif.jitPlugins ?? {}
if (Object.keys(jitPluginsConfig).length === 0) return input
const plugins = new Plugins({config: ctx.config})
const nonJitPlugins = await Promise.all(
requestedPlugins.map(async (plugin) => {
const name = await plugins.maybeUnfriendlyName(plugin)
return {jit: Boolean(jitPluginsConfig[name]), name}
}),
)
const nonJitPluginsNames = nonJitPlugins.filter((p) => !p.jit).map((p) => p.name)
if (nonJitPluginsNames.length > 0) {
throw new Errors.CLIError(`The following plugins are not JIT plugins: ${nonJitPluginsNames.join(', ')}`)
}
return input
},
}),
silent: Flags.boolean({
char: 's',
description: 'Silences npm output.',
exclusive: ['verbose'],
}),
verbose: Flags.boolean({
char: 'v',
description: 'Show verbose npm output.',
exclusive: ['silent'],
}),
}
static strict = false
static summary = 'Installs a plugin into <%= config.bin %>.'
flags!: Interfaces.InferredFlags<typeof PluginsInstall.flags>
// In this case we want these operations to happen
// sequentially so the `no-await-in-loop` rule is ignored
async parsePlugin(
plugins: Plugins,
input: string,
): Promise<{name: string; tag: string; type: 'npm'} | {type: 'repo'; url: string}> {
// git ssh url
if (input.startsWith('git+ssh://') || input.endsWith('.git')) {
return {type: 'repo', url: input}
}
const getNameAndTag = async (input: string): Promise<{name: string; tag: string}> => {
const regexAtSymbolNotAtBeginning = /(?<!^)@/
const [splitName, tag = 'latest'] = input.split(regexAtSymbolNotAtBeginning)
const name = splitName.startsWith('@') ? splitName : await plugins.maybeUnfriendlyName(splitName)
validateNpmPkgName(name)
if (this.flags.jit) {
const jitVersion = this.config.pjson.oclif?.jitPlugins?.[name]
if (jitVersion) {
if (regexAtSymbolNotAtBeginning.test(input))
this.warn(
`--jit flag is present along side a tag. Ignoring tag ${tag} and using the version specified in package.json (${jitVersion}).`,
)
return {name, tag: jitVersion}
}
this.warn(`--jit flag is present but ${name} is not a JIT plugin. Installing ${tag} instead.`)
return {name, tag}
}
return {name, tag}
}
// scoped npm package, e.g. @oclif/plugin-version
if (input.startsWith('@') && input.includes('/')) {
const {name, tag} = await getNameAndTag(input)
return {name, tag, type: 'npm'}
}
if (input.includes('/')) {
// github url, e.g. https://github.com/oclif/plugin-version
if (input.includes(':')) return {type: 'repo', url: input}
// github org/repo, e.g. oclif/plugin-version
return {type: 'repo', url: `https://github.com/${input}`}
}
// unscoped npm package, e.g. my-oclif-plugin
// friendly plugin name, e.g. version instead of @oclif/plugin-version (requires `scope` to be set in root plugin's package.json)
const {name, tag} = await getNameAndTag(input)
return {name, tag, type: 'npm'}
}
async run(): Promise<void> {
const {argv, flags} = await this.parse(PluginsInstall)
this.flags = flags
const plugins = new Plugins({
config: this.config,
logLevel: determineLogLevel(this.config, this.flags, 'notice'),
})
const aliases = this.config.pjson.oclif.aliases || {}
const count = argv.length
for (let name of argv as string[]) {
if (aliases[name] === null) this.error(`${name} is blocked`)
name = aliases[name] || name
// Ignore the root plugin. Error if it's the only plugin being installed.
if (name === this.config.name) {
const updateInstructions =
// this.config.binPath is set when the CLI is installed from an installer but not when it's installed from npm
this.config.binPath && this.config.plugins.get('@oclif/plugin-update')
? ` Use "${this.config.bin} update" to update ${this.config.name}.`
: ''
const msg = `Ignoring top-level CLI plugin ${this.config.name}.${updateInstructions}`
if (count === 1) {
this.error(msg)
} else {
this.warn(msg)
}
continue
}
const p = await this.parsePlugin(plugins, name)
let plugin
await this.config.runHook('plugins:preinstall', {
plugin: p,
})
try {
if (p.type === 'npm') {
ux.action.start(`${this.config.name}: Installing plugin ${cyan(plugins.friendlyName(p.name) + '@' + p.tag)}`)
plugin = await plugins.install(p.name, {
force: flags.force,
tag: p.tag,
})
} else {
ux.action.start(`${this.config.name}: Installing plugin ${cyan(p.url)}`)
plugin = await plugins.install(p.url, {force: flags.force})
}
} catch (error) {
ux.action.stop(bold.red('failed'))
throw error
}
const pluginInstance = new Plugin(plugin)
await pluginInstance.load()
this.config.plugins.set(pluginInstance.name, pluginInstance)
await this.config.runHook('plugins:postinstall', {})
ux.action.stop(`installed v${plugin.version}`)
}
}
}
function validateNpmPkgName(name: string): void {
if (!validate(name).validForNewPackages) {
throw new Errors.CLIError('Invalid npm package name.')
}
}