Skip to content

Commit af68abe

Browse files
change response for seneca actions
1 parent 31b6afb commit af68abe

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

lib/github.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,27 @@ function cmdGet (msg, done) {
3434

3535
context.log.debug(`Processing module: ${moduleName}`)
3636
cache.load$(moduleName, (err, github) => {
37-
if (err) return done(err)
37+
if (err) {
38+
context.log.debug(`Cannot load from cache module ${moduleName}, try now to get it remotely`)
39+
}
3840

3941
if (github && !msg.update) {
40-
return done(null, github.data$(github))
42+
return done(null, {ok: true, data: github.data$(github)})
4143
}
4244

4345
Request.get({url: registry, gzip: true}, (err, res, body) => {
44-
if (err) return done(err)
46+
if (err) {
47+
return done(err)
48+
}
4549

4650
var data = null
4751

48-
try { data = JSON.parse(body) }
49-
catch (e) { return done(e) }
52+
try {
53+
data = JSON.parse(body)
54+
}
55+
catch (e) {
56+
return done(e)
57+
}
5058

5159
var distTags = data['dist-tags'] || {}
5260
var latest = ((data.versions || {})[distTags.latest]) || {}
@@ -66,17 +74,17 @@ function cmdGet (msg, done) {
6674
}
6775

6876
if (!params.user || !params.repo) {
69-
return done(new Error(`module ${moduleName} not found on github`))
77+
return done(null, {ok: false, err: new Error(`module ${moduleName} not found on github`)})
7078
}
7179

7280
queryGithub(params, done)
7381
}
7482
else {
75-
return done(new Error(`invalid github url: ${url}, for module: ${moduleName}`))
83+
return done(null, {ok: false, err: new Error(`invalid github url: ${url}, for module: ${moduleName}`)})
7684
}
7785
}
7886
else {
79-
return done(new Error(`module ${moduleName} not found on github`))
87+
return done(null, {ok: false, err: new Error(`module ${moduleName} not found on github`)})
8088
}
8189
})
8290
})
@@ -104,7 +112,8 @@ function cmdGet (msg, done) {
104112
github.repos.getReadme({user: params.user, repo: params.repo}, (err, readme) => {
105113
if (err) {
106114
context.log.debug(`Read readme for ${moduleName} get error: ${err}`)
107-
return cb(err)
115+
// don't report error, as we want to search for other information, even if Readme is not loaded
116+
return cb(null, false)
108117
}
109118

110119
if (!readme || !readme.content) {
@@ -117,7 +126,8 @@ function cmdGet (msg, done) {
117126
}, (err, response) => {
118127
if (err && !response) {
119128
context.log.debug(`Render readme for ${moduleName} error: ${err}`)
120-
return cb(err)
129+
// don't report error, as we want to search for other information, even if Readme is not loaded
130+
return cb(null, false)
121131
} // API fails expecting a JSON object
122132

123133
cb(null, response.data)
@@ -131,22 +141,23 @@ function cmdGet (msg, done) {
131141
context.log.debug(`Read Github pull requests for ${moduleName} get error: ${err}`)
132142
}
133143

134-
cb(err, data)
144+
// don't report error, as we might have useful data
145+
return cb()
135146
})
136147
}
137148
}, (err, results) => {
138149
if (err) {
139150
context.log.debug(`Read pull requests for ${moduleName} got error: ${err}`)
140-
return complete(err)
151+
return done(null, {ok: false, err: err})
141152
}
142153
var data = {
143154
name: params.repo || '',
144155
user: params.user || '',
145156
repo: params.repo || '',
146-
stars: results.getRepository.stargazers_count || 0,
147-
watches: results.getRepository.subscribers_count || 0,
148-
forks: results.getRepository.forks_count || 0,
149-
last: results.getRepository.pushed_at || '',
157+
stars: results.getRepository ? results.getRepository.stargazers_count || 0 : 0,
158+
watches: results.getRepository ? results.getRepository.subscribers_count || 0 : 0,
159+
forks: results.getRepository ? results.getRepository.forks_count || 0 : 0,
160+
last: results.getRepository ? results.getRepository.pushed_at || '' : '',
150161
urlRepo: 'https://github.com/' + params.user + '/' + params.repo,
151162
urlClone: 'git+https://github.com/' + params.user + '/' + params.repo + '.git',
152163
urlSsh: '[email protected]:' + params.user + '/' + params.repo + '.git',
@@ -174,7 +185,7 @@ function cmdGet (msg, done) {
174185
context.log.debug(`Save Github data for ${moduleName} got error: ${err}`)
175186
return done(err)
176187
}
177-
else done(null, data ? data.data$(data) : data)
188+
else done(null, {ok: true, data: data ? data.data$(data) : data})
178189
}
179190
})
180191
}
@@ -184,11 +195,16 @@ function aliasGet (msg, done) {
184195
var seneca = this
185196
var payload = {name: msg.name}
186197

187-
seneca.act('role:github,cmd:get', payload, (err, data) => {
188-
if (err) return done(err)
198+
seneca.act('role:github,cmd:get', payload, (err, res) => {
199+
if (err) {
200+
return done(null, {ok: false, err: err})
201+
}
202+
203+
if (res && res.ok) {
204+
payload.data = res.data
205+
seneca.act('role:info,res:part,part:github', payload)
206+
}
189207

190-
payload.data = data
191-
seneca.act('role:info,res:part,part:github', payload)
192208
done(null, {ok: true})
193209
})
194210
}

0 commit comments

Comments
 (0)