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