1
1
import * as constants from "../constants" ;
2
- import * as osenv from "osenv" ;
3
2
import * as path from "path" ;
4
3
import * as shelljs from "shelljs" ;
5
4
@@ -19,72 +18,37 @@ export class ProjectService implements IProjectService {
19
18
if ( ! projectName ) {
20
19
this . $errors . fail ( "You must specify <App name> when creating a new project." ) ;
21
20
}
22
-
23
21
projectName = await this . $projectNameService . ensureValidName ( projectName , { force : this . $options . force } ) ;
24
22
25
- let projectId = this . $options . appid || this . $projectHelper . generateDefaultAppId ( projectName , constants . DEFAULT_APP_IDENTIFIER_PREFIX ) ;
26
-
27
23
let projectDir = path . join ( path . resolve ( this . $options . path || "." ) , projectName ) ;
28
24
this . $fs . createDirectory ( projectDir ) ;
29
25
if ( this . $fs . exists ( projectDir ) && ! this . $fs . isEmptyDir ( projectDir ) ) {
30
26
this . $errors . fail ( "Path already exists and is not empty %s" , projectDir ) ;
31
27
}
32
28
29
+ let projectId = this . $options . appid || this . $projectHelper . generateDefaultAppId ( projectName , constants . DEFAULT_APP_IDENTIFIER_PREFIX ) ;
33
30
this . createPackageJson ( projectDir , projectId ) ;
34
31
35
- let customAppPath = this . getCustomAppPath ( ) ;
36
- if ( customAppPath ) {
37
- customAppPath = path . resolve ( customAppPath ) ;
38
- if ( ! this . $fs . exists ( customAppPath ) ) {
39
- this . $errors . failWithoutHelp ( `The specified path "${ customAppPath } " doesn't exist. Check that you specified the path correctly and try again.` ) ;
40
- }
41
-
42
- let customAppContents = this . $fs . enumerateFilesInDirectorySync ( customAppPath ) ;
43
- if ( customAppContents . length === 0 ) {
44
- this . $errors . failWithoutHelp ( `The specified path "${ customAppPath } " is empty directory.` ) ;
45
- }
32
+ this . $logger . trace ( `Creating a new NativeScript project with name ${ projectName } and id ${ projectId } at location ${ projectDir } ` ) ;
33
+ if ( ! selectedTemplate ) {
34
+ selectedTemplate = constants . RESERVED_TEMPLATE_NAMES [ "default" ] ;
46
35
}
47
36
48
- this . $logger . trace ( "Creating a new NativeScript project with name %s and id %s at location %s" , projectName , projectId , projectDir ) ;
49
-
50
- let projectAppDirectory = path . join ( projectDir , constants . APP_FOLDER_NAME ) ;
51
- let appPath : string = null ;
52
- if ( customAppPath ) {
53
- this . $logger . trace ( "Using custom app from %s" , customAppPath ) ;
54
-
55
- // Make sure that the source app/ is not a direct ancestor of a target app/
56
- let relativePathFromSourceToTarget = path . relative ( customAppPath , projectAppDirectory ) ;
57
- // path.relative returns second argument if the paths are located on different disks
58
- // so in this case we don't need to make the check for direct ancestor
59
- if ( relativePathFromSourceToTarget !== projectAppDirectory ) {
60
- let doesRelativePathGoUpAtLeastOneDir = relativePathFromSourceToTarget . split ( path . sep ) [ 0 ] === ".." ;
61
- if ( ! doesRelativePathGoUpAtLeastOneDir ) {
62
- this . $errors . fail ( "Project dir %s must not be created at/inside the template used to create the project %s." , projectDir , customAppPath ) ;
63
- }
64
- }
65
- this . $logger . trace ( "Copying custom app into %s" , projectAppDirectory ) ;
66
- appPath = customAppPath ;
67
- } else {
37
+ try {
68
38
let templatePath = await this . $projectTemplatesService . prepareTemplate ( selectedTemplate , projectDir ) ;
69
- this . $logger . trace ( `Copying application from '${ templatePath } ' into '${ projectAppDirectory } '.` ) ;
70
- let templatePackageJson = this . $fs . readJson ( path . join ( templatePath , "package.json" ) ) ;
71
- selectedTemplate = templatePackageJson . name ;
72
- appPath = templatePath ;
73
- }
39
+ await this . extractTemplate ( projectDir , templatePath ) ;
74
40
75
- try {
76
- //TODO: plamen5kov: move copy of template and npm uninstall in prepareTemplate logic
77
- await this . createProjectCore ( projectDir , appPath , projectId ) ;
78
- let templatePackageJsonData = this . getDataFromJson ( appPath ) ;
41
+ let packageName = constants . TNS_CORE_MODULES_NAME ;
42
+ await this . $npm . install ( packageName , projectDir , { save : true , "save-exact" : true } ) ;
43
+
44
+ let templatePackageJsonData = this . getDataFromJson ( templatePath ) ;
79
45
this . mergeProjectAndTemplateProperties ( projectDir , templatePackageJsonData ) ; //merging dependencies from template (dev && prod)
80
46
this . removeMergedDependencies ( projectDir , templatePackageJsonData ) ;
47
+
81
48
await this . $npm . install ( projectDir , projectDir , { "ignore-scripts" : this . $options . ignoreScripts } ) ;
82
- selectedTemplate = selectedTemplate || "" ;
83
- let templateName = ( constants . RESERVED_TEMPLATE_NAMES [ selectedTemplate . toLowerCase ( ) ] || selectedTemplate /*user template*/ ) || constants . RESERVED_TEMPLATE_NAMES [ "default" ] ;
84
- await this . $npm . uninstall ( selectedTemplate , { save : true } , projectDir ) ;
85
49
86
- // TODO: plamen5kov: remove later (put only so tests pass (need to fix tests))
87
- this . $logger . trace ( `Using NativeScript verified template: ${ templateName } with version undefined.` ) ;
50
+ let templatePackageJson = this . $fs . readJson ( path . join ( templatePath , "package.json" ) ) ;
51
+ await this . $npm . uninstall ( templatePackageJson . name , { save : true } , projectDir ) ;
88
52
} catch ( err ) {
89
53
this . $fs . deleteDirectory ( projectDir ) ;
90
54
throw err ;
@@ -103,6 +67,18 @@ export class ProjectService implements IProjectService {
103
67
return null ;
104
68
}
105
69
70
+ private async extractTemplate ( projectDir : string , realTemplatePath : string ) : Promise < void > {
71
+ this . $fs . ensureDirectoryExists ( projectDir ) ;
72
+
73
+ let appDestinationPath = path . join ( projectDir , constants . APP_FOLDER_NAME ) ;
74
+ this . $fs . createDirectory ( appDestinationPath ) ;
75
+
76
+ this . $logger . trace ( `Copying application from '${ realTemplatePath } ' into '${ appDestinationPath } '.` ) ;
77
+ shelljs . cp ( '-R' , path . join ( realTemplatePath , "*" ) , appDestinationPath ) ;
78
+
79
+ this . $fs . createDirectory ( path . join ( projectDir , "platforms" ) ) ;
80
+ }
81
+
106
82
private removeMergedDependencies ( projectDir : string , templatePackageJsonData : any ) : void {
107
83
let extractedTemplatePackageJsonPath = path . join ( projectDir , constants . APP_FOLDER_NAME , constants . PACKAGE_JSON_FILE_NAME ) ;
108
84
for ( let key in templatePackageJsonData ) {
@@ -129,6 +105,8 @@ export class ProjectService implements IProjectService {
129
105
}
130
106
this . $logger . trace ( "New project package.json data: " , projectPackageJsonData ) ;
131
107
this . $fs . writeJson ( projectPackageJsonPath , projectPackageJsonData ) ;
108
+ } else {
109
+ this . $errors . failWithoutHelp ( `Couldn't find package.json data in installed template` ) ;
132
110
}
133
111
}
134
112
@@ -147,42 +125,9 @@ export class ProjectService implements IProjectService {
147
125
return sortedDeps ;
148
126
}
149
127
150
- private async createProjectCore ( projectDir : string , appSourcePath : string , projectId : string ) : Promise < void > {
151
- this . $fs . ensureDirectoryExists ( projectDir ) ;
152
-
153
- let appDestinationPath = path . join ( projectDir , constants . APP_FOLDER_NAME ) ;
154
- this . $fs . createDirectory ( appDestinationPath ) ;
155
-
156
- shelljs . cp ( '-R' , path . join ( appSourcePath , "*" ) , appDestinationPath ) ;
157
-
158
- this . $fs . createDirectory ( path . join ( projectDir , "platforms" ) ) ;
159
-
160
- let tnsModulesVersion = this . $options . tnsModulesVersion ;
161
- let packageName = constants . TNS_CORE_MODULES_NAME ;
162
- if ( tnsModulesVersion ) {
163
- packageName = `${ packageName } @${ tnsModulesVersion } ` ;
164
- }
165
- await this . $npm . install ( packageName , projectDir , { save : true , "save-exact" : true } ) ;
166
- }
167
-
168
128
private createPackageJson ( projectDir : string , projectId : string ) : void {
169
129
this . $projectDataService . initialize ( projectDir ) ;
170
130
this . $projectDataService . setValue ( "id" , projectId ) ;
171
131
}
172
-
173
- private getCustomAppPath ( ) : string {
174
- let customAppPath = this . $options . copyFrom || this . $options . linkTo ;
175
- if ( customAppPath ) {
176
- if ( customAppPath . indexOf ( "http://" ) === 0 ) {
177
- this . $errors . fail ( "Only local paths for custom app are supported." ) ;
178
- }
179
-
180
- if ( customAppPath . substr ( 0 , 1 ) === '~' ) {
181
- customAppPath = path . join ( osenv . home ( ) , customAppPath . substr ( 1 ) ) ;
182
- }
183
- }
184
-
185
- return customAppPath ;
186
- }
187
132
}
188
133
$injector . register ( "projectService" , ProjectService ) ;
0 commit comments