Skip to content

Commit 035114a

Browse files
committed
New starter project sourcing
1 parent 41cf47d commit 035114a

4 files changed

Lines changed: 140 additions & 44 deletions

File tree

bin/ionic

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ process.title = 'ionic';
77

88
var Ionic = require('../lib/ionic').Ionic;
99

10-
var ionic = new Ionic();
11-
ionic.run();
10+
Ionic.run();

lib/ionic.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,29 @@ var IonicStartTask = require('./ionic/start').IonicStartTask,
2121
IonicBuildTask = require('./ionic/build').IonicBuildTask,
2222
IonicLoginTask = require('./ionic/login').IonicLoginTask,
2323
IonicUploadTask = require('./ionic/upload').IonicUploadTask,
24-
IonicPackageTask = require('./ionic/package').IonicPackageTask;
24+
IonicPackageTask = require('./ionic/package').IonicPackageTask,
25+
path = require('path'),
26+
request = require('request'),
27+
os = require('os'),
28+
unzip = require('unzip'),
29+
colors = require('colors'),
30+
Q = require('q');
31+
32+
33+
colors.setTheme({
34+
silly: 'rainbow',
35+
input: 'grey',
36+
small: 'grey',
37+
verbose: 'cyan',
38+
prompt: 'grey',
39+
info: 'white',
40+
data: 'grey',
41+
help: 'cyan',
42+
warn: 'yellow',
43+
debug: 'blue',
44+
error: 'red'
45+
});
46+
2547

2648
var fs = require('fs'),
2749
argv = require('optimist').argv;
@@ -79,9 +101,7 @@ var TASKS = [
79101
}
80102
];
81103

82-
Ionic = function() {};
83-
84-
Ionic.prototype = {
104+
Ionic = {
85105
IONIC_DASH: 'http://apps.ionicframework.com/',
86106
IONIC_COOKIES: 'ionic.cookies',
87107
IONIC_API: 'api/v1/',
@@ -148,7 +168,7 @@ Ionic.prototype = {
148168
return this._printGenericUsage();
149169
}
150170

151-
console.log('Running', task.title, 'task...')
171+
console.log('Running', task.title.info.bold, 'task...')
152172

153173
var taskObj = new task.task();
154174
taskObj.run(this);
@@ -158,8 +178,44 @@ Ionic.prototype = {
158178
process.stderr.write('Error, exiting:');
159179
process.stderr.write(msg + '\n');
160180
process.exit(1);
161-
}
181+
},
182+
183+
/**
184+
* Fetch a repo from GitHub, unzip it to a specific folder.
185+
*/
186+
fetchRepo: function(targetPath, repoName, repoUrl) {
187+
var q = Q.defer();
188+
189+
// The folder name the project will be downloaded and extracted to
190+
var repoFolderName = repoName + '-master';
191+
console.log('Fetching repo:', repoUrl);
192+
193+
var tmpFolder = os.tmpdir();
194+
var tempZipFilePath = path.join(tmpFolder, repoName + new Date().getTime() + '.zip');
195+
var tempZipFileStream = fs.createWriteStream(tempZipFilePath)
162196

197+
var unzipRepo = function(fileName) {
198+
var readStream = fs.createReadStream(fileName);
199+
200+
var writeStream = unzip.Extract({ path: targetPath });
201+
writeStream.on('close', function() {
202+
q.resolve(repoFolderName);
203+
});
204+
readStream.pipe(writeStream);
205+
};
206+
207+
request({ url: repoUrl, encoding: null }, function(err, res, body) {
208+
if(res.statusCode !== 200) {
209+
q.reject(res);
210+
return;
211+
}
212+
tempZipFileStream.write(body);
213+
tempZipFileStream.close();
214+
unzipRepo(tempZipFilePath);
215+
});
216+
217+
return q.promise;
218+
}
163219
};
164220

165221
exports.Ionic = Ionic;

lib/ionic/start.js

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
2831
var IonicStartTask = function() {
2932
}
3033

34+
// The URL for the cordova wrapper project
35+
IonicStartTask.WRAPPER_REPO_NAME = 'ionic-cordova-wrapper';
36+
3137
IonicStartTask.HELP_LINE = 'Start a new Ionic project with the given name.';
3238

3339
IonicStartTask.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+
73100
IonicStartTask.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

120159
IonicStartTask.prototype._writeConfig = function(ionic) {
@@ -125,7 +164,7 @@ IonicStartTask.prototype._writeConfig = function(ionic) {
125164

126165
IonicStartTask.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;

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
"license": "MIT",
2727
"dependencies": {
2828
"archiver": "0.5.1",
29-
"cordova": "~3.2.0",
30-
"event-stream": "3.0.x",
29+
"cordova": "~3.2.0",
30+
"event-stream": "3.0.x",
3131
"form-data": "~0.1.0",
3232
"ncp": "0.4.2",
3333
"optimist": "0.6.0",
3434
"prompt": "0.2.12",
3535
"request": "2.27.0",
3636
"shelljs": "0.2.6",
37-
"unzip": "0.1.9"
37+
"unzip": "0.1.9",
38+
"q": "^1.0.1",
39+
"colors": "^0.6.2"
3840
}
3941
}

0 commit comments

Comments
 (0)