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