Skip to content

Commit 1c10195

Browse files
Merge pull request #463 from raphaelokon/master
Make listStarterkits return an array of repo objects
2 parents 872bb6c + 258f501 commit 1c10195

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

core/lib/starterkit_manager.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"use strict";
22

33
var starterkit_manager = function (config) {
4-
var path = require('path'),
5-
fs = require('fs-extra'),
6-
util = require('./utilities'),
7-
paths = config.paths;
4+
var path = require('path'),
5+
fetch = require('node-fetch'),
6+
fs = require('fs-extra'),
7+
util = require('./utilities'),
8+
paths = config.paths;
89

910
function loadStarterKit(starterkitName, clean) {
1011
try {
@@ -41,8 +42,35 @@ var starterkit_manager = function (config) {
4142
}
4243
}
4344

45+
/**
46+
* @func listStarterkits
47+
* @desc Fetches starterkit repos from GH API that contain 'starterkit' in their name for the user 'pattern-lab'
48+
* @returns {Promise} Returns an Array<{name,url}> for the starterkit repos
49+
*/
4450
function listStarterkits() {
45-
console.log('https://github.com/search?utf8=%E2%9C%93&q=starterkit+in%3Aname%2C+user%3Apattern-lab&type=Repositories&ref=searchresults');
51+
return fetch('https://api.github.com/search/repositories?q=starterkit+in:name+user:pattern-lab&sort=stars&order=desc', {
52+
method: 'GET',
53+
headers: {
54+
'Accept': 'application/json'
55+
}
56+
}).then(function (res) {
57+
var contentType = res.headers.get('content-type');
58+
if (contentType && contentType.indexOf('application/json') === -1) {
59+
throw new TypeError("StarterkitManager->listStarterkits: Not valid JSON");
60+
}
61+
return res.json()
62+
}).then(function (json) {
63+
if (!json.items || !Array.isArray(json.items)) {
64+
return false;
65+
}
66+
return json.items
67+
.map(function (repo) {
68+
return {name: repo.name, url: repo.html_url}
69+
});
70+
}).catch(function (err) {
71+
console.error(err);
72+
return false;
73+
});
4674
}
4775

4876
function packStarterkit() {
@@ -63,7 +91,7 @@ var starterkit_manager = function (config) {
6391
loadStarterKit(starterkitName, clean);
6492
},
6593
list_starterkits: function () {
66-
listStarterkits();
94+
return listStarterkits();
6795
},
6896
pack_starterkit: function () {
6997
packStarterkit();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"json5": "^0.5.0",
1212
"lodash": "~4.13.1",
1313
"markdown-it": "^6.0.1",
14+
"node-fetch": "^1.6.0",
1415
"patternengine-node-mustache": "^1.0.0"
1516
},
1617
"devDependencies": {

0 commit comments

Comments
 (0)