fix win -> windows
[webi-installers/.git] / _common / github.js
1 'use strict';
2
3 // this may need customizations between packages
4 const osMap = {
5   macos: /\b(apple|mac|darwin|iPhone|iOS|iPad)/i,
6   linux: /\b(linux)/i,
7   windows: /\b(win|microsoft|msft)/i,
8   sunos: /\b(sun)/i,
9   aix: /\b(aix)/i
10 };
11
12 // evaluation order matters
13 // (i.e. otherwise x86 and x64 can cross match)
14 var archArr = [
15   'amd64', // first and most likely match
16   'arm64',
17   'x86',
18   'ppc64le',
19   'ppc64',
20   'armv7l',
21   'armv6l',
22   's390x'
23 ];
24 var archMap = {
25   amd64: /(amd.?64|x64|[_\-]64)/i,
26   x86: /(86)\b/i,
27   ppc64le: /\b(ppc64le)/i,
28   ppc64: /\b(ppc64)\b/i,
29   arm64: /\b(arm64|arm)/i,
30   armv7l: /\b(armv?7l)/i,
31   armv6l: /\b(armv?6l)/i,
32   s390x: /\b(s390x)/i
33 };
34
35 var fileExtMap = {
36   deb: /\.deb$/i,
37   pkg: /\.pkg$/i,
38   exe: /\.exe$/i,
39   msi: /\.msi$/i,
40   zip: /\.zip$/i,
41   tar: /\.tar\..*$/i,
42   '7z': /\.7z$/i
43 };
44
45 /**
46  * Gets the releases for 'ripgrep'. This function could be trimmed down and made
47  * for use with any github release.
48  *
49  * @param request
50  * @param {string} owner
51  * @param {string} repo
52  * @returns {PromiseLike<any> | Promise<any>}
53  */
54 function getAllReleases(request, owner = 'BurntSushi', repo = 'ripgrep') {
55   if (!owner) {
56     return Promise.reject('missing owner for repo');
57   }
58   if (!repo) {
59     return Promise.reject('missing repo name');
60   }
61   return request({
62     url: `https://api.github.com/repos/${owner}/${repo}/releases`,
63     json: true
64   }).then((resp) => {
65     const gHubResp = resp.body;
66     const all = {
67       releases: [],
68       download: ''
69     };
70
71     gHubResp.forEach((release) => {
72       release['assets'].forEach((asset) => {
73         // set the primary download to the first of the releases
74         if (all.download === '') {
75           all.download = asset['browser_download_url'];
76         }
77
78         const name = asset['name'];
79         const os =
80           Object.keys(osMap).find(function (regKey) {
81             //console.log('github release os:', name, regKey, osMap[regKey]);
82             return osMap[regKey].test(name);
83           }) || 'unknown';
84         var arch;
85         archArr.some(function (regKey) {
86           //console.log('github release arch:', name, regKey, archMap[regKey]);
87           arch = name.match(archMap[regKey]) && regKey;
88           if (arch) {
89             return true;
90           }
91         })[0];
92
93         let fileExt = '';
94         Object.keys(fileExtMap).find((regKey) => {
95           const match = name.match(fileExtMap[regKey]);
96           if (match) {
97             fileExt = match[0];
98             return true;
99           }
100           return false;
101         });
102
103         all.releases.push({
104           download: asset['browser_download_url'],
105           date: (release['published_at'] || '').replace(/T.*/, ''),
106           version: release['tag_name'], // TODO tags aren't always semver / sensical
107           lts: /\b(lts)\b/.test(release['tag_name']),
108           channel: !release['prerelease'] ? 'stable' : 'beta',
109           ext: fileExt.slice(1),
110           arch,
111           os
112         });
113       });
114     });
115
116     return all;
117   });
118 }
119
120 module.exports = getAllReleases;
121
122 if (module === require.main) {
123   getAllReleases(require('@root/request'), 'BurntSushi', 'ripgrep').then(
124     function (all) {
125       console.log(JSON.stringify(all, null, 2));
126     }
127   );
128 }