-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinstall.ts
159 lines (131 loc) · 5.8 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
import {Command, Flags, ux, Args, Errors, Interfaces} from '@oclif/core'
import * as validate from 'validate-npm-package-name'
import * as chalk from 'chalk'
import Plugins from '../../plugins'
export default class PluginsInstall extends Command {
static description = `Installs a plugin into the CLI.
Can be installed from npm or a git url.
Installation of a user-installed plugin will override a core plugin.
e.g. If you have a core plugin that has a 'hello' command, installing a user-installed plugin with a 'hello' command will override the core plugin implementation. This is useful if a user needs to update core plugin functionality in the CLI without the need to patch and update the whole CLI.
`;
static usage = 'plugins:install PLUGIN...';
static examples = [
'$ <%= config.bin %> plugins:install <%- config.pjson.oclif.examplePlugin || "myplugin" %> ',
'$ <%= config.bin %> plugins:install https://github.com/someuser/someplugin',
'$ <%= config.bin %> plugins:install someuser/someplugin',
];
static strict = false;
static args = {
plugin: Args.string({description: 'Plugin to install.', required: true}),
};
static flags = {
help: Flags.help({char: 'h'}),
verbose: Flags.boolean({char: 'v'}),
force: Flags.boolean({
char: 'f',
description: 'Run yarn install with force flag.',
}),
jit: Flags.boolean({
hidden: true,
parse: async (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(ctx.config)
const nonJitPlugins = await Promise.all(requestedPlugins.map(async plugin => {
const name = await plugins.maybeUnfriendlyName(plugin)
return {name, jit: Boolean(jitPluginsConfig[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
},
}),
};
static aliases = ['plugins:add'];
plugins = new Plugins(this.config);
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
/* eslint-disable no-await-in-loop */
async run(): Promise<void> {
const {flags, argv} = await this.parse(PluginsInstall)
this.flags = flags
if (flags.verbose) this.plugins.verbose = true
const aliases = this.config.pjson.oclif.aliases || {}
for (let name of argv as string[]) {
if (aliases[name] === null) this.error(`${name} is blocked`)
name = aliases[name] || name
const p = await this.parsePlugin(name)
let plugin
await this.config.runHook('plugins:preinstall', {
plugin: p,
})
try {
if (p.type === 'npm') {
ux.action.start(
`Installing plugin ${chalk.cyan(this.plugins.friendlyName(p.name) + '@' + p.tag)}`,
)
plugin = await this.plugins.install(p.name, {
tag: p.tag,
force: flags.force,
})
} else {
ux.action.start(`Installing plugin ${chalk.cyan(p.url)}`)
plugin = await this.plugins.install(p.url, {force: flags.force})
}
} catch (error) {
ux.action.stop(chalk.bold.red('failed'))
throw error
}
ux.action.stop(`installed v${plugin.version}`)
}
}
/* eslint-enable no-await-in-loop */
async parsePlugin(input: string): Promise<{name: string; tag: string; type: 'npm'} | {url: string; type: 'repo'}> {
// git ssh url
if (input.startsWith('git+ssh://') || input.endsWith('.git')) {
return {url: input, type: 'repo'}
}
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 this.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 {url: input, type: 'repo'}
// github org/repo, e.g. oclif/plugin-version
return {url: `https://github.com/${input}`, type: 'repo'}
}
// 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'}
}
}
function validateNpmPkgName(name:string): void {
if (!validate(name).validForNewPackages) {
throw new Errors.CLIError('Invalid npm package name.')
}
}