Skip to content

Commit 2ac44da

Browse files
Add list dependencies
Add list plugins registered in package.json file when executing tns plugin in project.
1 parent 6d2c76e commit 2ac44da

File tree

5 files changed

+109
-20
lines changed

5 files changed

+109
-20
lines changed

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ $injector.require("broccoliPluginWrapper", "./tools/broccoli/broccoli-plugin-wra
7676

7777
$injector.require("pluginVariablesService", "./services/plugin-variables-service");
7878
$injector.require("pluginsService", "./services/plugins-service");
79+
$injector.requireCommand("plugin|*list", "./commands/plugin/list-plugins");
7980
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
8081
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
8182

lib/commands/plugin/list-plugins.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
///<reference path="../../.d.ts"/>
2+
"use strict";
3+
4+
import { createTable } from "../../common/helpers";
5+
6+
export class ListPluginsCommand implements ICommand {
7+
constructor(private $pluginsService: IPluginsService,
8+
private $logger: ILogger,
9+
private $options: IOptions) { }
10+
11+
allowedParameters: ICommandParameter[] = [];
12+
13+
public execute(args: string[]): IFuture<void> {
14+
return (() => {
15+
let installedPlugins: IPackageJsonDepedenciesResult = this.$pluginsService.getDependenciesFromPackageJson().wait();
16+
let headers: string[] = ["Plugin", "Version"];
17+
let dependenciesData: string[][] = [];
18+
let devDependenciesData: string[][] = [];
19+
20+
_.each(installedPlugins.dependencies, (dependency: IBasePluginData) => {
21+
dependenciesData.push([dependency.name, dependency.version]);
22+
});
23+
24+
let dependenciesTable: any = createTable(headers, dependenciesData);
25+
this.$logger.out("Dependencies");
26+
this.$logger.out(dependenciesTable.toString());
27+
28+
if (installedPlugins.devDependencies && installedPlugins.devDependencies.length) {
29+
_.each(installedPlugins.devDependencies, (dependency: IBasePluginData) => {
30+
devDependenciesData.push([dependency.name, dependency.version]);
31+
});
32+
33+
let devDependenciesTable: any = createTable(headers, devDependenciesData);
34+
35+
this.$logger.out("Dev Dependencies");
36+
this.$logger.out(devDependenciesTable.toString());
37+
} else {
38+
this.$logger.out("There are no dev dependencies.");
39+
}
40+
41+
let viewDependenciesCommand: string = "npm view <pluginName> grep dependencies".cyan.toString();
42+
let viewDevDependenciesCommand: string = "npm view <pluginName> grep devDependencies".cyan.toString();
43+
44+
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}`);
47+
}).future<void>()();
48+
}
49+
}
50+
51+
$injector.registerCommand("plugin|*list", ListPluginsCommand);

lib/definitions/plugins.d.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ interface IPluginsService {
55
getAllInstalledPlugins(): IFuture<IPluginData[]>;
66
ensureAllDependenciesAreInstalled(): IFuture<void>;
77
afterPrepareAllPlugins(): IFuture<void>;
8+
getDependenciesFromPackageJson(): IFuture<IPackageJsonDepedenciesResult>
9+
}
10+
11+
interface IPackageJsonDepedenciesResult {
12+
dependencies: IBasePluginData[],
13+
devDependencies?: IBasePluginData[]
14+
}
15+
16+
interface IBasePluginData {
17+
name: string;
18+
version: string;
819
}
920

1021
interface IPluginData extends INodeModuleData {
@@ -14,9 +25,7 @@ interface IPluginData extends INodeModuleData {
1425
pluginPlatformsFolderPath(platform: string): string;
1526
}
1627

17-
interface INodeModuleData {
18-
name: string;
19-
version: string;
28+
interface INodeModuleData extends IBasePluginData {
2029
fullPath: string;
2130
isPlugin: boolean;
2231
moduleInfo: any;
@@ -70,4 +79,4 @@ interface IPluginVariableData {
7079
defaultValue?: string;
7180
name?: string;
7281
value?: string;
73-
}
82+
}

lib/options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export class Options extends commonOptionsLibPath.OptionsBase {
3636
copyTo: { type: OptionType.String },
3737
baseConfig: { type: OptionType.String },
3838
platformTemplate: { type: OptionType.String },
39-
ng: {type: OptionType.Boolean }
39+
ng: {type: OptionType.Boolean },
40+
available: {type: OptionType.Boolean }
4041
},
4142
path.join($hostInfo.isWindows ? process.env.AppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"),
4243
$errors, $staticConfig);

lib/services/plugins-service.ts

+42-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class PluginsService implements IPluginsService {
3030
return (() => {
3131
this.ensure().wait();
3232
let dependencyData = this.$npm.cache(plugin, undefined, PluginsService.NPM_CONFIG).wait();
33-
if(dependencyData.nativescript) {
33+
if (dependencyData.nativescript) {
3434
let pluginData = this.convertToPluginData(dependencyData);
3535

3636
// Validate
@@ -44,7 +44,7 @@ export class PluginsService implements IPluginsService {
4444
try {
4545
this.$pluginVariablesService.savePluginVariablesInProjectFile(pluginData).wait();
4646
this.executeNpmCommand(PluginsService.INSTALL_COMMAND_NAME, plugin).wait();
47-
} catch(err) {
47+
} catch (err) {
4848
// Revert package.json
4949
this.$projectDataService.initialize(this.$projectData.projectDir);
5050
this.$projectDataService.removeProperty(this.$pluginVariablesService.getPluginVariablePropertyName(pluginData.name)).wait();
@@ -87,7 +87,7 @@ export class PluginsService implements IPluginsService {
8787
};
8888
this.executeForAllInstalledPlatforms(action).wait();
8989

90-
if(showMessage) {
90+
if (showMessage) {
9191
this.$logger.out(`Succsessfully removed plugin ${pluginName}`);
9292
}
9393
}).future<void>()();
@@ -100,11 +100,11 @@ export class PluginsService implements IPluginsService {
100100
let pluginDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, "tns_modules");
101101
let pluginData = this.convertToPluginData(dependencyData);
102102

103-
if(!this.isPluginDataValidForPlatform(pluginData, platform).wait()) {
103+
if (!this.isPluginDataValidForPlatform(pluginData, platform).wait()) {
104104
return;
105105
}
106106

107-
if(this.$fs.exists(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME)).wait()) {
107+
if (this.$fs.exists(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME)).wait()) {
108108
this.$fs.ensureDirectoryExists(pluginDestinationPath).wait();
109109
shelljs.cp("-Rf", pluginData.fullPath, pluginDestinationPath);
110110

@@ -126,7 +126,7 @@ export class PluginsService implements IPluginsService {
126126
let installedDependencies = this.$fs.exists(this.nodeModulesPath).wait() ? this.$fs.readDirectory(this.nodeModulesPath).wait() : [];
127127
let packageJsonContent = this.$fs.readJson(this.getPackageJsonFilePath()).wait();
128128
let allDependencies = _.keys(packageJsonContent.dependencies).concat(_.keys(packageJsonContent.devDependencies));
129-
if(this.$options.force || _.difference(allDependencies, installedDependencies).length) {
129+
if (this.$options.force || _.difference(allDependencies, installedDependencies).length) {
130130
this.$npm.install(this.$projectData.projectDir, this.$projectData.projectDir, { "ignore-scripts": this.$options.ignoreScripts }).wait();
131131
}
132132
}).future<void>()();
@@ -147,6 +147,33 @@ export class PluginsService implements IPluginsService {
147147
return this.executeForAllInstalledPlatforms(action);
148148
}
149149

150+
public getDependenciesFromPackageJson(): IFuture<IPackageJsonDepedenciesResult> {
151+
return (() => {
152+
let packageJson = this.$fs.readJson(this.getPackageJsonFilePath()).wait();
153+
let dependencies: IBasePluginData[] = this.mapDependenciesToBaseIPluginData(packageJson.dependencies);
154+
155+
let devDependencies: IBasePluginData[] = this.mapDependenciesToBaseIPluginData(packageJson.devDependencies);
156+
157+
return {
158+
dependencies,
159+
devDependencies
160+
};
161+
}).future<IPackageJsonDepedenciesResult>()();
162+
}
163+
164+
private mapDependenciesToBaseIPluginData(dependencies: any): IBasePluginData[] {
165+
let result: IBasePluginData[] = [];
166+
167+
_.forEach(_.keys(dependencies), (key: string) => {
168+
result.push({
169+
name: key,
170+
version: dependencies[key]
171+
});
172+
});
173+
174+
return result;
175+
}
176+
150177
private get nodeModulesPath(): string {
151178
return path.join(this.$projectData.projectDir, "node_modules");
152179
}
@@ -166,7 +193,7 @@ export class PluginsService implements IPluginsService {
166193

167194
private getNodeModuleData(module: string): IFuture<INodeModuleData> { // module can be modulePath or moduleName
168195
return (() => {
169-
if(!this.$fs.exists(module).wait() || path.basename(module) !== "package.json") {
196+
if (!this.$fs.exists(module).wait() || path.basename(module) !== "package.json") {
170197
module = this.getPackageJsonFilePathForModule(module);
171198
}
172199

@@ -190,7 +217,7 @@ export class PluginsService implements IPluginsService {
190217
pluginData.pluginPlatformsFolderPath = (platform: string) => path.join(pluginData.fullPath, "platforms", platform);
191218
let data = cacheData.nativescript || cacheData.moduleInfo;
192219

193-
if(pluginData.isPlugin) {
220+
if (pluginData.isPlugin) {
194221
pluginData.platformsData = data.platforms;
195222
pluginData.pluginVariables = data.variables;
196223
}
@@ -218,11 +245,11 @@ export class PluginsService implements IPluginsService {
218245
return (() => {
219246
let result = "";
220247

221-
if(npmCommandName === PluginsService.INSTALL_COMMAND_NAME) {
248+
if (npmCommandName === PluginsService.INSTALL_COMMAND_NAME) {
222249
result = this.$npm.install(npmCommandArguments, this.$projectData.projectDir, PluginsService.NPM_CONFIG).wait();
223-
} else if(npmCommandName === PluginsService.UNINSTALL_COMMAND_NAME) {
250+
} else if (npmCommandName === PluginsService.UNINSTALL_COMMAND_NAME) {
224251
result = this.$npm.uninstall(npmCommandArguments, PluginsService.NPM_CONFIG, this.$projectData.projectDir).wait();
225-
if(!result || !result.length) {
252+
if (!result || !result.length) {
226253
// indicates something's wrong with the data in package.json, for example version of the plugin that we are trying to remove is invalid.
227254
return npmCommandArguments.toLowerCase();
228255
}
@@ -241,7 +268,7 @@ export class PluginsService implements IPluginsService {
241268
let availablePlatforms = _.keys(this.$platformsData.availablePlatforms);
242269
_.each(availablePlatforms, platform => {
243270
let isPlatformInstalled = this.$fs.exists(path.join(this.$projectData.platformsDir, platform.toLowerCase())).wait();
244-
if(isPlatformInstalled) {
271+
if (isPlatformInstalled) {
245272
let platformData = this.$platformsData.getPlatformData(platform.toLowerCase());
246273
let pluginDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, "tns_modules");
247274
action(pluginDestinationPath, platform.toLowerCase(), platformData).wait();
@@ -265,12 +292,12 @@ export class PluginsService implements IPluginsService {
265292

266293
let installedFrameworkVersion = this.getInstalledFrameworkVersion(platform).wait();
267294
let pluginPlatformsData = pluginData.platformsData;
268-
if(pluginPlatformsData) {
295+
if (pluginPlatformsData) {
269296
let pluginVersion = (<any>pluginPlatformsData)[platform];
270-
if(!pluginVersion) {
297+
if (!pluginVersion) {
271298
this.$logger.warn(`${pluginData.name} is not supported for ${platform}.`);
272299
isValid = false;
273-
} else if(semver.gt(pluginVersion, installedFrameworkVersion)) {
300+
} else if (semver.gt(pluginVersion, installedFrameworkVersion)) {
274301
this.$logger.warn(`${pluginData.name} ${pluginVersion} for ${platform} is not compatible with the currently installed framework version ${installedFrameworkVersion}.`);
275302
isValid = false;
276303
}

0 commit comments

Comments
 (0)