11import * as constants from "./constants" ;
22import * as path from "path" ;
3+ import { parseJson } from "./common/helpers" ;
34import { EOL } from "os" ;
45
56interface IProjectType {
@@ -32,10 +33,9 @@ export class ProjectData implements IProjectData {
3233 public projectFilePath : string ;
3334 public projectId : string ;
3435 public projectName : string ;
36+ public nsConfig : any ;
3537 public appDirectoryPath : string ;
36- get appResourcesDirectoryPath ( ) : string {
37- return this . getAppResourcesDirectoryPath ( ) ;
38- }
38+ public appResourcesDirectoryPath : string ;
3939 public dependencies : any ;
4040 public devDependencies : IStringDictionary ;
4141 public projectType : string ;
@@ -49,45 +49,81 @@ export class ProjectData implements IProjectData {
4949
5050 public initializeProjectData ( projectDir ?: string ) : void {
5151 projectDir = projectDir || this . $projectHelper . projectDir ;
52+
5253 // If no project found, projectDir should be null
5354 if ( projectDir ) {
54- const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
55- let data : any = null ;
55+ const projectFilePath = this . getProjectFilePath ( projectDir ) ;
5656
5757 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 ) ;
8163 }
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 ( ) } ` ) ;
8286 }
8387
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 {
84116 const currentDir = path . resolve ( "." ) ;
85117 this . $logger . trace ( `Unable to find project. projectDir: ${ projectDir } , options.path: ${ this . $options . path } , ${ currentDir } ` ) ;
86118
87119 // This is the case when no project file found
88120 this . $errors . fail ( "No project found at or above '%s' and neither was a --path specified." , projectDir || this . $options . path || currentDir ) ;
89121 }
90122
123+ private getProjectFilePath ( projectDir : string ) : string {
124+ return path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
125+ }
126+
91127 public getAppResourcesDirectoryPath ( projectDir ?: string ) : string {
92128 if ( ! projectDir ) {
93129 projectDir = this . projectDir ;
@@ -97,23 +133,45 @@ export class ProjectData implements IProjectData {
97133 return null ;
98134 }
99135
100- const configNSFilePath = path . join ( projectDir , constants . CONFIG_NS_FILE_NAME ) ;
101- let absoluteAppResourcesDirPath : string ;
136+ return path . resolve ( projectDir , this . getAppResourcesRelativeDirectoryPath ( ) ) ;
137+ }
102138
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+ }
105146
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+ }
108151
109- absoluteAppResourcesDirPath = path . resolve ( projectDir , appResourcesDirPath ) ;
152+ if ( ! projectDir ) {
153+ return null ;
154+ }
110155
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 ;
113172 }
114173
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 ) ;
117175 }
118176
119177 private getProjectType ( ) : string {
0 commit comments