Skip to content

Commit 2ac30c8

Browse files
Add plugin find command
Add plugin find command to list the nativescript plugins available in npm. The command has --all and --count flags to change the output format.
1 parent 2ac44da commit 2ac30c8

15 files changed

+236
-53
lines changed

docs/man_pages/lib-management/plugin-add.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
plugin add
1+
plugin add
22
==========
33

44
Usage | Synopsis
55
------|-------
66
General | `$ tns plugin add <Plugin>`
77

8-
<% if(isConsole) { %>Installs the specified plugin and any packages that it depends on.<% } %>
8+
<% if(isConsole) { %>Installs the specified plugin and any packages that it depends on.<% } %>
99
<% if(isHtml) { %>Installs the specified plugin and its dependencies in the local `node_modules` folder, adds it to the `dependencies` section in `package.json`, and prepares the plugin for all installed platforms. If you have not configured any platforms for the project, the NativeScript CLI will prepare the plugin when you add a platform. For more information about working with plugins, see [NativeScript Plugins](https://github.com/NativeScript/nativescript-cli/blob/master/PLUGINS.md).<% } %>
1010

1111
### Attributes
@@ -28,4 +28,6 @@ Command | Description
2828
----------|----------
2929
[plugin](plugin.html) | Lets you manage the plugins for your project.
3030
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
31-
<% } %>
31+
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
32+
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
33+
<% } %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
plugin find
2+
==========
3+
4+
Usage | Synopsis
5+
---|---
6+
General | `$ tns plugin find [<PluginName>] [--all] [--count <Count>]`
7+
8+
Finds NativeScript plugins in npm.
9+
10+
### Options
11+
* `--all` - Specifies that all results will be shown at once.
12+
* `--count` - Specifies the number of results to show at a time. If not set, the default value is 10. After showing the specified number of results, the CLI will prompt you to continue showing more results or to exit the operation.
13+
14+
> **NOTE:** You cannot set --all and --count simultaneously.
15+
16+
### Attributes
17+
* `<PluginName>` is the name of plugin that you want to find. When specified the search string in npm will be "`nativescript <PluginName>`".
18+
* `<Count>` is the number of the plugins to display.
19+
20+
<% if(isHtml) { %>
21+
### Related Commands
22+
23+
Command | Description
24+
----------|----------
25+
[plugin](plugin.html) | Lets you manage the plugins for your project.
26+
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
27+
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
28+
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
29+
<% } %>

docs/man_pages/lib-management/plugin-remove.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
plugin remove
1+
plugin remove
22
==========
33

44
Usage | Synopsis
@@ -12,11 +12,13 @@ General | `$ tns plugin remove <Plugin>`
1212

1313
* `<Plugin>` is the name of the plugin as listed in its `package.json` file.
1414

15-
<% if(isHtml) { %>
15+
<% if(isHtml) { %>
1616
### Related Commands
1717

1818
Command | Description
1919
----------|----------
2020
[plugin](plugin.html) | Lets you manage the plugins for your project.
2121
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
22-
<% } %>
22+
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
23+
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
24+
<% } %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
plugin search
2+
==========
3+
4+
Usage | Synopsis
5+
---|---
6+
General | `$ tns plugin search [<PluginName>] [--all] [--count <Count>]`
7+
8+
Finds NativeScript plugins in npm.
9+
10+
### Options
11+
* `--all` - Specifies that all results will be shown at once.
12+
* `--count` - Specifies the number of results to show at a time. If not set, the default value is 10. After showing the specified number of results, the CLI will prompt you to continue showing more results or to exit the operation.
13+
14+
> **NOTE:** You cannot set --all and --count simultaneously.
15+
16+
### Attributes
17+
* `<PluginName>` is the name of plugin that you want to find. When specified the search string in npm will be "`nativescript <PluginName>`".
18+
* `<Count>` is the number of the plugins to display.
19+
20+
<% if(isHtml) { %>
21+
### Related Commands
22+
23+
Command | Description
24+
----------|----------
25+
[plugin](plugin.html) | Lets you manage the plugins for your project.
26+
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
27+
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
28+
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
29+
<% } %>

docs/man_pages/lib-management/plugin.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
plugin
1+
plugin
22
==========
33

44
Usage | Synopsis
@@ -12,12 +12,16 @@ Lets you manage the plugins for your project.
1212
`<Command>` extends the `plugin` command. You can set the following values for this attribute.
1313
* `add` - Installs the specified plugin and its dependencies.
1414
* `remove` - Uninstalls the specified plugin and its dependencies.
15+
* `find` - Finds NativeScript plugins in npm.
16+
* `search` - Finds NativeScript plugins in npm.
1517

16-
<% if(isHtml) { %>
18+
<% if(isHtml) { %>
1719
### Related Commands
1820

1921
Command | Description
2022
----------|----------
2123
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
2224
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
23-
<% } %>
25+
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
26+
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
27+
<% } %>

lib/bootstrap.ts

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ $injector.require("broccoliPluginWrapper", "./tools/broccoli/broccoli-plugin-wra
7777
$injector.require("pluginVariablesService", "./services/plugin-variables-service");
7878
$injector.require("pluginsService", "./services/plugins-service");
7979
$injector.requireCommand("plugin|*list", "./commands/plugin/list-plugins");
80+
$injector.requireCommand("plugin|find", "./commands/plugin/find-plugins");
81+
$injector.requireCommand("plugin|search", "./commands/plugin/find-plugins");
8082
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
8183
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
8284

lib/commands/plugin/find-plugins.ts

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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);

lib/commands/plugin/list-plugins.ts

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
///<reference path="../../.d.ts"/>
22
"use strict";
3-
43
import { createTable } from "../../common/helpers";
54

65
export class ListPluginsCommand implements ICommand {
76
constructor(private $pluginsService: IPluginsService,
8-
private $logger: ILogger,
9-
private $options: IOptions) { }
7+
private $logger: ILogger) { }
108

119
allowedParameters: ICommandParameter[] = [];
1210

1311
public execute(args: string[]): IFuture<void> {
1412
return (() => {
1513
let installedPlugins: IPackageJsonDepedenciesResult = this.$pluginsService.getDependenciesFromPackageJson().wait();
16-
let headers: string[] = ["Plugin", "Version"];
17-
let dependenciesData: string[][] = [];
18-
let devDependenciesData: string[][] = [];
1914

20-
_.each(installedPlugins.dependencies, (dependency: IBasePluginData) => {
21-
dependenciesData.push([dependency.name, dependency.version]);
22-
});
15+
let headers: string[] = ["Plugin", "Version"];
16+
let dependenciesData: string[][] = this.createTableCells(installedPlugins.dependencies);
2317

2418
let dependenciesTable: any = createTable(headers, dependenciesData);
25-
this.$logger.out("Dependencies");
19+
this.$logger.out("Dependencies:");
2620
this.$logger.out(dependenciesTable.toString());
2721

2822
if (installedPlugins.devDependencies && installedPlugins.devDependencies.length) {
29-
_.each(installedPlugins.devDependencies, (dependency: IBasePluginData) => {
30-
devDependenciesData.push([dependency.name, dependency.version]);
31-
});
23+
let devDependenciesData: string[][] = this.createTableCells(installedPlugins.devDependencies);
3224

3325
let devDependenciesTable: any = createTable(headers, devDependenciesData);
3426

35-
this.$logger.out("Dev Dependencies");
27+
this.$logger.out("Dev Dependencies:");
3628
this.$logger.out(devDependenciesTable.toString());
3729
} else {
3830
this.$logger.out("There are no dev dependencies.");
@@ -42,10 +34,14 @@ export class ListPluginsCommand implements ICommand {
4234
let viewDevDependenciesCommand: string = "npm view <pluginName> grep devDependencies".cyan.toString();
4335

4436
this.$logger.warn("NOTE:");
45-
this.$logger.warn(`If you want to see the dependencies of installed plugin use ${viewDependenciesCommand}`);
46-
this.$logger.warn(`If you want to see the dev dependencies of installed plugin use ${viewDevDependenciesCommand}`);
37+
this.$logger.warn(`If you want to check the dependencies of installed plugin use ${viewDependenciesCommand}`);
38+
this.$logger.warn(`If you want to check the dev dependencies of installed plugin use ${viewDevDependenciesCommand}`);
4739
}).future<void>()();
4840
}
41+
42+
private createTableCells(items: IBasePluginData[]): string[][] {
43+
return items.map(item => [item.name, item.version]);
44+
}
4945
}
5046

5147
$injector.registerCommand("plugin|*list", ListPluginsCommand);

lib/common

lib/declarations.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface INodePackageManager {
77
cacheUnpack(packageName: string, version: string, unpackTarget?: string): IFuture<void>;
88
view(packageName: string, propertyName: string): IFuture<any>;
99
executeNpmCommand(npmCommandName: string, currentWorkingDirectory: string): IFuture<any>;
10+
search(filter: string[], silent: boolean): IFuture<any>;
1011
}
1112

1213
interface INpmInstallationManager {
@@ -58,6 +59,7 @@ interface ILiveSyncService {
5859
}
5960

6061
interface IOptions extends ICommonOptions {
62+
all: boolean;
6163
baseConfig: string;
6264
client: boolean;
6365
compileSdk: number;
@@ -267,5 +269,5 @@ interface IProjectNameService {
267269
* @param {IOptions} current command options.
268270
* @return {IFuture<strng>} returns the selected name of the project.
269271
*/
270-
ensureValidName(projectName: string, validateOptions?: {force: boolean}): IFuture<string>;
272+
ensureValidName(projectName: string, validateOptions?: { force: boolean }): IFuture<string>;
271273
}

lib/definitions/plugins.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
interface IPluginsService {
22
add(plugin: string): IFuture<void>; // adds plugin by name, github url, local path and et.
33
remove(pluginName: string): IFuture<void>; // removes plugin only by name
4+
getAvailable(filter: string[]): IFuture<IDictionary<any>>; // gets all available plugins
45
prepare(pluginData: IDependencyData, platform: string): IFuture<void>;
56
getAllInstalledPlugins(): IFuture<IPluginData[]>;
67
ensureAllDependenciesAreInstalled(): IFuture<void>;

lib/node-package-manager.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class NodePackageManager implements INodePackageManager {
2929
} else {
3030
let future = new Future<void>();
3131
npm.load(config, (err: Error) => {
32-
if(err) {
32+
if (err) {
3333
future.throw(err);
3434
} else {
3535
future.return();
@@ -40,7 +40,7 @@ export class NodePackageManager implements INodePackageManager {
4040
}
4141

4242
public install(packageName: string, pathToSave: string, config?: any): IFuture<any> {
43-
if(this.$options.ignoreScripts) {
43+
if (this.$options.ignoreScripts) {
4444
config = config || {};
4545
config["ignore-scripts"] = true;
4646
}
@@ -49,7 +49,12 @@ export class NodePackageManager implements INodePackageManager {
4949
}
5050

5151
public uninstall(packageName: string, config?: any, path?: string): IFuture<any> {
52-
return this.loadAndExecute("uninstall", [[packageName]], { config, path});
52+
return this.loadAndExecute("uninstall", [[packageName]], { config, path });
53+
}
54+
55+
public search(filter: string[], silent: boolean): IFuture<any> {
56+
let args = (<any[]>([filter] || [])).concat(silent);
57+
return this.loadAndExecute("search", args);
5358
}
5459

5560
public cache(packageName: string, version: string, config?: any): IFuture<IDependencyData> {
@@ -86,7 +91,7 @@ export class NodePackageManager implements INodePackageManager {
8691
npm.prefix = oldNpmPath;
8792
}
8893

89-
if(err) {
94+
if (err) {
9095
future.throw(err);
9196
} else {
9297
future.return(data);

lib/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Options extends commonOptionsLibPath.OptionsBase {
3737
baseConfig: { type: OptionType.String },
3838
platformTemplate: { type: OptionType.String },
3939
ng: {type: OptionType.Boolean },
40-
available: {type: OptionType.Boolean }
40+
all: {type: OptionType.Boolean }
4141
},
4242
path.join($hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"),
4343
$errors, $staticConfig);

0 commit comments

Comments
 (0)