From 28ddc2f56ec67c5152a4a1b64ee3e94d8a571aa3 Mon Sep 17 00:00:00 2001 From: Fatme Havaluova Date: Tue, 23 Jun 2015 18:13:03 +0300 Subject: [PATCH] Implement install command https://github.com/NativeScript/nativescript-cli/issues/587 --- bin/nativescript.js | 0 lib/bootstrap.ts | 3 +- lib/commands/install.ts | 84 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) mode change 100644 => 100755 bin/nativescript.js create mode 100644 lib/commands/install.ts diff --git a/bin/nativescript.js b/bin/nativescript.js old mode 100644 new mode 100755 diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index 7047dfe7d2..00e03f8d5e 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -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"); \ No newline at end of file +$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin"); +$injector.requireCommand("install", "./commands/install"); \ No newline at end of file diff --git a/lib/commands/install.ts b/lib/commands/install.ts new file mode 100644 index 0000000000..6a718077be --- /dev/null +++ b/lib/commands/install.ts @@ -0,0 +1,84 @@ +/// +"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 { + return (() => { + let projectFilePath = this.getProjectFilePath(args[0]); + let projectData = this.getProjectData(projectFilePath).wait(); + let projectName = projectData.id.split(".")[2]; + + 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()(); + } + + public canExecute(args: string[]): IFuture { + 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()(); + } + + 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 { + 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()(); + } +} +$injector.registerCommand("install", InstallCommand); \ No newline at end of file