From 1475cc773d17df4caaee49aa36043b7fe65d338a Mon Sep 17 00:00:00 2001 From: Igor Klopov Date: Wed, 4 Jan 2017 14:21:15 +0300 Subject: [PATCH] Cloud class from cloud.js to reuse it for another owner/repo --- lib/cloud.js | 26 +++++++++++++++----------- lib/index.js | 6 ++++-- lib/upload.js | 6 ++++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/cloud.js b/lib/cloud.js index f6f2873..33f9da4 100644 --- a/lib/cloud.js +++ b/lib/cloud.js @@ -15,30 +15,33 @@ function uniqueName (name, names) { } } - const gh = new GitHub({ owner: 'zeit', repo: 'pkg-fetch' }); +export class Cloud { + constructor ({ owner, repo }) { + this.gh = new GitHub({ owner, repo }); + } - export async function upload (local, remote) { + async upload (local, remote) { const { tag } = remote; - let release = await gh.getRelease(tag); - if (!release) release = await gh.getReleaseDraft(tag); - if (!release) release = await gh.createRelease(tag); + let release = await this.gh.getRelease(tag); + if (!release) release = await this.gh.getReleaseDraft(tag); + if (!release) release = await this.gh.createRelease(tag); const names = release.assets.map(({ name }) => { assert(name); return name; }); const name = uniqueName(remote.name, names); - await gh.uploadAsset(local, release, name); + await this.gh.uploadAsset(local, release, name); } - export async function download (remote, local) { + async download (remote, local) { const { tag } = remote; const tempFile = local + '.downloading'; await mkdirp(path.dirname(tempFile)); const short = path.basename(local); - const ok = await gh.tryDirectly(tag, remote.name, tempFile, short); + const ok = await this.gh.tryDirectly(tag, remote.name, tempFile, short); if (!ok) { - let release = await gh.getRelease(tag); - if (!release) release = await gh.getReleaseDraft(tag); + let release = await this.gh.getRelease(tag); + if (!release) release = await this.gh.getReleaseDraft(tag); if (!release) return false; const assets = release.assets.filter(({ name }) => { assert(name); @@ -47,10 +50,11 @@ function uniqueName (name, names) { if (!assets.length) return false; assert(assets.length === 1); const asset = assets[0]; - await gh.downloadUrl(asset.url, tempFile, short); + await this.gh.downloadUrl(asset.url, tempFile, short); } await remove(local); await moveFile(tempFile, local); await remove(tempFile); return true; } +} diff --git a/lib/index.js b/lib/index.js index 375f761..5b9569d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,14 +3,16 @@ import { abiToNodeRange, // eslint-disable-line no-duplicate-imports hostPlatform, knownArchs, toFancyArch, toFancyPlatform } from './system.js'; import { localPlace, remotePlace } from './places.js'; import { log, wasReported } from './log.js'; +import { Cloud } from './cloud.js'; import build from './build.js'; -import { download } from './cloud.js'; import { exists } from 'fs-promise'; import patchesJson from '../patches/patches.json'; import path from 'path'; import semver from 'semver'; import { version } from '../package.json'; +const cloud = new Cloud({ owner: 'zeit', repo: 'pkg-fetch' }); + export async function need (opts = {}) { let { nodeRange, platform, arch, forceFetch, forceBuild } = opts; if (!nodeRange) throw wasReported('nodeRange not specified'); @@ -50,7 +52,7 @@ export async function need (opts = {}) { } if (!forceBuild) { log.info('Fetching base binaries to:', path.dirname(fetched)); - if (await download(remote, fetched)) return fetched; + if (await cloud.download(remote, fetched)) return fetched; fetchFailed = true; } if (fetchFailed) { diff --git a/lib/upload.js b/lib/upload.js index 036d735..4e7f11e 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -1,13 +1,15 @@ import { hostPlatform, targetArchs } from './system.js'; import { localPlace, remotePlace } from './places.js'; import { log, wasReported } from './log.js'; +import { Cloud } from './cloud.js'; import build from './build.js'; import patchesJson from '../patches/patches.json'; import path from 'path'; -import { upload } from './cloud.js'; import { verify } from './verify.js'; import { version } from '../package.json'; +const cloud = new Cloud({ owner: 'zeit', repo: 'pkg-fetch' }); + export function dontBuild (nodeVersion, targetPlatform, targetArch) { const major = nodeVersion.match(/^v?(\d+)/)[1] | 0; if (process.env.PKG_FETCH_ONLY_NODE7 && @@ -48,7 +50,7 @@ export async function main () { const remote = remotePlace({ arch: targetArch, nodeVersion, platform: hostPlatform, version }); try { - await upload(local, remote); + await cloud.upload(local, remote); } catch (error) { // TODO catch only network errors if (!error.wasReported) log.error(error);