Skip to content

Commit e6abdc7

Browse files
Fatme HavaluovaFatme Havaluova
Fatme Havaluova
authored and
Fatme Havaluova
committed
Implement install command #587
1 parent 3061fcd commit e6abdc7

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

bin/nativescript.js

100644100755
File mode changed.

lib/bootstrap.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ $injector.require("broccoliPluginWrapper", "./tools/broccoli/broccoli-plugin-wra
5858

5959
$injector.require("pluginsService", "./services/plugins-service");
6060
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
61-
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
61+
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
62+
$injector.requireCommand("install", "./commands/install");

lib/commands/install.ts

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
///<reference path="../.d.ts"/>
2+
"use strict";
3+
4+
import path = require("path");
5+
6+
export class InstallCommand implements ICommand {
7+
private _projectFilePath: string;
8+
9+
constructor(private $fs: IFileSystem,
10+
private $errors: IErrors,
11+
private $logger: ILogger,
12+
private $options: IOptions,
13+
private $injector: IInjector,
14+
private $staticConfig: IStaticConfig) { }
15+
16+
public enableHooks = false;
17+
18+
public allowedParameters: ICommandParameter[] = [];
19+
20+
public execute(args: string[]): IFuture<void> {
21+
return (() => {
22+
let projectFilePath = this.getProjectFilePath(args[0]);
23+
let projectData = this.getProjectData(projectFilePath).wait();
24+
let projectName = projectData.id.split(".")[2];
25+
26+
this.$injector.resolve("projectService").createProject(projectName).wait();
27+
28+
this.$options.path = path.join(this.$options.path || path.resolve("."), projectName);
29+
30+
this.$logger.info("Adding platforms...");
31+
32+
let $platformsData = this.$injector.resolve("platformsData");
33+
let $platformService = this.$injector.resolve("platformService");
34+
_.each($platformsData.platformsNames, platform => {
35+
let platformData = $platformsData.getPlatformData(platform);
36+
let frameworkPackageData = projectData[platformData.frameworkPackageName];
37+
if(frameworkPackageData && frameworkPackageData.version) {
38+
$platformService.addPlatforms([`${platform}@${frameworkPackageData.version}`]).wait();
39+
}
40+
});
41+
42+
}).future<void>()();
43+
}
44+
45+
public canExecute(args: string[]): IFuture<boolean> {
46+
return (() => {
47+
if(!args[0]) {
48+
this.$errors.failWithoutHelp("You must specify the path to package.json file.");
49+
}
50+
51+
let projectFilePath = this.getProjectFilePath(args[0]);
52+
if(!this.$fs.exists(projectFilePath).wait()) {
53+
this.$errors.failWithoutHelp("The provided path doesn't contain package.json.");
54+
}
55+
56+
let projectData = this.getProjectData(projectFilePath).wait();
57+
if(!projectData) {
58+
this.$errors.failWithoutHelp("Invalid project file. Verify that the specified package.json file contains a nativescript key and try again.");
59+
}
60+
61+
return true;
62+
}).future<boolean>()();
63+
}
64+
65+
private getProjectFilePath(providedPath: string): string {
66+
if(!this._projectFilePath) {
67+
providedPath = path.resolve(providedPath);
68+
this._projectFilePath = path.basename(providedPath) === "package.json" ? providedPath : path.join(providedPath, "package.json");
69+
}
70+
71+
return this._projectFilePath;
72+
}
73+
74+
private getProjectData(projectFilePath: string): IFuture<any> {
75+
return (() => {
76+
let fileContent = this.$fs.readJson(projectFilePath).wait();
77+
let projectData = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
78+
79+
return projectData;
80+
}).future<any>()();
81+
}
82+
}
83+
$injector.registerCommand("install", InstallCommand);

0 commit comments

Comments
 (0)