Skip to content

Commit 09cbcd4

Browse files
committed
start ionic project from any Github repo
1 parent b0a8687 commit 09cbcd4

3 files changed

Lines changed: 51 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### 1.2.4
22

33
* Use `cross-spawn` module to fix errors with using spawn on Windows
4+
* Start ionic project from any Github repo
45
* Start ionic projects using a local directory
56
* Use specific npm versions in package.json to avoid any future errors from breaking changes
67

lib/ionic.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ Ionic = {
599599
var request = require('request');
600600
request({ url: archiveUrl, encoding: null, proxy: proxy }, function(err, res, body) {
601601
if(!res || res.statusCode !== 200) {
602+
console.error('Unable to fetch:'.error.bold, archiveUrl, '(' + res.statusCode + ')');
602603
q.reject(res);
603604
return;
604605
}
@@ -607,6 +608,7 @@ Ionic = {
607608
tempZipFileStream.close();
608609
unzipRepo(tempZipFilePath);
609610
} catch(e) {
611+
console.log(e.error);
610612
q.reject(e);
611613
}
612614
}).on('response', function(res){

lib/ionic/start.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs'),
33
request = require('request'),
44
ncp = require('ncp').ncp,
55
path = require('path'),
6+
parseUrl = require('url').parse,
67
shelljs = require('shelljs/global'),
78
argv = require('optimist').argv,
89
prompt = require('prompt'),
@@ -150,13 +151,19 @@ IonicTask.prototype.fetchWrapper = function() {
150151
IonicTask.prototype.fetchSeed = function() {
151152

152153
// Codepen: http://codepen.io/ionic/pen/GpCst
153-
if(this.template.toLowerCase().indexOf('codepen') > -1) {
154+
if( /\/\/codepen.io\//i.test(this.template) ) {
154155
this.seedType = 'codepen';
155156
return this.fetchCodepen();
156157
}
157158

159+
// Github URL: http://github.com/myrepo/
160+
if( /\/\/github.com\//i.test(this.template) ) {
161+
this.seedType = 'github';
162+
return this.fetchGithubStarter(this.template);
163+
}
164+
158165
// Local Directory: /User/starterapp
159-
if( this.template.indexOf('/') > -1 || this.template.indexOf('\\') > -1 ) {
166+
if( (this.template.indexOf('/') > -1 || this.template.indexOf('\\') > -1) && (this.indexOf('http://') === -1 && this.indexOf('https://') === -1)) {
160167
this.seedType = 'local';
161168
return this.fetchLocalStarter();
162169
}
@@ -322,31 +329,57 @@ IonicTask.prototype.fetchLocalStarter = function() {
322329

323330

324331
IonicTask.prototype.fetchIonicStarter = function() {
325-
var self = this;
326-
var q = Q.defer();
327332

328333
// Get the starter project repo name:
329-
var repoName = 'ionic-starter-' + self.template;
334+
var repoName = 'ionic-starter-' + this.template;
330335

331336
// Get the URL for the starter project repo:
332-
var repoUrl = 'https://github.com/driftyco/' + repoName + '/archive/master.zip';
337+
var repoUrl = 'https://github.com/driftyco/' + repoName;
338+
339+
return this.fetchGithubStarter(repoUrl);
340+
};
341+
342+
343+
IonicTask.prototype.fetchGithubStarter = function(repoUrl) {
344+
var self = this;
345+
var q = Q.defer();
346+
347+
// https://github.com/driftyco/ionic-starter-tabs/
348+
var urlParse = parseUrl(repoUrl);
349+
var pathSplit = urlParse.pathname.replace(/\//g, ' ').trim().split(' ');
350+
if(!urlParse.hostname || urlParse.hostname.toLowerCase() !== 'github.com' || pathSplit.length !== 2) {
351+
console.log( ('Invalid Github URL: ' + repoUrl).error );
352+
console.log( ('Example of a valid URL: https://github.com/driftyco/ionic-starter-tabs/').error );
353+
self.ionic.fail('');
354+
q.reject();
355+
return q.promise;
356+
}
357+
var repoName = pathSplit[1];
358+
var repoFolderName = repoName + '-master';
359+
360+
// ensure there's an ending /
361+
if(repoUrl.substr(repoUrl.length -1) !== '/') {
362+
repoUrl += '/';
363+
}
364+
repoUrl += 'archive/master.zip';
333365

334366
Ionic.fetchArchive(self.targetPath, repoUrl).then(function() {
335367

336-
var repoFolderName = repoName + '-master';
368+
try {
369+
// Move the content of this repo into the www folder
370+
cp('-Rf', self.targetPath + '/' + repoFolderName + '/.', 'www');
337371

338-
// Move the content of this repo into the www folder
339-
cp('-Rf', self.targetPath + '/' + repoFolderName + '/.', 'www');
372+
// Clean up start template folder
373+
rm('-rf', self.targetPath + '/' + repoFolderName + '/');
340374

341-
// Clean up start template folder
342-
rm('-rf', self.targetPath + '/' + repoFolderName + '/');
375+
q.resolve();
343376

344-
q.resolve();
377+
} catch(e) {
378+
q.reject(e);
379+
}
345380

346381
}).catch(function(err) {
347-
console.error('Error: Unable to fetch project: HTTP:'.error.bold, err.statusCode);
348-
console.error('Valid project types are "blank", "tabs", or "sidemenu"'.error.bold);
349-
console.error('More info available at: http://ionicframework.com/getting-started/'.error.bold);
382+
console.error('More info available at: \nhttp://ionicframework.com/getting-started/\nhttps://github.com/driftyco/ionic-cli'.error.bold);
350383
return self.ionic.fail('');
351384
});
352385

0 commit comments

Comments
 (0)