-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathfind-plugins.ts
106 lines (78 loc) · 3.04 KB
/
find-plugins.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
///<reference path="../../.d.ts"/>
"use strict";
import { createTable, isInteractive } from "../../common/helpers";
import { NATIVESCRIPT_KEY_NAME } from "../../constants";
import Future = require("fibers/future");
export class FindPluginsCommand implements ICommand {
private static COUNT_OF_PLUGINS_TO_DISPLAY: number = 10;
constructor(private $pluginsService: IPluginsService,
private $errors: IErrors,
private $logger: ILogger,
private $prompter: IPrompter,
private $options: IOptions,
private $progressIndicator: IProgressIndicator) { }
execute(args: string[]): IFuture<void> {
return (() => {
let filter: string[] = this.prepareFilter(args);
let pluginsFuture: IFuture<IDictionary<any>> = this.$pluginsService.getAvailable(filter);
if (this.$options.json) {
this.$logger.out(JSON.stringify(pluginsFuture.wait()));
return;
}
this.$logger.printInfoMessageOnSameLine("Searching npm please be patient...");
this.$progressIndicator.showProgressIndicator(pluginsFuture, 500).wait();
let plugins: IDictionary<any> = pluginsFuture.get();
this.showPlugins(plugins).wait();
}).future<void>()();
}
canExecute(args: string[]): IFuture<boolean> {
return Future.fromResult(true);
}
public allowedParameters: ICommandParameter[] = [];
private showPlugins(plugins: IDictionary<any>): IFuture<void> {
return (() => {
let allPluginsNames: string[] = _.keys(plugins).sort();
if (!allPluginsNames || !allPluginsNames.length) {
this.$logger.warn("No plugins found.");
return;
}
let count: number = this.$options.count || FindPluginsCommand.COUNT_OF_PLUGINS_TO_DISPLAY;
if (!isInteractive() || this.$options.all) {
count = allPluginsNames.length;
}
let data: string[][] = [];
let pluginsToDisplay: string[] = allPluginsNames.splice(0, count);
let shouldDisplayMorePlugins: boolean = true;
this.$logger.out("Available NativeScript plugins:");
do {
data = this.createTableCells(plugins, pluginsToDisplay);
let table: any = this.createPluginsTable(data);
this.$logger.out(table.toString());
pluginsToDisplay = allPluginsNames.splice(0, count);
if (!pluginsToDisplay || pluginsToDisplay.length < 1) {
return;
}
shouldDisplayMorePlugins = this.$prompter.confirm("Load more plugins?").wait();
} while (shouldDisplayMorePlugins);
}).future<void>()();
}
private createPluginsTable(data: string[][]): any {
let headers: string[] = ["Plugin", "Version", "Description"];
let table: any = createTable(headers, data);
return table;
}
private createTableCells(plugins: IDictionary<any>, pluginsToDisplay: string[]): string[][] {
return pluginsToDisplay.map(pluginName => {
let pluginDetails: any = plugins[pluginName];
return [pluginName, pluginDetails.version, pluginDetails.description || ""];
});
}
private prepareFilter(args: string[]): string[] {
return _(args || [])
.map(arg => arg.toLowerCase())
.concat(NATIVESCRIPT_KEY_NAME)
.uniq()
.value();
}
}
$injector.registerCommand(["plugin|find", "plugin|search"], FindPluginsCommand);