@@ -6,6 +6,9 @@ var fs = require('fs'),
66 shelljs = require ( 'shelljs/global' ) ,
77 unzip = require ( 'unzip' ) ,
88 argv = require ( 'optimist' ) . argv ,
9+ colors = require ( 'colors' ) ,
10+ Q = require ( 'q' ) ,
11+ IonicProject = require ( '../ionic' ) ,
912 IonicProject = require ( './project' ) ,
1013 IonicTask = require ( './task' ) . IonicTask ;
1114 IonicStats = require ( './stats' ) . IonicStats ;
@@ -28,6 +31,9 @@ fs.mkdirParent = function(dirPath, mode, callback) {
2831var IonicStartTask = function ( ) {
2932}
3033
34+ // The URL for the cordova wrapper project
35+ IonicStartTask . WRAPPER_REPO_NAME = 'ionic-cordova-wrapper' ;
36+
3137IonicStartTask . HELP_LINE = 'Start a new Ionic project with the given name.' ;
3238
3339IonicStartTask . prototype = new IonicTask ( ) ;
@@ -53,11 +59,11 @@ IonicStartTask.prototype.run = function(ionic) {
5359
5460 // Make sure to create this, or ask them if they want to override it
5561 if ( this . _checkTargetPath ( ) === false ) {
56- process . stderr . write ( 'Exiting .\n' ) ;
62+ process . stderr . write ( '\nExiting .\n' ) ;
5763 process . exit ( 1 ) ;
5864 }
5965
60- console . log ( 'Creating Ionic app in folder' , this . targetPath , 'based on' , starterProject , 'project' ) ;
66+ console . log ( 'Creating Ionic app in folder' , this . targetPath , 'based on' , starterProject . info . bold , 'project' ) ;
6167
6268 fs . mkdirSync ( this . targetPath ) ;
6369
@@ -70,51 +76,84 @@ IonicStartTask.prototype._getStarterUrl = function(repo) {
7076 return 'https://github.com/driftyco/' + repo + '/archive/master.zip' ;
7177} ;
7278
79+ IonicStartTask . prototype . _fetchWrapper = function ( ) {
80+ var q = Q . defer ( ) ;
81+ var self = this ;
82+
83+ var repoName = IonicStartTask . WRAPPER_REPO_NAME ;
84+ var repoUrl = 'https://github.com/driftyco/' + IonicStartTask . WRAPPER_REPO_NAME + '/archive/master.zip' ;
85+ console . log ( 'Fetching' , 'Cordova' . info . bold , 'wrapper' ) ;
86+
87+ Ionic . fetchRepo ( self . targetPath , repoName , repoUrl ) . then ( function ( repoFolderName ) {
88+ cp ( '-R' , self . targetPath + '/' + repoFolderName + '/.' , self . targetPath ) ;
89+ rm ( '-rf' , self . targetPath + '/' + repoFolderName + '/' ) ;
90+ cd ( self . targetPath ) ;
91+
92+ q . resolve ( self . targetPath ) ;
93+ } , function ( err ) {
94+ q . reject ( err ) ;
95+ } ) ;
96+
97+ return q . promise ;
98+ } ;
99+
73100IonicStartTask . prototype . _fetchAndWriteSeed = function ( projectType ) {
74101 var self = this ;
75102
76- var repoName = 'ionic-starter-' + projectType ;
77- var repoFolderName = repoName + '-master' ;
78- var templateUrl = this . _getStarterUrl ( repoName ) ;
103+ // First, grab the wrapper project.
79104
80- console . log ( 'Downloading starter template from' , templateUrl ) ;
105+ var wrapperPromise = this . _fetchWrapper ( ) ;
81106
82- var tmpFolder = os . tmpdir ( ) ;
83- var tempZipFilePath = path . join ( tmpFolder , repoName + new Date ( ) . getTime ( ) + '.zip' ) ;
84- var tempZipFileStream = fs . createWriteStream ( tempZipFilePath )
107+ wrapperPromise . then ( function ( targetPath ) {
108+ // Get the starter project repo name:
109+ var repoName = 'ionic-starter-' + projectType ;
85110
86- var unzipRepo = function ( fileName ) {
87- var readStream = fs . createReadStream ( fileName ) ;
111+ // The folder name the project will be downloaded and extracted to
112+ var repoFolderName = repoName + '-master' ;
88113
89- var writeStream = unzip . Extract ( { path : self . targetPath } ) ;
90- writeStream . on ( 'close' , function ( ) {
91- //fs.renameSync(self.targetPath + '/' + 'ionic-angular-cordova-seed-master', self.targetPath + '/app');
92- cp ( '-R' , self . targetPath + '/' + repoFolderName + '/.' , self . targetPath ) ;
93- rm ( '-rf' , self . targetPath + '/' + repoFolderName + '/' ) ;
94- console . log ( 'Project created!' ) ;
114+ // Get the URL for the starter project repo:
115+ var repoUrl = self . _getStarterUrl ( repoName ) ;
95116
96- cd ( self . targetPath ) ;
97- console . log ( 'Initializing cordova project.' ) ;
117+ Ionic . fetchRepo ( self . targetPath , repoName , repoUrl ) . then ( function ( repoFolderName ) {
118+ // Move the content of this repo into the www folder
119+ cp ( '-R' , targetPath + '/' + repoFolderName + '/.' , 'www' ) ;
120+
121+ // Copy the root config file to the www folder
122+ cp ( targetPath + '/config.xml' , 'www' ) ;
123+
124+ // Clean up start template folder
125+ rm ( '-rf' , targetPath + '/' + repoFolderName + '/' ) ;
126+
127+ console . log ( 'Initializing cordova project.' . info . bold ) ;
98128 if ( ! exec ( 'cordova plugin add org.apache.cordova.device' ) || ! exec ( 'cordova plugin add org.apache.cordova.console' ) ||
99129 ! exec ( 'cordova plugin add org.apache.cordova.statusbar' ) ) {
100- process . stderr . write ( 'Unable to install one or more cordova plugins.\n' ) ;
130+ process . stderr . write ( 'Unable to install one or more cordova plugins.\n' . error . bold ) ;
101131 }
102132
133+ self . _printQuickHelp ( ) ;
134+
103135 IonicStats . t ( 'start' , { } ) ;
136+ } , function ( err ) {
137+ console . error ( 'Error: Unable to fetch project: HTTP' . error . bold , res . statusCode ) ;
138+ console . error ( 'Valid project types are blank, tabs, or sidemenu (or see more on our starter page: http://ionicframework.com/getting-started/)' . error . bold ) ;
139+ Ionic . fail ( '' ) ;
104140 } ) ;
105- readStream . pipe ( writeStream ) ;
106- } ;
107-
108- request ( { url : templateUrl , encoding : null } , function ( err , res , body ) {
109- if ( res . statusCode !== 200 ) {
110- console . error ( 'Error: Unable to fetch project: HTTP' , res . statusCode ) ;
111- console . error ( 'Valid project types are blank, tabs, or sidemenu (or see more on our starter page: http://ionicframework.com/getting-started/)' ) ;
112- return ;
113- }
114- tempZipFileStream . write ( body ) ;
115- tempZipFileStream . close ( ) ;
116- unzipRepo ( tempZipFilePath ) ;
141+
142+ } , function ( err ) {
143+ Ionic . fail ( 'Unable to grab wrapper project:' . error . bold , err ) ;
117144 } ) ;
145+
146+ } ;
147+
148+ IonicStartTask . prototype . _printQuickHelp = function ( ) {
149+ console . log ( '\nYour Ionic project is ready to go!' . green . bold ) ;
150+ console . log ( '\nSome quick tips:' ) ;
151+ console . log ( ' * Make sure to add a platform (ios or Android):' , 'ionic platform add android [ios]' . info . bold ) ;
152+ console . log ( ' Note: iOS development requires OS X currently' . small ) ;
153+ console . log ( '\n * To build your project, run' , 'ionic build [platform]' . info . bold ) ;
154+ console . log ( '\n * To emulate your project in a simulator or emulator, run' , 'ionic emulate [platform]' . info . bold ) ;
155+ console . log ( '\n * To run your project on a device, run' , 'ionic run [platform]' . info . bold ) ;
156+ console . log ( '\n\nFor more help, visit the Ionic docs:' , 'http://ionicframework.com/docs' . info . bold ) ;
118157} ;
119158
120159IonicStartTask . prototype . _writeConfig = function ( ionic ) {
@@ -125,7 +164,7 @@ IonicStartTask.prototype._writeConfig = function(ionic) {
125164
126165IonicStartTask . prototype . _checkTargetPath = function ( ) {
127166 if ( fs . existsSync ( this . targetPath ) ) {
128- process . stderr . write ( 'The directory ' + this . targetPath + ' already exists, please remove it if you would like to create a new ionic project there.\n' ) ;
167+ process . stderr . write ( 'The directory ' . error . bold + this . targetPath + ' already exists, please remove it if you would like to create a new ionic project there.\n' . error . bold ) ;
129168 return false ;
130169 }
131170 return true ;
0 commit comments