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