1 | const Octokit = require('@octokit/rest')
|
2 | const ok = new Octokit({
|
3 | auth: process.env.GITHUB_TOKEN
|
4 | })
|
5 | const log = console.log
|
6 |
|
7 | let api = {
|
8 | $url: 'https://github.com/',
|
9 | $lcl: /github\.com\/(.*)$/,
|
10 | $fcl: '/',
|
11 | $scl: 'github.com',
|
12 |
|
13 | info(ad, done) {
|
14 |
|
15 |
|
16 | let seg = ad.key.split('/')
|
17 |
|
18 | if (seg.length > 2) {
|
19 | ad.branch = seg.pop()
|
20 | for (; seg.length > 2; seg.pop());
|
21 | ad.key = seg.join('/')
|
22 | }
|
23 |
|
24 | let owner = seg.shift()
|
25 | let repo = seg.shift()
|
26 |
|
27 |
|
28 |
|
29 | let fetch = () => {
|
30 | let h = ad.branch
|
31 | ? ok.repos.getBranch({ owner, repo, branch: ad.branch })
|
32 | : ok.repos.listTags({ owner, repo })
|
33 | h.then(err => {
|
34 |
|
35 | let data = err.data
|
36 | let d = {
|
37 | name: repo,
|
38 | owner,
|
39 | author: owner,
|
40 | page: api.$url + ad.key
|
41 | }
|
42 |
|
43 | d.version = ad.branch
|
44 | ? [
|
45 | {
|
46 | name: data.commit.sha.slice(0, 7),
|
47 | link: `${api.$url}${ad.key}/archive/${
|
48 | !ad.branch ? 'master' : ad.branch
|
49 | }.zip`
|
50 | }
|
51 | ]
|
52 | : data.map(x => {
|
53 | return {
|
54 | name: x.name,
|
55 | link: `${api.$url}${ad.key}/archive/${x.name}.zip`
|
56 | }
|
57 | })
|
58 |
|
59 | if (!d.version || !d.version.length) {
|
60 | ad.branch = 'master'
|
61 | fetch()
|
62 | return
|
63 | }
|
64 |
|
65 | let c = ad.branch ? data.commit : data[0].commit
|
66 |
|
67 | ok.git
|
68 | .getCommit({
|
69 | owner,
|
70 | repo,
|
71 | commit_sha: c.sha
|
72 | })
|
73 | .then(({ data }) => {
|
74 |
|
75 | d.update = new Date(data.committer.date).valueOf() / 1000
|
76 | done(d)
|
77 | })
|
78 | }).catch(err => {
|
79 | if (err) {
|
80 | let msg = err.toString()
|
81 | if (typeof msg === 'string' && msg.match(/rate limit/)) {
|
82 | log(
|
83 | '\nThe [github] API has reached its rate limits. You can either create a GITHUB_TOKEN env, or try another time after an hour.'
|
84 | )
|
85 | log(
|
86 | 'To aquire a GITHUB_TOKEN, goto https://github.com/settings/tokens, and click "Generate new token"\n'
|
87 | )
|
88 | }
|
89 | }
|
90 | done()
|
91 | })
|
92 | }
|
93 |
|
94 | fetch()
|
95 | }
|
96 | }
|
97 |
|
98 | module.exports = api
|