@@ -46,8 +46,8 @@ export class ProjectService implements IProjectService {
4646 }
4747
4848 try {
49- const templatePath = await this . $projectTemplatesService . prepareTemplate ( selectedTemplate , projectDir ) ;
50- await this . extractTemplate ( projectDir , templatePath ) ;
49+ const { templatePath, templateVersion } = await this . $projectTemplatesService . prepareTemplate ( selectedTemplate , projectDir ) ;
50+ await this . extractTemplate ( projectDir , templatePath , templateVersion ) ;
5151
5252 await this . ensureAppResourcesExist ( projectDir ) ;
5353
@@ -57,17 +57,24 @@ export class ProjectService implements IProjectService {
5757 await this . $npmInstallationManager . install ( constants . TNS_CORE_MODULES_NAME , projectDir , { dependencyType : "save" } ) ;
5858 }
5959
60- this . mergeProjectAndTemplateProperties ( projectDir , templatePackageJsonData ) ; //merging dependencies from template (dev && prod)
61- this . removeMergedDependencies ( projectDir , templatePackageJsonData ) ;
60+ if ( templateVersion === constants . TemplateVersions . v1 ) {
61+ this . mergeProjectAndTemplateProperties ( projectDir , templatePackageJsonData ) ; // merging dependencies from template (dev && prod)
62+ this . removeMergedDependencies ( projectDir , templatePackageJsonData ) ;
63+ }
64+
65+ const templatePackageJson = this . $fs . readJson ( path . join ( templatePath , constants . PACKAGE_JSON_FILE_NAME ) ) ;
6266
67+ // Install devDependencies and execute all scripts:
6368 await this . $npm . install ( projectDir , projectDir , {
6469 disableNpmInstall : false ,
6570 frameworkPath : null ,
6671 ignoreScripts : projectOptions . ignoreScripts
6772 } ) ;
6873
69- const templatePackageJson = this . $fs . readJson ( path . join ( templatePath , "package.json" ) ) ;
7074 await this . $npm . uninstall ( templatePackageJson . name , { save : true } , projectDir ) ;
75+ if ( templateVersion === constants . TemplateVersions . v2 ) {
76+ this . alterPackageJsonData ( projectDir , projectId ) ;
77+ }
7178 } catch ( err ) {
7279 this . $fs . deleteDirectory ( projectDir ) ;
7380 throw err ;
@@ -99,21 +106,32 @@ export class ProjectService implements IProjectService {
99106 return null ;
100107 }
101108
102- private async extractTemplate ( projectDir : string , realTemplatePath : string ) : Promise < void > {
109+ private async extractTemplate ( projectDir : string , realTemplatePath : string , templateVersion : string ) : Promise < void > {
103110 this . $fs . ensureDirectoryExists ( projectDir ) ;
104111
105- const appDestinationPath = this . $projectData . getAppDirectoryPath ( projectDir ) ;
106- this . $fs . createDirectory ( appDestinationPath ) ;
112+ this . $logger . trace ( `Template version is ${ templateVersion } ` ) ;
113+ let destinationDir = "" ;
114+ switch ( templateVersion ) {
115+ case constants . TemplateVersions . v2 :
116+ destinationDir = projectDir ;
117+ break ;
118+ case constants . TemplateVersions . v1 :
119+ default :
120+ const appDestinationPath = this . $projectData . getAppDirectoryPath ( projectDir ) ;
121+ this . $fs . createDirectory ( appDestinationPath ) ;
122+ destinationDir = appDestinationPath ;
123+ break ;
124+ }
107125
108- this . $logger . trace ( `Copying application from '${ realTemplatePath } ' into '${ appDestinationPath } '.` ) ;
109- shelljs . cp ( '-R' , path . join ( realTemplatePath , "*" ) , appDestinationPath ) ;
126+ this . $logger . trace ( `Copying application from '${ realTemplatePath } ' into '${ destinationDir } '.` ) ;
127+ shelljs . cp ( '-R' , path . join ( realTemplatePath , "*" ) , destinationDir ) ;
110128
111129 this . $fs . createDirectory ( path . join ( projectDir , "platforms" ) ) ;
112130 }
113131
114132 private async ensureAppResourcesExist ( projectDir : string ) : Promise < void > {
115- const appPath = this . $projectData . getAppDirectoryPath ( projectDir ) ,
116- appResourcesDestinationPath = this . $projectData . getAppResourcesDirectoryPath ( projectDir ) ;
133+ const appPath = this . $projectData . getAppDirectoryPath ( projectDir ) ;
134+ const appResourcesDestinationPath = this . $projectData . getAppResourcesDirectoryPath ( projectDir ) ;
117135
118136 if ( ! this . $fs . exists ( appResourcesDestinationPath ) ) {
119137 this . $fs . createDirectory ( appResourcesDestinationPath ) ;
@@ -127,11 +145,20 @@ export class ProjectService implements IProjectService {
127145 ignoreScripts : false
128146 } ) ;
129147
130- const defaultTemplateAppResourcesPath = path . join ( projectDir , constants . NODE_MODULES_FOLDER_NAME ,
131- defaultTemplateName , constants . APP_RESOURCES_FOLDER_NAME ) ;
148+ const obsoleteAppResourcesPath = path . join ( projectDir ,
149+ constants . NODE_MODULES_FOLDER_NAME ,
150+ defaultTemplateName ,
151+ constants . APP_RESOURCES_FOLDER_NAME ) ;
152+
153+ const defaultTemplateAppResourcesPath = path . join ( projectDir ,
154+ constants . NODE_MODULES_FOLDER_NAME ,
155+ defaultTemplateName ,
156+ constants . APP_FOLDER_NAME ,
157+ constants . APP_RESOURCES_FOLDER_NAME ) ;
132158
133- if ( this . $fs . exists ( defaultTemplateAppResourcesPath ) ) {
134- shelljs . cp ( '-R' , defaultTemplateAppResourcesPath , appPath ) ;
159+ const pathToAppResources = this . $fs . exists ( defaultTemplateAppResourcesPath ) ? defaultTemplateAppResourcesPath : obsoleteAppResourcesPath ;
160+ if ( this . $fs . exists ( pathToAppResources ) ) {
161+ shelljs . cp ( '-R' , pathToAppResources , appPath ) ;
135162 }
136163
137164 await this . $npm . uninstall ( defaultTemplateName , { save : true } , projectDir ) ;
@@ -187,13 +214,38 @@ export class ProjectService implements IProjectService {
187214 private createPackageJson ( projectDir : string , projectId : string ) : void {
188215 const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
189216
190- this . $fs . writeJson ( projectFilePath , {
191- "description" : "NativeScript Application" ,
192- "license" : "SEE LICENSE IN <your-license-filename>" ,
193- "readme" : "NativeScript Application" ,
194- "repository" : "<fill-your-repository-here>"
195- } ) ;
217+ this . $fs . writeJson ( projectFilePath , this . packageJsonDefaultData ) ;
218+
219+ this . setAppId ( projectDir , projectId ) ;
220+ }
221+
222+ private get packageJsonDefaultData ( ) : IStringDictionary {
223+ return {
224+ description : "NativeScript Application" ,
225+ license : "SEE LICENSE IN <your-license-filename>" ,
226+ readme : "NativeScript Application" ,
227+ repository : "<fill-your-repository-here>"
228+ } ;
229+ }
230+
231+ private alterPackageJsonData ( projectDir : string , projectId : string ) : void {
232+ const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
233+
234+ const packageJsonData = this . $fs . readJson ( projectFilePath ) ;
235+
236+ // Remove the metadata keys from the package.json
237+ let updatedPackageJsonData = _ . omitBy < any , any > ( packageJsonData , ( value : any , key : string ) => _ . startsWith ( key , "_" ) ) ;
238+ updatedPackageJsonData = _ . merge ( updatedPackageJsonData , this . packageJsonDefaultData ) ;
239+
240+ if ( updatedPackageJsonData . nativescript && updatedPackageJsonData . nativescript . templateVersion ) {
241+ delete updatedPackageJsonData . nativescript . templateVersion ;
242+ }
243+
244+ this . $fs . writeJson ( projectFilePath , updatedPackageJsonData ) ;
245+ this . setAppId ( projectDir , projectId ) ;
246+ }
196247
248+ private setAppId ( projectDir : string , projectId : string ) : void {
197249 this . $projectDataService . setNSValue ( projectDir , "id" , projectId ) ;
198250 }
199251}
0 commit comments