11import * as constants from "../constants" ;
2- import * as osenv from "osenv" ;
32import * as path from "path" ;
43import * as shelljs from "shelljs" ;
54
@@ -19,72 +18,37 @@ export class ProjectService implements IProjectService {
1918 if ( ! projectName ) {
2019 this . $errors . fail ( "You must specify <App name> when creating a new project." ) ;
2120 }
22-
2321 projectName = await this . $projectNameService . ensureValidName ( projectName , { force : this . $options . force } ) ;
2422
25- let projectId = this . $options . appid || this . $projectHelper . generateDefaultAppId ( projectName , constants . DEFAULT_APP_IDENTIFIER_PREFIX ) ;
26-
2723 let projectDir = path . join ( path . resolve ( this . $options . path || "." ) , projectName ) ;
2824 this . $fs . createDirectory ( projectDir ) ;
2925 if ( this . $fs . exists ( projectDir ) && ! this . $fs . isEmptyDir ( projectDir ) ) {
3026 this . $errors . fail ( "Path already exists and is not empty %s" , projectDir ) ;
3127 }
3228
29+ let projectId = this . $options . appid || this . $projectHelper . generateDefaultAppId ( projectName , constants . DEFAULT_APP_IDENTIFIER_PREFIX ) ;
3330 this . createPackageJson ( projectDir , projectId ) ;
3431
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" ] ;
4635 }
4736
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 {
6838 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 ) ;
7440
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 ) ;
7945 this . mergeProjectAndTemplateProperties ( projectDir , templatePackageJsonData ) ; //merging dependencies from template (dev && prod)
8046 this . removeMergedDependencies ( projectDir , templatePackageJsonData ) ;
47+
8148 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 ) ;
8549
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 ) ;
8852 } catch ( err ) {
8953 this . $fs . deleteDirectory ( projectDir ) ;
9054 throw err ;
@@ -103,6 +67,18 @@ export class ProjectService implements IProjectService {
10367 return null ;
10468 }
10569
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+
10682 private removeMergedDependencies ( projectDir : string , templatePackageJsonData : any ) : void {
10783 let extractedTemplatePackageJsonPath = path . join ( projectDir , constants . APP_FOLDER_NAME , constants . PACKAGE_JSON_FILE_NAME ) ;
10884 for ( let key in templatePackageJsonData ) {
@@ -129,6 +105,8 @@ export class ProjectService implements IProjectService {
129105 }
130106 this . $logger . trace ( "New project package.json data: " , projectPackageJsonData ) ;
131107 this . $fs . writeJson ( projectPackageJsonPath , projectPackageJsonData ) ;
108+ } else {
109+ this . $errors . failWithoutHelp ( `Couldn't find package.json data in installed template` ) ;
132110 }
133111 }
134112
@@ -147,42 +125,9 @@ export class ProjectService implements IProjectService {
147125 return sortedDeps ;
148126 }
149127
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-
168128 private createPackageJson ( projectDir : string , projectId : string ) : void {
169129 this . $projectDataService . initialize ( projectDir ) ;
170130 this . $projectDataService . setValue ( "id" , projectId ) ;
171131 }
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- }
187132}
188133$injector . register ( "projectService" , ProjectService ) ;
0 commit comments