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