-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discover available plugins, and install them (#87)
* 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
1 parent
4c6d1a1
commit 4a2a66e
Showing
8 changed files
with
579 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.