Skip to content

Commit 23d60aa

Browse files
committed
Merge pull request #40 from nodezoo/search
Fixes for search
2 parents f3ca385 + 6630563 commit 23d60aa

2 files changed

Lines changed: 118 additions & 114 deletions

File tree

github.js

Lines changed: 111 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -22,130 +22,132 @@ module.exports = function (options) {
2222
seneca.add('role:github,cmd:parse', cmd_parse)
2323
seneca.add('role:github,cmd:extract', cmd_extract)
2424

25-
function cmd_get (args, done) {
26-
var github_name = args.name
27-
var github_ent = seneca.make$('github')
28-
29-
var url = opts.registry + github_name
30-
// check if in the cache
31-
github_ent.load$(github_name, function (err, github) {
32-
if (err){
33-
return done(err)
34-
}
35-
if (github && !args.update) {
36-
return done(null, github)
37-
}
38-
else {
39-
// get giturl from npm
40-
Request.get(url, function (err, res, body) {
41-
if (err) {
42-
return done(err)
43-
}
44-
else if (_.isEmpty(JSON.parse(body))) {
45-
return done(err)
46-
}
47-
var data = JSON.parse(body)
48-
// take giturl from npm data
49-
seneca.act('role:github,cmd:extract', {data: data}, function (err, data) {
50-
if (err) {
51-
return done(err)
52-
}
53-
// parse username and repo from giturl
54-
var gitData = cmd_parse(data)
55-
56-
if (gitData){
57-
var user = gitData[1]
58-
var repo = gitData[2]
59-
gitUrl = 'http://github.com/' + user + '/' + repo
60-
}
61-
if (!user) {
62-
return done(err)
63-
}
64-
else {
65-
// get github data using github username and repo name
66-
seneca.act('role:github,cmd:query', {name: github_name, user: user, repo: repo}, done)
67-
}
68-
})
69-
})
70-
}
71-
})
25+
return {
26+
name: 'nodezoo-github'
7227
}
28+
}
7329

74-
function cmd_query (args, done) {
75-
var github_ent = seneca.make$('github')
76-
var github_name = args.name
77-
var user = args.user
78-
var repo = args.repo
79-
80-
github.authenticate({
81-
type: 'basic',
82-
username: opts.token,
83-
password: 'x-oauth-basic'
84-
})
85-
86-
github.repos.get({user: user, repo: repo}, function (err,repo) {
87-
if (err) {
88-
return done(err)
89-
}
90-
var data
91-
92-
if (repo) {
93-
data = {
94-
name: args.repo,
95-
user: args.user,
96-
repo: args.repo,
97-
stars: repo.stargazers_count,
98-
watches: repo.subscribers_count,
99-
forks: repo.forks_count,
100-
last: repo.pushed_at,
101-
url: gitUrl,
102-
gitClone: repo.clone_url
30+
function cmd_get (args, done) {
31+
var seneca = this
32+
33+
var github_name = args.name
34+
var github_ent = seneca.make$('github')
35+
36+
var url = opts.registry + github_name
37+
// check if in the cache
38+
github_ent.load$(github_name, function (err, github) {
39+
if (err){
40+
return done(err)
41+
}
42+
if (github && !args.update) {
43+
return done(null, github)
44+
}
45+
else {
46+
// get giturl from npm
47+
Request.get(url, function (err, res, body) {
48+
if (err) {
49+
return done(err)
50+
}
51+
else if (_.isEmpty(JSON.parse(body))) {
52+
return done(err)
10353
}
104-
// update the data if module exists in cache, if not create it
105-
github_ent.load$(github_name, function(err,github){
54+
var data = JSON.parse(body)
55+
// take giturl from npm data
56+
seneca.act('role:github,cmd:extract', {data: data}, function (err, data) {
10657
if (err) {
107-
return done(err);
58+
return done(err)
59+
}
60+
// parse username and repo from giturl
61+
var gitData = cmd_parse(data)
62+
63+
if (gitData){
64+
var user = gitData[1]
65+
var repo = gitData[2]
66+
gitUrl = 'http://github.com/' + user + '/' + repo
10867
}
109-
if (github) {
110-
return github.data$(data).save$(done);
68+
if (!user) {
69+
return done(err)
11170
}
11271
else {
113-
data.id$ = github_name
114-
github_ent.make$(data).save$(done)
115-
/* DEAN!!!!!!!!!!!!!
116-
This is where were are doing the override command but without the override
117-
possible issue here with it not having the object saved before
118-
the insert is called, not sure yet.
119-
*/
120-
seneca.act('role:search,cmd:insert',{data:data})
72+
// get github data using github username and repo name
73+
seneca.act('role:github,cmd:query', {name: github_name, user: user, repo: repo}, done)
12174
}
12275
})
76+
})
77+
}
78+
})
79+
}
12380

124-
}
125-
else return done()
126-
})
127-
}
81+
function cmd_query (args, done) {
82+
var seneca = this
12883

129-
function cmd_extract (args, done) {
130-
var data = args.data
131-
var dist_tags = data['dist-tags'] || {}
132-
var latest = ((data.versions || {})[dist_tags.latest]) || {}
133-
var repository = latest.repository || {}
84+
var github_ent = seneca.make$('github')
85+
var github_name = args.name
86+
var user = args.user
87+
var repo = args.repo
13488

135-
var out = {
136-
giturl: repository.url
89+
github.authenticate({
90+
type: 'basic',
91+
username: opts.token,
92+
password: 'x-oauth-basic'
93+
})
94+
95+
github.repos.get({user: user, repo: repo}, function (err,repo) {
96+
if (err) {
97+
return done(err)
13798
}
99+
var data
100+
101+
if (repo) {
102+
data = {
103+
name: args.repo,
104+
user: args.user,
105+
repo: args.repo,
106+
stars: repo.stargazers_count,
107+
watches: repo.subscribers_count,
108+
forks: repo.forks_count,
109+
last: repo.pushed_at,
110+
url: gitUrl,
111+
gitClone: repo.clone_url
112+
}
113+
// update the data if module exists in cache, if not create it
114+
github_ent.load$(github_name, function(err,github){
115+
if (err) {
116+
return done(err);
117+
}
118+
if (github) {
119+
return github.data$(data).save$(done);
120+
}
121+
else {
122+
data.id$ = github_name
123+
github_ent.make$(data).save$(done)
124+
}
125+
})
138126

139-
done(null, out)
127+
}
128+
else return done()
129+
})
130+
}
131+
132+
function cmd_extract (args, done) {
133+
var data = args.data
134+
var dist_tags = data['dist-tags'] || {}
135+
var latest = ((data.versions || {})[dist_tags.latest]) || {}
136+
var repository = latest.repository || {}
137+
138+
var out = {
139+
giturl: repository.url
140140
}
141141

142-
function cmd_parse (args) {
143-
var m = /[\/:]([^\/:]+?)[\/:]([^\/]+?)(\.git)*$/.exec(args.giturl)
144-
if (m) {
145-
return (m)
146-
}
147-
else {
148-
return null
149-
}
142+
done(null, out)
143+
}
144+
145+
function cmd_parse (args) {
146+
var m = /[\/:]([^\/:]+?)[\/:]([^\/]+?)(\.git)*$/.exec(args.giturl)
147+
if (m) {
148+
return (m)
149+
}
150+
else {
151+
return null
150152
}
151153
}

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@
3030
},
3131
"license": "MIT",
3232
"dependencies": {
33-
"code": "2.1.0",
3433
"eraro": "0.4.1",
35-
"eslint-config-seneca": "1.1.2",
36-
"eslint-plugin-hapi": "4.0.0",
37-
"eslint-plugin-standard": "1.3.1",
3834
"github": "0.2.2",
39-
"lab": "8.2.0",
4035
"lodash": "4.6.1",
4136
"request": "2.69.0",
4237
"seneca": "2.0.0",
@@ -47,5 +42,12 @@
4742
"toolbag": "2.0.0",
4843
"toolbag-plugin-stats-collector": "1.0.0",
4944
"toolbag-plugin-udp-reporter": "1.0.0"
45+
},
46+
"devDependencies": {
47+
"code": "2.1.0",
48+
"eslint-config-seneca": "1.1.2",
49+
"eslint-plugin-hapi": "4.0.0",
50+
"eslint-plugin-standard": "1.3.1",
51+
"lab": "8.2.0"
5052
}
5153
}

0 commit comments

Comments
 (0)