@@ -9,6 +9,7 @@ export class ProjectDataService implements IProjectDataService {
9
9
10
10
private projectFilePath : string ;
11
11
private projectData : IDictionary < any > ;
12
+ private projectFileIndent : string ;
12
13
13
14
constructor ( private $fs : IFileSystem ,
14
15
private $staticConfig : IStaticConfig ,
@@ -17,7 +18,7 @@ export class ProjectDataService implements IProjectDataService {
17
18
}
18
19
19
20
public initialize ( projectDir : string ) : void {
20
- if ( ! this . projectFilePath ) {
21
+ if ( ! this . projectFilePath ) {
21
22
this . projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
22
23
}
23
24
}
@@ -32,35 +33,35 @@ export class ProjectDataService implements IProjectDataService {
32
33
public setValue ( key : string , value : any ) : IFuture < void > {
33
34
return ( ( ) => {
34
35
this . loadProjectFile ( ) . wait ( ) ;
35
- if ( ! this . projectData [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] ) {
36
+ if ( ! this . projectData [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] ) {
36
37
this . projectData [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] = Object . create ( null ) ;
37
38
}
38
39
this . projectData [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] [ key ] = value ;
39
- this . $fs . writeJson ( this . projectFilePath , this . projectData , "\t" ) . wait ( ) ;
40
+ this . $fs . writeJson ( this . projectFilePath , this . projectData , this . projectFileIndent ) . wait ( ) ;
40
41
} ) . future < void > ( ) ( ) ;
41
42
}
42
43
43
44
public removeProperty ( propertyName : string ) : IFuture < void > {
44
45
return ( ( ) => {
45
46
this . loadProjectFile ( ) . wait ( ) ;
46
- delete this . projectData [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] [ propertyName ] ;
47
- this . $fs . writeJson ( this . projectFilePath , this . projectData , "\t" ) . wait ( ) ;
47
+ delete this . projectData [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] [ propertyName ] ;
48
+ this . $fs . writeJson ( this . projectFilePath , this . projectData , this . projectFileIndent ) . wait ( ) ;
48
49
} ) . future < void > ( ) ( ) ;
49
50
}
50
51
51
52
public removeDependency ( dependencyName : string ) : IFuture < void > {
52
53
return ( ( ) => {
53
54
this . loadProjectFile ( ) . wait ( ) ;
54
55
delete this . projectData [ ProjectDataService . DEPENDENCIES_KEY_NAME ] [ dependencyName ] ;
55
- this . $fs . writeJson ( this . projectFilePath , this . projectData , "\t" ) . wait ( ) ;
56
+ this . $fs . writeJson ( this . projectFilePath , this . projectData , this . projectFileIndent ) . wait ( ) ;
56
57
} ) . future < void > ( ) ( ) ;
57
58
}
58
59
59
60
private loadProjectFile ( ) : IFuture < void > {
60
61
return ( ( ) => {
61
62
assert . ok ( this . projectFilePath , "Initialize method of projectDataService is not called." ) ;
62
63
63
- if ( ! this . $fs . exists ( this . projectFilePath ) . wait ( ) ) {
64
+ if ( ! this . $fs . exists ( this . projectFilePath ) . wait ( ) ) {
64
65
this . $fs . writeJson ( this . projectFilePath , {
65
66
"description" : "NativeScript Application" ,
66
67
"license" : "SEE LICENSE IN <your-license-filename>" ,
@@ -69,8 +70,22 @@ export class ProjectDataService implements IProjectDataService {
69
70
} ) . wait ( ) ;
70
71
}
71
72
72
- this . projectData = this . $fs . readJson ( this . projectFilePath ) . wait ( ) || Object . create ( null ) ;
73
+ // Detect indent and use it later to write JSON.
74
+ let projectFileContent = this . $fs . readText ( this . projectFilePath ) . wait ( ) ;
75
+
76
+ this . projectFileIndent = projectFileContent ? this . detectIndent ( projectFileContent ) : "\t" ;
77
+
78
+ this . projectData = projectFileContent ? JSON . parse ( projectFileContent ) : Object . create ( null ) ;
79
+
73
80
} ) . future < void > ( ) ( ) ;
74
81
}
82
+
83
+ private detectIndent ( content : string ) : any {
84
+ const leadingSpace = content . match ( / ( ^ [ ] + ) \S / m) ;
85
+ if ( leadingSpace ) {
86
+ return leadingSpace [ 1 ] . length ;
87
+ }
88
+ return "\t" ;
89
+ }
75
90
}
76
91
$injector . register ( "projectDataService" , ProjectDataService ) ;
0 commit comments