refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / _common / brew.js
1 'use strict';
2
3 /**
4  * Gets a releases from 'brew'.
5  *
6  * @param request
7  * @param {string} formula
8  * @returns {PromiseLike<any> | Promise<any>}
9  */
10 function getAllReleases(request, formula) {
11   if (!formula) {
12     return Promise.reject('missing formula for brew');
13   }
14   return request({
15     url: 'https://formulae.brew.sh/api/formula/' + formula + '.json',
16     fail: true, // https://git.coolaj86.com/coolaj86/request.js/issues/2
17     json: true
18   })
19     .then(failOnBadStatus)
20     .then(function (resp) {
21       var ver = resp.body.versions.stable;
22       var dl = (
23         resp.body.bottle.stable.files.high_sierra ||
24         resp.body.bottle.stable.files.catalina
25       ).url.replace(new RegExp(ver.replace(/\./g, '\\.'), 'g'), '{{ v }}');
26       return [
27         {
28           version: ver,
29           download: dl.replace(/{{ v }}/g, ver)
30         }
31       ].concat(
32         resp.body.versioned_formulae.map(function (f) {
33           var ver = f.replace(/.*@/, '');
34           return {
35             version: ver,
36             download: dl
37           };
38         })
39       );
40     })
41     .catch(function (err) {
42       console.error('Error fetching MariaDB versions (brew)');
43       console.error(err);
44       return [];
45     });
46 }
47
48 function failOnBadStatus(resp) {
49   if (resp.statusCode >= 400) {
50     var err = new Error('Non-successful status code: ' + resp.statusCode);
51     err.code = 'ESTATUS';
52     err.response = resp;
53     throw err;
54   }
55   return resp;
56 }
57
58 module.exports = getAllReleases;
59
60 if (module === require.main) {
61   getAllReleases(require('@root/request'), 'mariadb').then(function (all) {
62     console.info(JSON.stringify(all, null, 2));
63   });
64 }