UNPKG

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