UNPKG

1.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("async-file");
4const cli_ux_1 = require("cli-ux");
5const git = require("./git");
6const got = require('got');
7async function uploadArchive(url, filePath) {
8 const request = got.stream.put(url, {
9 headers: {
10 'content-length': (await fs.stat(filePath)).size,
11 },
12 });
13 fs.createReadStream(filePath).pipe(request);
14 return new Promise((resolve, reject) => {
15 request.on('error', reject);
16 request.on('response', resolve);
17 });
18}
19async function prepareSource(ref, command) {
20 const filePath = await git.createArchive(ref);
21 const { body: source } = await command.heroku.post('/sources');
22 await uploadArchive(source.source_blob.put_url, filePath);
23 return Promise.resolve(source);
24}
25async function createSourceBlob(ref, command) {
26 try {
27 const githubRepository = await git.githubRepository();
28 const { user, repo } = githubRepository;
29 const { body: archiveLink } = await command.heroku.get(`https://kolkrabbi.heroku.com/github/repos/${user}/${repo}/tarball/${ref}`);
30 if (await command.heroku.request(archiveLink.archive_link, { method: 'HEAD' })) {
31 return archiveLink.archive_link;
32 }
33 }
34 catch (error) {
35 // the commit isn't in the repo, we will package the local git commit instead
36 cli_ux_1.ux.debug(`Commit not found in pipeline repository: ${error}`);
37 }
38 const sourceBlob = await prepareSource(ref, command);
39 return sourceBlob.source_blob.get_url;
40}
41exports.createSourceBlob = createSourceBlob;