@@ -46,8 +46,8 @@ export class ProjectService implements IProjectService {
46
46
}
47
47
48
48
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 ) ;
51
51
52
52
await this . ensureAppResourcesExist ( projectDir ) ;
53
53
@@ -57,17 +57,24 @@ export class ProjectService implements IProjectService {
57
57
await this . $npmInstallationManager . install ( constants . TNS_CORE_MODULES_NAME , projectDir , { dependencyType : "save" } ) ;
58
58
}
59
59
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 ) ) ;
62
66
67
+ // Install devDependencies and execute all scripts:
63
68
await this . $npm . install ( projectDir , projectDir , {
64
69
disableNpmInstall : false ,
65
70
frameworkPath : null ,
66
71
ignoreScripts : projectOptions . ignoreScripts
67
72
} ) ;
68
73
69
- const templatePackageJson = this . $fs . readJson ( path . join ( templatePath , "package.json" ) ) ;
70
74
await this . $npm . uninstall ( templatePackageJson . name , { save : true } , projectDir ) ;
75
+ if ( templateVersion === constants . TemplateVersions . v2 ) {
76
+ this . alterPackageJsonData ( projectDir , projectId ) ;
77
+ }
71
78
} catch ( err ) {
72
79
this . $fs . deleteDirectory ( projectDir ) ;
73
80
throw err ;
@@ -99,21 +106,31 @@ export class ProjectService implements IProjectService {
99
106
return null ;
100
107
}
101
108
102
- private async extractTemplate ( projectDir : string , realTemplatePath : string ) : Promise < void > {
109
+ private async extractTemplate ( projectDir : string , realTemplatePath : string , templateVersion : string ) : Promise < void > {
103
110
this . $fs . ensureDirectoryExists ( projectDir ) ;
104
111
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
+ }
107
125
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 ) ;
110
128
111
129
this . $fs . createDirectory ( path . join ( projectDir , "platforms" ) ) ;
112
130
}
113
131
114
132
private async ensureAppResourcesExist ( projectDir : string ) : Promise < void > {
115
- const appPath = this . $projectData . getAppDirectoryPath ( projectDir ) ,
116
- appResourcesDestinationPath = this . $projectData . getAppResourcesDirectoryPath ( projectDir ) ;
133
+ const appResourcesDestinationPath = this . $projectData . getAppResourcesDirectoryPath ( projectDir ) ;
117
134
118
135
if ( ! this . $fs . exists ( appResourcesDestinationPath ) ) {
119
136
this . $fs . createDirectory ( appResourcesDestinationPath ) ;
@@ -127,11 +144,20 @@ export class ProjectService implements IProjectService {
127
144
ignoreScripts : false
128
145
} ) ;
129
146
130
- const defaultTemplateAppResourcesPath = path . join ( projectDir , constants . NODE_MODULES_FOLDER_NAME ,
131
- defaultTemplateName , constants . APP_RESOURCES_FOLDER_NAME ) ;
147
+ const obsoleteAppResourcesPath = path . join ( projectDir ,
148
+ constants . NODE_MODULES_FOLDER_NAME ,
149
+ defaultTemplateName ,
150
+ constants . APP_RESOURCES_FOLDER_NAME ) ;
151
+
152
+ const defaultTemplateAppResourcesPath = path . join ( projectDir ,
153
+ constants . NODE_MODULES_FOLDER_NAME ,
154
+ defaultTemplateName ,
155
+ constants . APP_FOLDER_NAME ,
156
+ constants . APP_RESOURCES_FOLDER_NAME ) ;
132
157
133
- if ( this . $fs . exists ( defaultTemplateAppResourcesPath ) ) {
134
- shelljs . cp ( '-R' , defaultTemplateAppResourcesPath , appPath ) ;
158
+ const pathToAppResources = this . $fs . exists ( defaultTemplateAppResourcesPath ) ? defaultTemplateAppResourcesPath : obsoleteAppResourcesPath ;
159
+ if ( this . $fs . exists ( pathToAppResources ) ) {
160
+ shelljs . cp ( '-R' , pathToAppResources , appResourcesDestinationPath ) ;
135
161
}
136
162
137
163
await this . $npm . uninstall ( defaultTemplateName , { save : true } , projectDir ) ;
@@ -187,13 +213,38 @@ export class ProjectService implements IProjectService {
187
213
private createPackageJson ( projectDir : string , projectId : string ) : void {
188
214
const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
189
215
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
- } ) ;
216
+ this . $fs . writeJson ( projectFilePath , this . packageJsonDefaultData ) ;
217
+
218
+ this . setAppId ( projectDir , projectId ) ;
219
+ }
220
+
221
+ private get packageJsonDefaultData ( ) : IStringDictionary {
222
+ return {
223
+ description : "NativeScript Application" ,
224
+ license : "SEE LICENSE IN <your-license-filename>" ,
225
+ readme : "NativeScript Application" ,
226
+ repository : "<fill-your-repository-here>"
227
+ } ;
228
+ }
229
+
230
+ private alterPackageJsonData ( projectDir : string , projectId : string ) : void {
231
+ const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
232
+
233
+ const packageJsonData = this . $fs . readJson ( projectFilePath ) ;
234
+
235
+ // Remove the metadata keys from the package.json
236
+ let updatedPackageJsonData = _ . omitBy < any , any > ( packageJsonData , ( value : any , key : string ) => _ . startsWith ( key , "_" ) ) ;
237
+ updatedPackageJsonData = _ . merge ( updatedPackageJsonData , this . packageJsonDefaultData ) ;
238
+
239
+ if ( updatedPackageJsonData . nativescript && updatedPackageJsonData . nativescript . templateVersion ) {
240
+ delete updatedPackageJsonData . nativescript . templateVersion ;
241
+ }
242
+
243
+ this . $fs . writeJson ( projectFilePath , updatedPackageJsonData ) ;
244
+ this . setAppId ( projectDir , projectId ) ;
245
+ }
196
246
247
+ private setAppId ( projectDir : string , projectId : string ) : void {
197
248
this . $projectDataService . setNSValue ( projectDir , "id" , projectId ) ;
198
249
}
199
250
}
0 commit comments