|
| 1 | +///<reference path="../../.d.ts"/> |
| 2 | +"use strict"; |
| 3 | +import { createTable, isInteractive } from "../../common/helpers"; |
| 4 | +import { NATIVESCRIPT_KEY_NAME } from "../../constants"; |
| 5 | +import Future = require("fibers/future"); |
| 6 | + |
| 7 | +export class FindPluginsCommand implements ICommand { |
| 8 | + private static COUNT_OF_PLUGINS_TO_DISPLAY: number = 10; |
| 9 | + |
| 10 | + constructor(private $pluginsService: IPluginsService, |
| 11 | + private $errors: IErrors, |
| 12 | + private $logger: ILogger, |
| 13 | + private $prompter: IPrompter, |
| 14 | + private $options: IOptions, |
| 15 | + private $progressIndicator: IProgressIndicator) { } |
| 16 | + |
| 17 | + execute(args: string[]): IFuture<void> { |
| 18 | + return (() => { |
| 19 | + let filter: string[] = this.prepareFilter(args); |
| 20 | + |
| 21 | + let pluginsFuture: IFuture<IDictionary<any>> = this.$pluginsService.getAvailable(filter); |
| 22 | + if (this.$options.json) { |
| 23 | + this.$logger.out(JSON.stringify(pluginsFuture.wait())); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + this.$logger.printInfoMessageOnSameLine("Searching npm please be patient..."); |
| 28 | + this.$progressIndicator.showProgressIndicator(pluginsFuture, 500).wait(); |
| 29 | + let plugins: IDictionary<any> = pluginsFuture.get(); |
| 30 | + |
| 31 | + this.$logger.out("Available NativeScript plugins"); |
| 32 | + this.showPlugins(plugins).wait(); |
| 33 | + }).future<void>()(); |
| 34 | + } |
| 35 | + |
| 36 | + canExecute(args: string[]): IFuture<boolean> { |
| 37 | + return Future.fromResult(true); |
| 38 | + } |
| 39 | + |
| 40 | + public allowedParameters: ICommandParameter[] = []; |
| 41 | + |
| 42 | + private showPlugins(plugins: IDictionary<any>): IFuture<void> { |
| 43 | + return (() => { |
| 44 | + let allPluginsNames: string[] = _.keys(plugins).sort(); |
| 45 | + |
| 46 | + let count: number = this.$options.count || FindPluginsCommand.COUNT_OF_PLUGINS_TO_DISPLAY; |
| 47 | + |
| 48 | + if (!isInteractive() || this.$options.all) { |
| 49 | + count = allPluginsNames.length; |
| 50 | + } |
| 51 | + |
| 52 | + let data: string[][] = []; |
| 53 | + |
| 54 | + let pluginsToDisplay: string[] = allPluginsNames.splice(0, count); |
| 55 | + let shouldDisplayMorePlugins: boolean = true; |
| 56 | + |
| 57 | + do { |
| 58 | + data = this.createTableCells(plugins, pluginsToDisplay); |
| 59 | + |
| 60 | + let table: any = this.createPluginsTable(data); |
| 61 | + |
| 62 | + this.$logger.out(table.toString()); |
| 63 | + |
| 64 | + pluginsToDisplay = allPluginsNames.splice(0, count); |
| 65 | + |
| 66 | + if (!pluginsToDisplay || pluginsToDisplay.length < 1) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + shouldDisplayMorePlugins = this.$prompter.confirm("Load more plugins?").wait(); |
| 71 | + } while (shouldDisplayMorePlugins); |
| 72 | + }).future<void>()(); |
| 73 | + } |
| 74 | + |
| 75 | + private createPluginsTable(data: string[][]): any { |
| 76 | + let headers: string[] = ["Plugin", "Version", "Description"]; |
| 77 | + |
| 78 | + let table: any = createTable(headers, data); |
| 79 | + |
| 80 | + return table; |
| 81 | + } |
| 82 | + |
| 83 | + private createTableCells(plugins: IDictionary<any>, pluginsToDisplay: string[]): string[][] { |
| 84 | + return pluginsToDisplay.map(pluginName => { |
| 85 | + let pluginDetails: any = plugins[pluginName]; |
| 86 | + return [pluginName, pluginDetails.version, pluginDetails.description || ""]; |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + private prepareFilter(args: string[]): string[] { |
| 91 | + return _(args || []) |
| 92 | + .map(arg => arg.toLowerCase()) |
| 93 | + .concat(NATIVESCRIPT_KEY_NAME) |
| 94 | + .uniq() |
| 95 | + .value(); |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +$injector.registerCommand(["plugin|find", "plugin|search"], FindPluginsCommand); |
0 commit comments