1
1
import * as constants from "./constants" ;
2
2
import * as path from "path" ;
3
+ import { parseJson } from "./common/helpers" ;
3
4
import { EOL } from "os" ;
4
5
5
6
interface IProjectType {
@@ -32,10 +33,9 @@ export class ProjectData implements IProjectData {
32
33
public projectFilePath : string ;
33
34
public projectId : string ;
34
35
public projectName : string ;
36
+ public nsConfig : any ;
35
37
public appDirectoryPath : string ;
36
- get appResourcesDirectoryPath ( ) : string {
37
- return this . getAppResourcesDirectoryPath ( ) ;
38
- }
38
+ public appResourcesDirectoryPath : string ;
39
39
public dependencies : any ;
40
40
public devDependencies : IStringDictionary ;
41
41
public projectType : string ;
@@ -49,45 +49,81 @@ export class ProjectData implements IProjectData {
49
49
50
50
public initializeProjectData ( projectDir ?: string ) : void {
51
51
projectDir = projectDir || this . $projectHelper . projectDir ;
52
+
52
53
// If no project found, projectDir should be null
53
54
if ( projectDir ) {
54
- const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
55
- let data : any = null ;
55
+ const projectFilePath = this . getProjectFilePath ( projectDir ) ;
56
56
57
57
if ( this . $fs . exists ( projectFilePath ) ) {
58
- let fileContent : any = null ;
59
- try {
60
- fileContent = this . $fs . readJson ( projectFilePath ) ;
61
- data = fileContent [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] ;
62
- } catch ( err ) {
63
- this . $errors . failWithoutHelp ( `The project file ${ this . projectFilePath } is corrupted. ${ EOL } ` +
64
- `Consider restoring an earlier version from your source control or backup.${ EOL } ` +
65
- `Additional technical info: ${ err . toString ( ) } ` ) ;
66
- }
67
-
68
- if ( data ) {
69
- this . projectDir = projectDir ;
70
- this . projectName = this . $projectHelper . sanitizeName ( path . basename ( projectDir ) ) ;
71
- this . platformsDir = path . join ( projectDir , constants . PLATFORMS_DIR_NAME ) ;
72
- this . projectFilePath = projectFilePath ;
73
- this . appDirectoryPath = path . join ( projectDir , constants . APP_FOLDER_NAME ) ;
74
- this . projectId = data . id ;
75
- this . dependencies = fileContent . dependencies ;
76
- this . devDependencies = fileContent . devDependencies ;
77
- this . projectType = this . getProjectType ( ) ;
78
-
79
- return ;
80
- }
58
+ let packageJsonContent : any = null ;
59
+ packageJsonContent = this . $fs . readText ( projectFilePath ) ;
60
+ const nsConfigContent : any = this . getNsConfigContent ( projectDir ) ;
61
+
62
+ this . initializeProjectDataFromContent ( packageJsonContent , nsConfigContent , projectDir ) ;
81
63
}
64
+
65
+ return ;
66
+ }
67
+
68
+ this . errorInvalidProject ( projectDir ) ;
69
+ }
70
+
71
+ public initializeProjectDataFromContent ( packageJsonContent : string , nsconfigContent : string , projectDir ?: string ) : void {
72
+ projectDir = projectDir || this . $projectHelper . projectDir || "" ;
73
+ const projectFilePath = this . getProjectFilePath ( projectDir ) ;
74
+ // If no project found, projectDir should be null
75
+ let nsData : any = null ;
76
+ let nsConfig : any = null ;
77
+ let packageJsonData : any = null ;
78
+
79
+ try {
80
+ packageJsonData = parseJson ( packageJsonContent ) ;
81
+ nsData = packageJsonData [ this . $staticConfig . CLIENT_NAME_KEY_IN_PROJECT_FILE ] ;
82
+ } catch ( err ) {
83
+ this . $errors . failWithoutHelp ( `The project file ${ this . projectFilePath } is corrupted. ${ EOL } ` +
84
+ `Consider restoring an earlier version from your source control or backup.${ EOL } ` +
85
+ `Additional technical info: ${ err . toString ( ) } ` ) ;
82
86
}
83
87
88
+ try {
89
+ nsConfig = nsconfigContent ? parseJson ( nsconfigContent ) : null ;
90
+ } catch ( err ) {
91
+ this . $errors . failWithoutHelp ( `The NativeScript configuration file ${ constants . CONFIG_NS_FILE_NAME } is corrupted. ${ EOL } ` +
92
+ `Consider restoring an earlier version from your source control or backup.${ EOL } ` +
93
+ `Additional technical info: ${ err . toString ( ) } ` ) ;
94
+ }
95
+
96
+ if ( nsData ) {
97
+ this . projectDir = projectDir ;
98
+ this . projectName = this . $projectHelper . sanitizeName ( path . basename ( projectDir ) ) ;
99
+ this . platformsDir = path . join ( projectDir , constants . PLATFORMS_DIR_NAME ) ;
100
+ this . projectFilePath = projectFilePath ;
101
+ this . projectId = nsData . id ;
102
+ this . dependencies = packageJsonData . dependencies ;
103
+ this . devDependencies = packageJsonData . devDependencies ;
104
+ this . projectType = this . getProjectType ( ) ;
105
+ this . nsConfig = nsConfig ;
106
+ this . appDirectoryPath = this . getAppDirectoryPath ( ) ;
107
+ this . appResourcesDirectoryPath = this . getAppResourcesDirectoryPath ( ) ;
108
+
109
+ return ;
110
+ }
111
+
112
+ this . errorInvalidProject ( projectDir ) ;
113
+ }
114
+
115
+ private errorInvalidProject ( projectDir : string ) : void {
84
116
const currentDir = path . resolve ( "." ) ;
85
117
this . $logger . trace ( `Unable to find project. projectDir: ${ projectDir } , options.path: ${ this . $options . path } , ${ currentDir } ` ) ;
86
118
87
119
// This is the case when no project file found
88
120
this . $errors . fail ( "No project found at or above '%s' and neither was a --path specified." , projectDir || this . $options . path || currentDir ) ;
89
121
}
90
122
123
+ private getProjectFilePath ( projectDir : string ) : string {
124
+ return path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
125
+ }
126
+
91
127
public getAppResourcesDirectoryPath ( projectDir ?: string ) : string {
92
128
if ( ! projectDir ) {
93
129
projectDir = this . projectDir ;
@@ -97,23 +133,45 @@ export class ProjectData implements IProjectData {
97
133
return null ;
98
134
}
99
135
100
- const configNSFilePath = path . join ( projectDir , constants . CONFIG_NS_FILE_NAME ) ;
101
- let absoluteAppResourcesDirPath : string ;
136
+ return path . resolve ( projectDir , this . getAppResourcesRelativeDirectoryPath ( ) ) ;
137
+ }
102
138
103
- if ( this . $fs . exists ( configNSFilePath ) ) {
104
- const configNS = this . $fs . readJson ( configNSFilePath ) ;
139
+ public getAppResourcesRelativeDirectoryPath ( ) : string {
140
+ if ( this . nsConfig && this . nsConfig [ constants . CONFIG_NS_APP_RESOURCES_ENTRY ] ) {
141
+ return this . nsConfig [ constants . CONFIG_NS_APP_RESOURCES_ENTRY ] ;
142
+ }
143
+
144
+ return path . join ( this . getAppDirectoryRelativePath ( ) , constants . APP_RESOURCES_FOLDER_NAME ) ;
145
+ }
105
146
106
- if ( configNS && configNS [ constants . CONFIG_NS_APP_RESOURCES_ENTRY ] ) {
107
- const appResourcesDirPath = configNS [ constants . CONFIG_NS_APP_RESOURCES_ENTRY ] ;
147
+ public getAppDirectoryPath ( projectDir ?: string ) : string {
148
+ if ( ! projectDir ) {
149
+ projectDir = this . projectDir ;
150
+ }
108
151
109
- absoluteAppResourcesDirPath = path . resolve ( projectDir , appResourcesDirPath ) ;
152
+ if ( ! projectDir ) {
153
+ return null ;
154
+ }
110
155
111
- return absoluteAppResourcesDirPath ;
112
- }
156
+ return path . resolve ( projectDir , this . getAppDirectoryRelativePath ( ) ) ;
157
+ }
158
+
159
+ public getAppDirectoryRelativePath ( ) : string {
160
+ if ( this . nsConfig && this . nsConfig [ constants . CONFIG_NS_APP_ENTRY ] ) {
161
+ return this . nsConfig [ constants . CONFIG_NS_APP_ENTRY ] ;
162
+ }
163
+
164
+ return constants . APP_FOLDER_NAME ;
165
+ }
166
+
167
+ private getNsConfigContent ( projectDir : string ) : string {
168
+ const configNSFilePath = path . join ( projectDir , constants . CONFIG_NS_FILE_NAME ) ;
169
+
170
+ if ( ! this . $fs . exists ( configNSFilePath ) ) {
171
+ return null ;
113
172
}
114
173
115
- // if no nsconfig is present default to app/App_Resources
116
- return path . join ( projectDir , constants . APP_FOLDER_NAME , constants . APP_RESOURCES_FOLDER_NAME ) ;
174
+ return this . $fs . readText ( configNSFilePath ) ;
117
175
}
118
176
119
177
private getProjectType ( ) : string {
0 commit comments