@@ -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,16 +38,25 @@ 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, templateVersion } = await this . $projectTemplatesService . prepareTemplate ( selectedTemplate , projectDir ) ;
59
+ const { templatePath, templateVersion } = await this . $projectTemplatesService . prepareTemplate ( template , projectDir ) ;
50
60
await this . extractTemplate ( projectDir , templatePath , templateVersion ) ;
51
61
52
62
await this . ensureAppResourcesExist ( projectDir ) ;
@@ -68,19 +78,22 @@ export class ProjectService implements IProjectService {
68
78
await this . $npm . install ( projectDir , projectDir , {
69
79
disableNpmInstall : false ,
70
80
frameworkPath : null ,
71
- ignoreScripts : projectOptions . ignoreScripts
81
+ ignoreScripts
72
82
} ) ;
73
83
74
84
await this . $npm . uninstall ( templatePackageJson . name , { save : true } , projectDir ) ;
75
85
if ( templateVersion === constants . TemplateVersions . v2 ) {
76
- this . alterPackageJsonData ( projectDir , projectId ) ;
86
+ this . alterPackageJsonData ( projectDir , appId ) ;
77
87
}
78
88
} catch ( err ) {
79
89
this . $fs . deleteDirectory ( projectDir ) ;
80
90
throw err ;
81
91
}
82
92
83
- this . $logger . printMarkdown ( "Project `%s` was successfully created." , projectName ) ;
93
+ this . $hooksService . executeAfterHooks ( Hooks . createProject , {
94
+ hookArgs : projectCreationSettings
95
+ } ) ;
96
+
84
97
return { projectName, projectDir } ;
85
98
}
86
99
@@ -114,7 +127,8 @@ export class ProjectService implements IProjectService {
114
127
let destinationDir = "" ;
115
128
switch ( templateVersion ) {
116
129
case constants . TemplateVersions . v1 :
117
- const appDestinationPath = this . $projectData . getAppDirectoryPath ( projectDir ) ;
130
+ const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
131
+ const appDestinationPath = projectData . getAppDirectoryPath ( projectDir ) ;
118
132
this . $fs . createDirectory ( appDestinationPath ) ;
119
133
destinationDir = appDestinationPath ;
120
134
break ;
@@ -131,8 +145,9 @@ export class ProjectService implements IProjectService {
131
145
}
132
146
133
147
private async ensureAppResourcesExist ( projectDir : string ) : Promise < void > {
134
- const appPath = this . $projectData . getAppDirectoryPath ( projectDir ) ;
135
- const appResourcesDestinationPath = this . $projectData . getAppResourcesDirectoryPath ( projectDir ) ;
148
+ const projectData = this . $projectDataService . getProjectData ( projectDir ) ;
149
+ const appPath = projectData . getAppDirectoryPath ( projectDir ) ;
150
+ const appResourcesDestinationPath = projectData . getAppResourcesDirectoryPath ( projectDir ) ;
136
151
137
152
if ( ! this . $fs . exists ( appResourcesDestinationPath ) ) {
138
153
this . $fs . createDirectory ( appResourcesDestinationPath ) ;
@@ -172,7 +187,8 @@ export class ProjectService implements IProjectService {
172
187
}
173
188
174
189
private removeMergedDependencies ( projectDir : string , templatePackageJsonData : any ) : void {
175
- 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 ) ;
176
192
for ( const key in templatePackageJsonData ) {
177
193
if ( constants . PackageJsonKeysToKeep . indexOf ( key ) === - 1 ) {
178
194
delete templatePackageJsonData [ key ] ;
0 commit comments