-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdiscover.js
More file actions
144 lines (125 loc) · 4.07 KB
/
discover.js
File metadata and controls
144 lines (125 loc) · 4.07 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
/*
* Copyright 2019 Adobe Inc. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const { Command, Flags, ux } = require('@oclif/core')
const inquirer = require('inquirer')
const { sortValues } = require('../helpers')
/*
This is how cordova does it:
https://npmsearch.com/query?fields=name,keywords,license,description,author,modified,homepage,version,rating&q=keywords:%22ecosystem:cordova%22&sort=rating:desc&size=500&start=0
future: use keywords ecosytem:aio-cli-plugin
*/
class DiscoCommand extends Command {
async _install (plugins) {
// get installed plugins
const installedPlugins = this.config.commands.map(elem => {
return elem.pluginName
})
const inqChoices = plugins
.filter(elem => { // remove any installed plugins from the list
return !installedPlugins.includes(elem.name)
})
.map(elem => { // map to expected inquirer format
return {
name: `${elem.name}@${elem.version}`,
value: elem.name
}
})
if (!(inqChoices.length)) {
this.log('All available plugins appear to be installed.')
return []
}
const response = await inquirer.prompt([{
name: 'plugins',
message: 'Select plugins to install',
type: 'checkbox',
choices: inqChoices
}])
// install the plugins in sequence
for (const plugin of response.plugins) {
await this.config.runCommand('plugins:install', [plugin])
}
return response.plugins
}
async _list (plugins) {
const options = {
year: 'numeric',
month: 'long',
day: 'numeric'
}
const columns = {
name: {
width: 10,
get: row => `${row.name}`
},
version: {
minWidth: 10,
get: row => `${row.version}`
},
description: {
get: row => `${row.description}`
},
published: {
get: row => `${new Date(row.date).toLocaleDateString('en', options)}`
}
}
// skip ones that aren't from us
ux.table(plugins, columns)
}
async run () {
const { flags } = await this.parse(DiscoCommand)
try {
const url = 'https://registry.npmjs.org/-/v1/search?text=aio-cli-plugin'
const response = await fetch(url)
const json = await response.json()
// ours only, this could become a flag, searching for oclif-plugin reveals some more
const adobeOnly = json.objects
.map(e => e.package)
.filter(elem => elem.name && elem.name.startsWith('@adobe/aio-cli-plugin'))
sortValues(adobeOnly, {
descending: flags['sort-order'] !== 'asc',
field: flags['sort-field']
})
if (flags.install) {
return this._install(adobeOnly)
} else {
return this._list(adobeOnly)
}
} catch (error) {
this.error(error.toString())
}
}
}
DiscoCommand.description = `Discover plugins to install
Lists only plugins with prefix '@adobe/aio-cli-plugin'
To install a plugin, run 'aio plugins install NAME'
`
DiscoCommand.aliases = ['plugins:discover']
DiscoCommand.flags = {
install: Flags.boolean({
char: 'i',
default: false,
description: 'interactive install mode'
}),
'sort-field': Flags.string({
char: 'f',
default: 'date',
options: ['date', 'name'],
description: 'which column to sort, use the sort-order flag to specify sort direction'
}),
'sort-order': Flags.string({
char: 'o',
default: 'desc',
options: ['asc', 'desc'],
description: 'sort order for a column, use the sort-field flag to specify which column to sort'
})
}
module.exports = DiscoCommand