11import * as constants from "./constants" ;
22import * as path from "path" ;
3+ import { parseJson } from "./common/helpers" ;
34import { EOL } from "os" ;
45
56interface IProjectType {
@@ -32,6 +33,7 @@ export class ProjectData implements IProjectData {
3233 public projectFilePath : string ;
3334 public projectId : string ;
3435 public projectName : string ;
36+ public nsConfig : any ;
3537 get appDirectoryPath ( ) : string {
3638 return this . getAppDirectoryPath ( ) ;
3739 }
@@ -51,86 +53,119 @@ export class ProjectData implements IProjectData {
5153
5254 public initializeProjectData ( projectDir ?: string ) : void {
5355 projectDir = projectDir || this . $projectHelper . projectDir ;
56+
5457 // If no project found, projectDir should be null
5558 if ( projectDir ) {
56- const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
57- let data : any = null ;
59+ const projectFilePath = this . getProjectFilePath ( projectDir ) ;
5860
5961 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 ) ;
8267 }
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 ( ) } ` ) ;
8398 }
8499
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 ) : void {
85118 const currentDir = path . resolve ( "." ) ;
86119 this . $logger . trace ( `Unable to find project. projectDir: ${ projectDir } , options.path: ${ this . $options . path } , ${ currentDir } ` ) ;
87120
88121 // This is the case when no project file found
89122 this . $errors . fail ( "No project found at or above '%s' and neither was a --path specified." , projectDir || this . $options . path || currentDir ) ;
90123 }
91124
125+ private getProjectFilePath ( projectDir : string ) : string {
126+ return path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
127+ }
128+
92129 public getAppResourcesDirectoryPath ( projectDir ?: string ) : string {
93130 if ( ! projectDir ) {
94131 projectDir = this . projectDir ;
95132 }
96133
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+ }
102136
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 ] ;
104140 }
105141
106- return absoluteAppResourcesDirPath || path . join ( this . getAppDirectoryPath ( projectDir ) , constants . APP_RESOURCES_FOLDER_NAME ) ;
142+ return path . join ( this . getAppDirectoryRelativePath ( ) , constants . APP_RESOURCES_FOLDER_NAME ) ;
107143 }
108144
109145 public getAppDirectoryPath ( projectDir ?: string ) : string {
110146 if ( ! projectDir ) {
111147 projectDir = this . projectDir ;
112148 }
113149
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+ }
119152
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 ] ;
121156 }
122157
123- return absoluteAppDirPath || path . join ( projectDir , constants . APP_FOLDER_NAME ) ;
158+ return constants . APP_FOLDER_NAME ;
124159 }
125160
126- private getNsConfig ( projectDir : string ) : Object {
161+ private getNsConfigContent ( projectDir : string ) : string {
127162 const configNSFilePath = path . join ( projectDir , constants . CONFIG_NS_FILE_NAME ) ;
128163
129164 if ( ! this . $fs . exists ( configNSFilePath ) ) {
130165 return null ;
131166 }
132167
133- return this . $fs . readJson ( configNSFilePath ) ;
168+ return this . $fs . readText ( configNSFilePath ) ;
134169 }
135170
136171 private getProjectType ( ) : string {
0 commit comments