Cloud class from cloud.js to reuse it for another owner/repo

This commit is contained in:
Igor Klopov 2017-01-04 14:21:15 +03:00
parent d78acc18a5
commit 1475cc773d
3 changed files with 23 additions and 15 deletions

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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);