@@ -2,14 +2,15 @@ import * as constants from "../constants";
2
2
import * as path from "path" ;
3
3
import * as shelljs from "shelljs" ;
4
4
import { exported } from "../common/decorators" ;
5
+ import { Hooks } from "../constants" ;
5
6
6
7
export class ProjectService implements IProjectService {
7
8
8
- constructor ( private $npm : INodePackageManager ,
9
+ constructor ( private $hooksService : IHooksService ,
10
+ private $npm : INodePackageManager ,
9
11
private $errors : IErrors ,
10
12
private $fs : IFileSystem ,
11
13
private $logger : ILogger ,
12
- private $projectData : IProjectData ,
13
14
private $projectDataService : IProjectDataService ,
14
15
private $projectHelper : IProjectHelper ,
15
16
private $projectNameService : IProjectNameService ,
@@ -37,17 +38,26 @@ export class ProjectService implements IProjectService {
37
38
this . $errors . fail ( "Path already exists and is not empty %s" , projectDir ) ;
38
39
}
39
40
40
- const projectId = projectOptions . appId || this . $projectHelper . generateDefaultAppId ( projectName , constants . DEFAULT_APP_IDENTIFIER_PREFIX ) ;
41
- this . createPackageJson ( projectDir , projectId ) ;
42
-
43
- this . $logger . trace ( `Creating a new NativeScript project with name ${ projectName } and id ${ projectId } at location ${ projectDir } ` ) ;
41
+ const appId = projectOptions . appId || this . $projectHelper . generateDefaultAppId ( projectName , constants . DEFAULT_APP_IDENTIFIER_PREFIX ) ;
42
+ this . createPackageJson ( projectDir , appId ) ;
43
+ this . $logger . trace ( `Creating a new NativeScript project with name ${ projectName } and id ${ appId } at location ${ projectDir } ` ) ;
44
44
if ( ! selectedTemplate ) {
45
45
selectedTemplate = constants . RESERVED_TEMPLATE_NAMES [ "default" ] ;
46
46
}
47
47
48
+ const projectCreationData = await this . createProjectCore ( { template : selectedTemplate , projectDir, ignoreScripts : projectOptions . ignoreScripts , appId : appId , projectName } ) ;
49
+
50
+ this . $logger . printMarkdown ( "Project `%s` was successfully created." , projectCreationData . projectName ) ;
51
+
52
+ return projectCreationData ;
53
+ }
54
+
55
+ private async createProjectCore ( projectCreationSettings : IProjectCreationSettings ) : Promise < ICreateProjectData > {
56
+ const { template, projectDir, appId, projectName, ignoreScripts } = projectCreationSettings ;
57
+
48
58
try {
49
- const templatePath = await this . $projectTemplatesService . prepareTemplate ( selectedTemplate , projectDir ) ;
50
- await this . extractTemplate ( projectDir , templatePath ) ;
59
+ const { templatePath, templateVersion } = await this . $projectTemplatesService . prepareTemplate ( template , projectDir ) ;
60
+ await this . extractTemplate ( projectDir , templatePath , templateVersion ) ;
51
61
52
62
await this . ensureAppResourcesExist ( projectDir ) ;
53
63
@@ -57,23 +67,33 @@ export class ProjectService implements IProjectService {
57
67
await this . $npmInstallationManager . install ( constants . TNS_CORE_MODULES_NAME , projectDir , { dependencyType : "save" } ) ;
58
68
}
59
69
60
- this . mergeProjectAndTemplateProperties ( projectDir , templatePackageJsonData ) ; //merging dependencies from template (dev && prod)
61
- this . removeMergedDependencies ( projectDir , templatePackageJsonData ) ;
70
+ if ( templateVersion === constants . TemplateVersions . v1 ) {
71
+ this . mergeProjectAndTemplateProperties ( projectDir , templatePackageJsonData ) ; // merging dependencies from template (dev && prod)
72
+ this . removeMergedDependencies ( projectDir , templatePackageJsonData ) ;
73
+ }
62
74
75
+ const templatePackageJson = this . $fs . readJson ( path . join ( templatePath , constants . PACKAGE_JSON_FILE_NAME ) ) ;
76
+
77
+ // Install devDependencies and execute all scripts:
63
78
await this . $npm . install ( projectDir , projectDir , {
64
79
disableNpmInstall : false ,
65
80
frameworkPath : null ,
66
- ignoreScripts : projectOptions . ignoreScripts
81
+ ignoreScripts
67
82
} ) ;
68
83
69
- const templatePackageJson = this . $fs . readJson ( path . join ( templatePath , "package.json" ) ) ;
70
84
await this . $npm . uninstall ( templatePackageJson . name , { save : true } , projectDir ) ;
85
+ if ( templateVersion === constants . TemplateVersions . v2 ) {
86
+ this . alterPackageJsonData ( projectDir , appId ) ;
87
+ }
71
88
} catch ( err ) {
72
89
this . $fs . deleteDirectory ( projectDir ) ;
73
90
throw err ;
74
91
}
75
92
76
- this . $logger . printMarkdown ( "Project `%s` was successfully created." , projectName ) ;
93
+ this . $hooksService . executeAfterHooks ( Hooks . createProject , {
94
+ hookArgs : projectCreationSettings
95
+ } ) ;
96
+
77
97
return { projectName, projectDir } ;
78
98
}
79
99
@@ -100,21 +120,34 @@ export class ProjectService implements IProjectService {
100
120
return null ;
101
121
}
102
122
103
- private async extractTemplate ( projectDir : string , realTemplatePath : string ) : Promise < void > {
123
+ private async extractTemplate ( projectDir : string , realTemplatePath : string , templateVersion : string ) : Promise < void > {
104
124
this . $fs . ensureDirectoryExists ( projectDir ) ;
105
125
106
- const appDestinationPath = this . $projectData . getAppDirectoryPath ( projectDir ) ;
107
- this . $fs . createDirectory ( appDestinationPath ) ;
126
+ this . $logger . trace ( `Template version is ${ templateVersion } ` ) ;
127
+ let destinationDir = "" ;
128
+ switch ( templateVersion ) {
129
+ case constants . TemplateVersions . v1 :
130
+ const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
131
+ const appDestinationPath = projectData . getAppDirectoryPath ( projectDir ) ;
132
+ this . $fs . createDirectory ( appDestinationPath ) ;
133
+ destinationDir = appDestinationPath ;
134
+ break ;
135
+ case constants . TemplateVersions . v2 :
136
+ default :
137
+ destinationDir = projectDir ;
138
+ break ;
139
+ }
108
140
109
- this . $logger . trace ( `Copying application from '${ realTemplatePath } ' into '${ appDestinationPath } '.` ) ;
110
- shelljs . cp ( '-R' , path . join ( realTemplatePath , "*" ) , appDestinationPath ) ;
141
+ this . $logger . trace ( `Copying application from '${ realTemplatePath } ' into '${ destinationDir } '.` ) ;
142
+ shelljs . cp ( '-R' , path . join ( realTemplatePath , "*" ) , destinationDir ) ;
111
143
112
144
this . $fs . createDirectory ( path . join ( projectDir , "platforms" ) ) ;
113
145
}
114
146
115
147
private async ensureAppResourcesExist ( projectDir : string ) : Promise < void > {
116
- const appPath = this . $projectData . getAppDirectoryPath ( projectDir ) ,
117
- appResourcesDestinationPath = this . $projectData . getAppResourcesDirectoryPath ( projectDir ) ;
148
+ const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
149
+ const appPath = projectData . getAppDirectoryPath ( projectDir ) ;
150
+ const appResourcesDestinationPath = projectData . getAppResourcesDirectoryPath ( projectDir ) ;
118
151
119
152
if ( ! this . $fs . exists ( appResourcesDestinationPath ) ) {
120
153
this . $fs . createDirectory ( appResourcesDestinationPath ) ;
@@ -128,10 +161,24 @@ export class ProjectService implements IProjectService {
128
161
ignoreScripts : false
129
162
} ) ;
130
163
131
- const defaultTemplateAppResourcesPath = path . join ( projectDir , constants . NODE_MODULES_FOLDER_NAME ,
132
- defaultTemplateName , constants . APP_RESOURCES_FOLDER_NAME ) ;
164
+ const defaultTemplatePath = path . join ( projectDir , constants . NODE_MODULES_FOLDER_NAME , defaultTemplateName ) ;
165
+ const defaultTemplateVersion = this . $projectTemplatesService . getTemplateVersion ( defaultTemplatePath ) ;
166
+
167
+ let defaultTemplateAppResourcesPath : string = null ;
168
+ switch ( defaultTemplateVersion ) {
169
+ case constants . TemplateVersions . v1 :
170
+ defaultTemplateAppResourcesPath = path . join ( projectDir ,
171
+ constants . NODE_MODULES_FOLDER_NAME ,
172
+ defaultTemplateName ,
173
+ constants . APP_RESOURCES_FOLDER_NAME ) ;
174
+ break ;
175
+ case constants . TemplateVersions . v2 :
176
+ default :
177
+ const defaultTemplateProjectData = this . $projectDataService . getProjectData ( defaultTemplatePath ) ;
178
+ defaultTemplateAppResourcesPath = defaultTemplateProjectData . appResourcesDirectoryPath ;
179
+ }
133
180
134
- if ( this . $fs . exists ( defaultTemplateAppResourcesPath ) ) {
181
+ if ( defaultTemplateAppResourcesPath && this . $fs . exists ( defaultTemplateAppResourcesPath ) ) {
135
182
shelljs . cp ( '-R' , defaultTemplateAppResourcesPath , appPath ) ;
136
183
}
137
184
@@ -140,7 +187,8 @@ export class ProjectService implements IProjectService {
140
187
}
141
188
142
189
private removeMergedDependencies ( projectDir : string , templatePackageJsonData : any ) : void {
143
- const extractedTemplatePackageJsonPath = path . join ( this . $projectData . getAppDirectoryPath ( projectDir ) , constants . PACKAGE_JSON_FILE_NAME ) ;
190
+ const appDirectoryPath = this . $projectDataService . getProjectData ( projectDir ) . appDirectoryPath ;
191
+ const extractedTemplatePackageJsonPath = path . join ( appDirectoryPath , constants . PACKAGE_JSON_FILE_NAME ) ;
144
192
for ( const key in templatePackageJsonData ) {
145
193
if ( constants . PackageJsonKeysToKeep . indexOf ( key ) === - 1 ) {
146
194
delete templatePackageJsonData [ key ] ;
@@ -188,13 +236,38 @@ export class ProjectService implements IProjectService {
188
236
private createPackageJson ( projectDir : string , projectId : string ) : void {
189
237
const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
190
238
191
- this . $fs . writeJson ( projectFilePath , {
192
- "description" : "NativeScript Application" ,
193
- "license" : "SEE LICENSE IN <your-license-filename>" ,
194
- "readme" : "NativeScript Application" ,
195
- "repository" : "<fill-your-repository-here>"
196
- } ) ;
239
+ this . $fs . writeJson ( projectFilePath , this . packageJsonDefaultData ) ;
240
+
241
+ this . setAppId ( projectDir , projectId ) ;
242
+ }
243
+
244
+ private get packageJsonDefaultData ( ) : IStringDictionary {
245
+ return {
246
+ description : "NativeScript Application" ,
247
+ license : "SEE LICENSE IN <your-license-filename>" ,
248
+ readme : "NativeScript Application" ,
249
+ repository : "<fill-your-repository-here>"
250
+ } ;
251
+ }
252
+
253
+ private alterPackageJsonData ( projectDir : string , projectId : string ) : void {
254
+ const projectFilePath = path . join ( projectDir , this . $staticConfig . PROJECT_FILE_NAME ) ;
255
+
256
+ const packageJsonData = this . $fs . readJson ( projectFilePath ) ;
257
+
258
+ // Remove the metadata keys from the package.json
259
+ let updatedPackageJsonData = _ . omitBy < any , any > ( packageJsonData , ( value : any , key : string ) => _ . startsWith ( key , "_" ) ) ;
260
+ updatedPackageJsonData = _ . merge ( updatedPackageJsonData , this . packageJsonDefaultData ) ;
261
+
262
+ if ( updatedPackageJsonData . nativescript && updatedPackageJsonData . nativescript . templateVersion ) {
263
+ delete updatedPackageJsonData . nativescript . templateVersion ;
264
+ }
265
+
266
+ this . $fs . writeJson ( projectFilePath , updatedPackageJsonData ) ;
267
+ this . setAppId ( projectDir , projectId ) ;
268
+ }
197
269
270
+ private setAppId ( projectDir : string , projectId : string ) : void {
198
271
this . $projectDataService . setNSValue ( projectDir , "id" , projectId ) ;
199
272
}
200
273
}
0 commit comments