Skip to content
This repository has been archived by the owner on Jun 6, 2022. It is now read-only.

Commit

Permalink
use api for github
Browse files Browse the repository at this point in the history
  • Loading branch information
antiwinter committed Sep 12, 2019
1 parent ae21377 commit 67ffcf4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 68 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"@octokit/rest": "^16.28.9",
"async": "^3.0.1",
"chalk": "^2.4.2",
"commander": "^2.20.0",
Expand Down
121 changes: 54 additions & 67 deletions source/github.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const x = require('x-ray')({
filters: {
trim(v) {
return typeof v === 'string' ? v.trim() : v
},
time(v) {
return new Date(v).valueOf() / 1000
}
}
const Octokit = require('@octokit/rest')
const ok = new Octokit({
// auth: process.env.GITHUB_TOKEN
})
const g = require('got')
const log = console.log

let api = {
Expand All @@ -16,38 +11,6 @@ let api = {
$fcl: '/',
$scl: 'github.com',

$tip(ad, done) {
let page = api.$url + ad.key + `/tree/${!ad.branch ? 'master' : ad.branch}`
x(page, {
name: 'h1 strong a',
owner: 'h1 .author a',
author: 'h1 .author a',
stars: ['.social-count | trim'],
tease: '.commit-tease-sha | trim',
update: 'relative-time@datetime | time'
})((err, d) => {
// log('/tip', ad.key, err, d, '\n')
if (err || !d || !d.tease) {
done()
return
}

d.stars = d.stars[1]
d.version = [
{
name: d.tease,
time: d.update,
link: `${api.$url}${ad.key}/archive/${
!ad.branch ? 'master' : ad.branch
}.zip`
}
]
d.page = page

done(d)
})
},

info(ad, done) {
// install branch tip if branch provided

Expand All @@ -59,36 +22,60 @@ let api = {
ad.key = seg.join('/')
}

if (ad.branch) {
api.$tip(ad, done)
return
}
let owner = seg.shift()
let repo = seg.shift()

x(api.$url + ad.key + '/tags', {
name: 'h1 strong a',
owner: 'h1 .author a',
author: 'h1 .author a',
stars: ['.social-count | trim'],
version: x('.Box-row', [
{
name: 'h4 a | trim',
time: 'relative-time@datetime | time',
link: '[rel="nofollow"]@href'
// log('getting', { owner, repo, branch: ad.branch })

let fetch = () => {
let h = ad.branch
? ok.repos.getBranch({ owner, repo, branch: ad.branch })
: ok.repos.listTags({ owner, repo })
h.then(({ data }) => {
// log('got dat', data)
let d = {
name: repo,
owner,
author: owner,
page: api.$url + ad.key
}

d.version = ad.branch
? [
{
name: data.commit.sha.slice(0, 7),
link: `${api.$url}${ad.key}/archive/${
!ad.branch ? 'master' : ad.branch
}.zip`
}
]
: data.map(x => {
return {
name: x.name,
link: `${api.$url}${ad.key}/archive/${x.name}.zip`
}
})

if (!d.version || !d.version.length) {
ad.branch = 'master'
fetch()
return
}
])
})((err, d) => {
// log('/???', ad.key, err, d)
if (err || !d || !d.version.length) {
api.$tip(ad, done)
return
}

d.update = d.version[0].time
d.page = api.$url + ad.key
d.stars = d.stars[1]
let c = ad.branch ? data.commit : data[0].commit
g(c.url).then(res => {
res = JSON.parse(res.body)
// log('inner', res)
d.update = new Date(res.commit.committer.date).valueOf() / 1000

done(d)
})
}).catch(err => {
done()
})
}

done(d)
})
fetch()
}
}

Expand Down
27 changes: 26 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,24 @@
universal-user-agent "^3.0.0"
url-template "^2.0.8"

"@octokit/rest@^16.28.9":
version "16.28.9"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.28.9.tgz#ac8c5f3ff305e9e0a0989a5245e4286f057a95d7"
integrity sha512-IKGnX+Tvzt7XHhs8f4ajqxyJvYAMNX5nWfoJm4CQj8LZToMiaJgutf5KxxpxoC3y5w7JTJpW5rnWnF4TsIvCLA==
dependencies:
"@octokit/request" "^5.0.0"
"@octokit/request-error" "^1.0.2"
atob-lite "^2.0.0"
before-after-hook "^2.0.0"
btoa-lite "^1.0.0"
deprecation "^2.0.0"
lodash.get "^4.4.2"
lodash.set "^4.3.2"
lodash.uniq "^4.5.0"
octokit-pagination-methods "^1.1.0"
once "^1.4.0"
universal-user-agent "^4.0.0"

"@samverschueren/stream-to-observable@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz#ecdf48d532c58ea477acfcab80348424f8d0662f"
Expand Down Expand Up @@ -2914,7 +2932,7 @@ os-homedir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"

os-name@3.1.0, os-name@^3.0.0:
os-name@3.1.0, os-name@^3.0.0, os-name@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/os-name/-/os-name-3.1.0.tgz#dec19d966296e1cd62d701a5a66ee1ddeae70801"
integrity sha512-h8L+8aNjNcMpo/mAIBPn5PXCM16iyPGjHNWo6U1YO8sJTMHtEtyczI6QJnLoplswm6goopQkqc7OAnjhWcugVg==
Expand Down Expand Up @@ -3970,6 +3988,13 @@ universal-user-agent@^3.0.0:
dependencies:
os-name "^3.0.0"

universal-user-agent@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-4.0.0.tgz#27da2ec87e32769619f68a14996465ea1cb9df16"
integrity sha512-eM8knLpev67iBDizr/YtqkJsF3GK8gzDc6st/WKzrTuPtcsOKW/0IdL4cnMBsU69pOx0otavLWBDGTwg+dB0aA==
dependencies:
os-name "^3.1.0"

unzip-response@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/unzip-response/-/unzip-response-2.0.1.tgz#d2f0f737d16b0615e72a6935ed04214572d56f97"
Expand Down

0 comments on commit 67ffcf4

Please sign in to comment.