UNPKG

4.69 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Fetches the latest release version of the cumulus message adapter
5 *
6 * @param {string} gitPath - path to the cumulus message adapter repo
7 * @returns {Promise.<string>} Promise resolution is string of latest github release, e.g. 'v0.0.1'
8 */
9let fetchLatestMessageAdapterRelease = (() => {
10 var _ref = _asyncToGenerator(function* (gitPath) {
11 const url = process.env.GITHUB_TOKEN ? `https://api.github.com/repos/${gitPath}/releases/latest?access_token=${process.env.GITHUB_TOKEN}` : `https://api.github.com/repos/${gitPath}/releases/latest`;
12
13 const response = yield got(url, {
14 json: true,
15 headers: {
16 'User-Agent': '@cumulus/deployment' // Required by Github API
17 }
18 });
19
20 return response.body.tag_name;
21 });
22
23 return function fetchLatestMessageAdapterRelease(_x) {
24 return _ref.apply(this, arguments);
25 };
26})();
27
28/**
29 * Determine the version of the cumulus-message-adapter to use
30 *
31 * @param {string} version - the cumulus-message-adapter version (default to null)
32 * @param {string} gitPath - path to the cumulus message adapter repo
33 * @returns {Promise.<string>} - the message adapter version
34 */
35
36
37function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
38
39const fs = require('fs-extra');
40const http = require('@cumulus/common/http');
41const got = require('got');
42const extract = require('extract-zip');
43
44/**
45 * `downloadZipfile` downloads zip file from remote location and stores on disk
46 *
47 * @param {string} fileUrl - URL file location
48 * @param {string} localFilename - Where to store file locally
49 * @returns {Promise} resolves when the download is completed
50 */
51function downloadZipfile(fileUrl, localFilename) {
52 return http.download(fileUrl, localFilename, {
53 headers: {
54 Accept: 'application/octet-stream',
55 'Content-Type': 'application/zip',
56 'Content-Transfer-Encoding': 'binary'
57 }
58 });
59}
60
61/**
62 * unzip a given zip file to the given destination
63 *
64 * @param {string} filename - the zip file to extract
65 * @param {string} dst - the destination to extract the file
66 * @returns {Promise.<string>} the path of the extracted zip
67 */
68function extractZipFile(filename, dst) {
69 // create the destination folder it doesn't exist
70 fs.mkdirpSync(dst);
71 return new Promise((resolve, reject) => {
72 extract(filename, { dir: dst }, err => {
73 if (err) return reject(err);
74 console.log(`${filename} extracted to ${dst}`);
75 return resolve(dst);
76 });
77 });
78}function messageAdapterVersion(version, gitPath) {
79 if (version) {
80 return Promise.resolve(version);
81 }
82 return fetchLatestMessageAdapterRelease(gitPath);
83}
84
85/**
86 * The Github URL of the cumulus-message-adapter zip file
87 *
88 * @param {string} version - the cumulus-message-adapter version (default to null)
89 * @param {string} gitPath - path to the cumulus message adapter repo
90 * @param {string} filename - the zip file to extract
91 * @returns {Promise.<string>} - the URL to fetch the cumulus-message-adapter from
92 */
93function messageAdapterUrl(version, gitPath, filename) {
94 return messageAdapterVersion(version, gitPath).then(ver => process.env.GITHUB_TOKEN ? `https://github.com/${gitPath}/releases/download/${ver}/${filename}?access_token=${process.env.GITHUB_TOKEN}` : `https://github.com/${gitPath}/releases/download/${ver}/${filename}`);
95}
96
97/**
98 * Determines which release version should be downloaded from
99 * cumulus-message-adapter repository and then downloads that file.
100 *
101 * @param {string} version - the cumulus-message-adapter version (default to null)
102 * @param {string} gitPath - path to the cumulus message adapter repo
103 * @param {string} filename - the zip file to extract
104 * @param {string} src - the path to where the zip file should be downloaded to
105 * @param {string} dest - the path to where the zip file should be extracted to
106 * @returns {Promise} returns the path of the extracted message adapter or an empty response
107 */
108function fetchMessageAdapter(version, gitPath, filename, src, dest) {
109 return messageAdapterUrl(version, gitPath, filename).then(url => downloadZipfile(url, src)).then(() => extractZipFile(src, dest));
110}
111
112module.exports = {
113 downloadZipfile,
114 extractZipFile,
115 fetchLatestMessageAdapterRelease,
116 messageAdapterVersion,
117 messageAdapterUrl,
118 fetchMessageAdapter
119};
\No newline at end of file