1 | 'use strict';
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const got = require('got');
|
4 | const git = require('./git');
|
5 | const fs = require("async-file");
|
6 | async 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 | }
|
18 | async 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 | }
|
26 | async function urlExists(url) {
|
27 | return got.head(url);
|
28 | }
|
29 | async 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 | }
|
44 | exports.createSourceBlob = createSourceBlob;
|