From 67ffcf4c0c728a511105b13dd324d4c2301c30d6 Mon Sep 17 00:00:00 2001 From: warits Date: Thu, 12 Sep 2019 14:26:25 +0800 Subject: [PATCH] use api for github --- package.json | 1 + source/github.js | 121 +++++++++++++++++++++-------------------------- yarn.lock | 27 ++++++++++- 3 files changed, 81 insertions(+), 68 deletions(-) diff --git a/package.json b/package.json index a42725f..e64bd96 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "@octokit/rest": "^16.28.9", "async": "^3.0.1", "chalk": "^2.4.2", "commander": "^2.20.0", diff --git a/source/github.js b/source/github.js index ea2d3e8..bb345c3 100644 --- a/source/github.js +++ b/source/github.js @@ -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 = { @@ -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 @@ -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() } } diff --git a/yarn.lock b/yarn.lock index c411961..5d221fd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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== @@ -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"