Skip to content

Commit

Permalink
Add plugin find command
Browse files Browse the repository at this point in the history
Add plugin find command to list the nativescript plugins available in npm. The command has --all and --count flags to change the output format.
  • Loading branch information
TsvetanMilanov committed Apr 5, 2016
1 parent 2ac44da commit 2ac30c8
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 53 deletions.
8 changes: 5 additions & 3 deletions docs/man_pages/lib-management/plugin-add.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
plugin add
plugin add
==========

Usage | Synopsis
------|-------
General | `$ tns plugin add <Plugin>`

<% if(isConsole) { %>Installs the specified plugin and any packages that it depends on.<% } %>
<% if(isConsole) { %>Installs the specified plugin and any packages that it depends on.<% } %>
<% 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).<% } %>

### Attributes
Expand All @@ -28,4 +28,6 @@ Command | Description
----------|----------
[plugin](plugin.html) | Lets you manage the plugins for your project.
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
<% } %>
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
<% } %>
29 changes: 29 additions & 0 deletions docs/man_pages/lib-management/plugin-find.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugin find
==========

Usage | Synopsis
---|---
General | `$ tns plugin find [<PluginName>] [--all] [--count <Count>]`

Finds NativeScript plugins in npm.

### Options
* `--all` - Specifies that all results will be shown at once.
* `--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.

> **NOTE:** You cannot set --all and --count simultaneously.
### Attributes
* `<PluginName>` is the name of plugin that you want to find. When specified the search string in npm will be "`nativescript <PluginName>`".
* `<Count>` is the number of the plugins to display.

<% if(isHtml) { %>
### Related Commands

Command | Description
----------|----------
[plugin](plugin.html) | Lets you manage the plugins for your project.
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
<% } %>
8 changes: 5 additions & 3 deletions docs/man_pages/lib-management/plugin-remove.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
plugin remove
plugin remove
==========

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

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

<% if(isHtml) { %>
<% if(isHtml) { %>
### Related Commands

Command | Description
----------|----------
[plugin](plugin.html) | Lets you manage the plugins for your project.
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
<% } %>
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
<% } %>
29 changes: 29 additions & 0 deletions docs/man_pages/lib-management/plugin-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugin search
==========

Usage | Synopsis
---|---
General | `$ tns plugin search [<PluginName>] [--all] [--count <Count>]`

Finds NativeScript plugins in npm.

### Options
* `--all` - Specifies that all results will be shown at once.
* `--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.

> **NOTE:** You cannot set --all and --count simultaneously.
### Attributes
* `<PluginName>` is the name of plugin that you want to find. When specified the search string in npm will be "`nativescript <PluginName>`".
* `<Count>` is the number of the plugins to display.

<% if(isHtml) { %>
### Related Commands

Command | Description
----------|----------
[plugin](plugin.html) | Lets you manage the plugins for your project.
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
<% } %>
10 changes: 7 additions & 3 deletions docs/man_pages/lib-management/plugin.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
plugin
plugin
==========

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

<% if(isHtml) { %>
<% if(isHtml) { %>
### Related Commands

Command | Description
----------|----------
[plugin add](plugin-add.html) | Installs the specified plugin and its dependencies.
[plugin remove](plugin-remove.html) | Uninstalls the specified plugin and its dependencies.
<% } %>
[plugin find](plugin-find.html) | Finds NativeScript plugins in npm.
[plugin search](plugin-search.html) | Finds NativeScript plugins in npm.
<% } %>
2 changes: 2 additions & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ $injector.require("broccoliPluginWrapper", "./tools/broccoli/broccoli-plugin-wra
$injector.require("pluginVariablesService", "./services/plugin-variables-service");
$injector.require("pluginsService", "./services/plugins-service");
$injector.requireCommand("plugin|*list", "./commands/plugin/list-plugins");
$injector.requireCommand("plugin|find", "./commands/plugin/find-plugins");
$injector.requireCommand("plugin|search", "./commands/plugin/find-plugins");
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");

Expand Down
100 changes: 100 additions & 0 deletions lib/commands/plugin/find-plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
///<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.$logger.out("Available NativeScript plugins");
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();

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;

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);
28 changes: 12 additions & 16 deletions lib/commands/plugin/list-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
///<reference path="../../.d.ts"/>
"use strict";

import { createTable } from "../../common/helpers";

export class ListPluginsCommand implements ICommand {
constructor(private $pluginsService: IPluginsService,
private $logger: ILogger,
private $options: IOptions) { }
private $logger: ILogger) { }

allowedParameters: ICommandParameter[] = [];

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

_.each(installedPlugins.dependencies, (dependency: IBasePluginData) => {
dependenciesData.push([dependency.name, dependency.version]);
});
let headers: string[] = ["Plugin", "Version"];
let dependenciesData: string[][] = this.createTableCells(installedPlugins.dependencies);

let dependenciesTable: any = createTable(headers, dependenciesData);
this.$logger.out("Dependencies");
this.$logger.out("Dependencies:");
this.$logger.out(dependenciesTable.toString());

if (installedPlugins.devDependencies && installedPlugins.devDependencies.length) {
_.each(installedPlugins.devDependencies, (dependency: IBasePluginData) => {
devDependenciesData.push([dependency.name, dependency.version]);
});
let devDependenciesData: string[][] = this.createTableCells(installedPlugins.devDependencies);

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

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

this.$logger.warn("NOTE:");
this.$logger.warn(`If you want to see the dependencies of installed plugin use ${viewDependenciesCommand}`);
this.$logger.warn(`If you want to see the dev dependencies of installed plugin use ${viewDevDependenciesCommand}`);
this.$logger.warn(`If you want to check the dependencies of installed plugin use ${viewDependenciesCommand}`);
this.$logger.warn(`If you want to check the dev dependencies of installed plugin use ${viewDevDependenciesCommand}`);
}).future<void>()();
}

private createTableCells(items: IBasePluginData[]): string[][] {
return items.map(item => [item.name, item.version]);
}
}

$injector.registerCommand("plugin|*list", ListPluginsCommand);
2 changes: 1 addition & 1 deletion lib/common
Submodule common updated 2 files
+16 −15 declarations.d.ts
+14 −13 options.ts
4 changes: 3 additions & 1 deletion lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface INodePackageManager {
cacheUnpack(packageName: string, version: string, unpackTarget?: string): IFuture<void>;
view(packageName: string, propertyName: string): IFuture<any>;
executeNpmCommand(npmCommandName: string, currentWorkingDirectory: string): IFuture<any>;
search(filter: string[], silent: boolean): IFuture<any>;
}

interface INpmInstallationManager {
Expand Down Expand Up @@ -58,6 +59,7 @@ interface ILiveSyncService {
}

interface IOptions extends ICommonOptions {
all: boolean;
baseConfig: string;
client: boolean;
compileSdk: number;
Expand Down Expand Up @@ -267,5 +269,5 @@ interface IProjectNameService {
* @param {IOptions} current command options.
* @return {IFuture<strng>} returns the selected name of the project.
*/
ensureValidName(projectName: string, validateOptions?: {force: boolean}): IFuture<string>;
ensureValidName(projectName: string, validateOptions?: { force: boolean }): IFuture<string>;
}
1 change: 1 addition & 0 deletions lib/definitions/plugins.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface IPluginsService {
add(plugin: string): IFuture<void>; // adds plugin by name, github url, local path and et.
remove(pluginName: string): IFuture<void>; // removes plugin only by name
getAvailable(filter: string[]): IFuture<IDictionary<any>>; // gets all available plugins
prepare(pluginData: IDependencyData, platform: string): IFuture<void>;
getAllInstalledPlugins(): IFuture<IPluginData[]>;
ensureAllDependenciesAreInstalled(): IFuture<void>;
Expand Down
13 changes: 9 additions & 4 deletions lib/node-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class NodePackageManager implements INodePackageManager {
} else {
let future = new Future<void>();
npm.load(config, (err: Error) => {
if(err) {
if (err) {
future.throw(err);
} else {
future.return();
Expand All @@ -40,7 +40,7 @@ export class NodePackageManager implements INodePackageManager {
}

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

public uninstall(packageName: string, config?: any, path?: string): IFuture<any> {
return this.loadAndExecute("uninstall", [[packageName]], { config, path});
return this.loadAndExecute("uninstall", [[packageName]], { config, path });
}

public search(filter: string[], silent: boolean): IFuture<any> {
let args = (<any[]>([filter] || [])).concat(silent);
return this.loadAndExecute("search", args);
}

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

if(err) {
if (err) {
future.throw(err);
} else {
future.return(data);
Expand Down
2 changes: 1 addition & 1 deletion lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Options extends commonOptionsLibPath.OptionsBase {
baseConfig: { type: OptionType.String },
platformTemplate: { type: OptionType.String },
ng: {type: OptionType.Boolean },
available: {type: OptionType.Boolean }
all: {type: OptionType.Boolean }
},
path.join($hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"),
$errors, $staticConfig);
Expand Down
Loading

0 comments on commit 2ac30c8

Please sign in to comment.