UNPKG

1.5 kBJavaScriptView Raw
1'use strict';
2Object.defineProperty(exports, "__esModule", { value: true });
3const got = require('got');
4const git = require('./git');
5const fs = require("async-file");
6async function uploadArchive(url, filePath) {
7 const request = got.stream.put(url, {
8 headers: {
9 'content-length': (await fs.stat(filePath)).size
10 }
11 });
12 fs.createReadStream(filePath).pipe(request);
13 return new Promise((resolve, reject) => {
14 request.on('error', reject);
15 request.on('response', resolve);
16 });
17}
18async function prepareSource(ref, command) {
19 const [filePath, source] = await [
20 git.createArchive(ref),
21 command.heroku.post('/sources', { body: command })
22 ];
23 await uploadArchive(source.source_blob.put_url, filePath);
24 return Promise.resolve(source);
25}
26async function urlExists(url) {
27 return got.head(url);
28}
29async function createSourceBlob(ref, command) {
30 try {
31 const githubRepository = await git.githubRepository();
32 const { user, repo } = githubRepository;
33 let { body: archiveLink } = await command.heroku.get(`https://kolkrabbi.heroku.com/github/repos/${user}/${repo}/tarball/${ref}`);
34 if (await urlExists(archiveLink.archive_link)) {
35 return archiveLink.archive_link;
36 }
37 }
38 catch (ex) {
39 command.error(ex);
40 }
41 const sourceBlob = await prepareSource(ref, command);
42 return sourceBlob.source_blob.get_url;
43}
44exports.createSourceBlob = createSourceBlob;