Skip to content

Commit

Permalink
Discover available plugins, and install them (#87)
Browse files Browse the repository at this point in the history
* added basic search functionality (no tests)
* filter discover results that are not adobe scoped
* use api.npm.io to search, install with -i flag
* remove already installed plugins from installable list
* Add sorting, fix linting, re-factor a bit
* Completed 100% test coverage for discover command
  • Loading branch information
purplecabbage authored and shazron committed Dec 3, 2019
1 parent 4c6d1a1 commit 4a2a66e
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 14 deletions.
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ aio-cli
Adobe I/O Extensible CLI

<!-- toc -->
* [Getting Started](#getting-started)
* [aio cli health](#aio-cli-health)
* [Getting started](#getting-started)
* [Usage](#usage)
* [Commands](#commands)
<!-- tocstop -->
Expand Down Expand Up @@ -210,6 +211,7 @@ USAGE
* [`aio console:reset-integration [INTEGRATION_ID]`](#aio-consolereset-integration-integration_id)
* [`aio console:select-integration [INTEGRATION_ID]`](#aio-consoleselect-integration-integration_id)
* [`aio console:selected-integration`](#aio-consoleselected-integration)
* [`aio discover`](#aio-discover)
* [`aio help [COMMAND]`](#aio-help-command)
* [`aio jwt-auth:access-token`](#aio-jwt-authaccess-token)
* [`aio plugins`](#aio-plugins)
Expand Down Expand Up @@ -689,7 +691,7 @@ USAGE
$ aio console:reset-integration [INTEGRATION_ID]
DESCRIPTION
after running this command all clients will need to run `console:select-integration`
after running this command all clients will need to run `console:select-integration`
to get a new auth hash in their .wskprops file
ALIASES
Expand All @@ -715,7 +717,7 @@ OPTIONS
DESCRIPTION
Run 'console:ls' to get a list of integrations to select from.
The .wskprops file will be written to your home folder, and you will be prompted whether you want to overwrite an
The .wskprops file will be written to your home folder, and you will be prompted whether you want to overwrite an
existing file.
ALIASES
Expand Down Expand Up @@ -743,6 +745,30 @@ ALIASES

_See code: [@adobe/aio-cli-plugin-console](https://github.com/adobe/aio-cli-plugin-console/blob/v2.0.3/src/commands/console/selected-integration.js)_

## `aio discover`

Discover plugins to install

```
USAGE
$ aio discover
OPTIONS
-f, --sort-field=date|name [default: date] which column to sort, use the sort-order flag to specify sort direction
-i, --install interactive install mode
-o, --sort-order=asc|desc [default: desc] sort order for a column, use the sort-field flag to specify which column
to sort
DESCRIPTION
To install a plugin, run 'aio plugins install NAME'
ALIASES
$ aio plugins:discover
```

_See code: [src/commands/discover.js](https://github.com/adobe/aio-cli/blob/v2.2.3/src/commands/discover.js)_

## `aio help [COMMAND]`

display help for aio
Expand Down Expand Up @@ -834,15 +860,15 @@ DESCRIPTION
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
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.
ALIASES
$ aio plugins:add
EXAMPLES
$ aio plugins:install myplugin
$ aio plugins:install myplugin
$ aio plugins:install https://github.com/someuser/someplugin
$ aio plugins:install someuser/someplugin
```
Expand All @@ -867,7 +893,7 @@ OPTIONS
DESCRIPTION
Installation of a linked plugin will override a user-installed or core plugin.
e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
command will override the user-installed or core plugin implementation. This is useful for development work.
EXAMPLE
Expand Down
52 changes: 46 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@oclif/plugin-help": "^2.1.4",
"@oclif/plugin-not-found": "^1.2.0",
"@oclif/plugin-plugins": "^1.2.0",
"inquirer": "^6.3.1",
"node-fetch": "^2.5.0",
"chalk": "^2.4.2"
},
"devDependencies": {
Expand All @@ -34,6 +36,7 @@
"eslint-plugin-promise": "^4.0.0",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.9.0",
"jest-fetch-mock": "^2.1.2",
"jest-junit": "^9.0.0",
"jest-plugin-fs": "^2.9.0",
"stdout-stderr": "^0.1.9"
Expand All @@ -54,7 +57,7 @@
"license": "Apache-2.0",
"main": "lib/index.js",
"oclif": {
"commands": "./lib/commands",
"commands": "./src/commands",
"bin": "aio",
"plugins": [
"@oclif/plugin-help",
Expand Down
143 changes: 143 additions & 0 deletions src/commands/discover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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 } = require('@oclif/command')
const { cli } = require('cli-ux')
const fetch = require('node-fetch')
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
cli.table(plugins, columns)
}

async run () {
const { flags } = this.parse(DiscoCommand)

try {
const url = 'https://api.npms.io/v2/search?q=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.results
.map(e => e.package)
.filter(elem => elem.scope === 'adobe')

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('Oops:' + error)
}
}
}

DiscoCommand.description = `Discover plugins to install
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
33 changes: 33 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Sort array values according to the sort order and/or sort-field.
*
* Note that this will use the Javascript sort() function, thus the values will
* be sorted in-place.
*
* @param {array} values array of objects (with fields to sort by)
* @param {object} [options]
* @param {boolean} [options.descending] true by default, sort order
* @param {string} [options.field] 'date' by default, sort field ('name', 'date' options)
*/
function sortValues (values, { descending = true, field = 'date' } = {}) {
const supportedFields = ['name', 'date']
if (!supportedFields.includes(field)) { // unknown field, we just return the array
return values
}

values.sort((left, right) => {
const d1 = left[field]
const d2 = right[field]

if (descending) {
return (d1 > d2) ? -1 : (d1 < d2) ? 1 : 0
} else {
return (d1 > d2) ? 1 : (d1 < d2) ? -1 : 0
}
})
return values
}

module.exports = {
sortValues
}
Loading

0 comments on commit 4a2a66e

Please sign in to comment.