Skip to content

Implement install command #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/nativescript.js
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ $injector.require("broccoliPluginWrapper", "./tools/broccoli/broccoli-plugin-wra

$injector.require("pluginsService", "./services/plugins-service");
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
$injector.requireCommand("install", "./commands/install");
84 changes: 84 additions & 0 deletions lib/commands/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
///<reference path="../.d.ts"/>
"use strict";

import path = require("path");

export class InstallCommand implements ICommand {
private _projectData: any;

constructor(private $fs: IFileSystem,
private $errors: IErrors,
private $logger: ILogger,
private $options: IOptions,
private $injector: IInjector,
private $staticConfig: IStaticConfig) { }

public enableHooks = false;

public allowedParameters: ICommandParameter[] = [];

public execute(args: string[]): IFuture<void> {
return (() => {
let projectFilePath = this.getProjectFilePath(args[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can cache the result of this.getProjectFilePath(args[0]) in a variable, as you have already read the file and its content in canExecute.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an even better solution is to cache the contents of package.json (or the nativescript key) inside getProjectData.

let projectData = this.getProjectData(projectFilePath).wait();
let projectName = projectData.id.split(".")[2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider validating projectData.id && projectName, because package.json is under user control and can have invalid content.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should validate that projectData has id. I've tried removing it from my package.json and the result is:

$ tns install .
Cannot read property 'split' of undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it in canExecute function.


this.$injector.resolve("projectService").createProject(projectName).wait();

this.$options.path = path.join(this.$options.path || path.resolve("."), projectName);

this.$logger.info("Adding platforms...");

let $platformsData = this.$injector.resolve("platformsData");
let $platformService = this.$injector.resolve("platformService");
_.each($platformsData.platformsNames, platform => {
let platformData = $platformsData.getPlatformData(platform);
let frameworkPackageData = projectData[platformData.frameworkPackageName];
if(frameworkPackageData && frameworkPackageData.version) {
$platformService.addPlatforms([`${platform}@${frameworkPackageData.version}`]).wait();
}
});

}).future<void>()();
}

public canExecute(args: string[]): IFuture<boolean> {
return (() => {
let projectFilePath = this.getProjectFilePath(args[0]);
let errorMessage = args[0] ? "The provided path doesn't contain package.json." :
"The current directory doesn't contain package.json file. Execute the command in directory which contains package.json file or specify the path to package.json file.";

if(!this.$fs.exists(projectFilePath).wait()) {
this.$errors.failWithoutHelp(errorMessage);
}

let projectData = this.getProjectData(projectFilePath).wait();
if(!projectData) {
this.$errors.failWithoutHelp("Invalid project file. Verify that the specified package.json file contains a nativescript key and try again.");
}

if(!projectData.id) {
this.$errors.failWithoutHelp("Invalid project file. Verify that the specified package.json file contains an id and try again.");
}

return true;
}).future<boolean>()();
}

private getProjectFilePath(providedPath: string): string {
let resolvedPath = path.resolve(providedPath || ".");
return path.basename(resolvedPath) === "package.json" ? resolvedPath : path.join(resolvedPath, "package.json");
}

private getProjectData(projectFilePath: string): IFuture<any> {
return (() => {
if(!this._projectData) {
let fileContent = this.$fs.readJson(projectFilePath).wait();
this._projectData = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
}

return this._projectData;
}).future<any>()();
}
}
$injector.registerCommand("install", InstallCommand);