bugfix: update arh waterfall to properly detect armv7 vs arm64
[webi-installers/.git] / _webi / normalize.js
1 'use strict';
2
3 // this may need customizations between packages
4 var osMap = {
5   macos: /(\b|_)(apple|os(\s_-)?x\b|mac|darwin|iPhone|iOS|iPad)/i,
6   linux: /(\b|_)(linux)/i,
7   freebsd: /(\b|_)(freebsd)/i,
8   windows: /(\b|_)(win|microsoft|msft)/i,
9   sunos: /(\b|_)(sun)/i,
10   aix: /(\b|_)(aix)/i
11 };
12
13 var maps = {
14   oses: {},
15   arches: {},
16   formats: {}
17 };
18
19 Object.keys(osMap).forEach(function (name) {
20   maps.oses[name] = true;
21 });
22
23 var formats = ['zip', 'xz', 'tar', 'pkg', 'msi', 'git', 'exe', 'dmg'];
24 formats.forEach(function (name) {
25   maps.formats[name] = true;
26 });
27
28 // evaluation order matters
29 // (i.e. otherwise x86 and x64 can cross match)
30 var arches = [
31   // arm 7 cannot be confused with arm64
32   'armv7l',
33   // amd64 is more likely than arm64
34   'amd64',
35   // arm6 has the same prefix as arm64
36   'armv6l',
37   // arm64 is more likely than arm6, and should be the default
38   'arm64',
39   'x86',
40   'ppc64le',
41   'ppc64',
42   's390x'
43 ];
44 // Used for detecting system arch from package download url, for example:
45 //
46 // https://git.com/org/foo/releases/v0.7.9/foo-aarch64-linux-musl.tar.gz
47 // https://git.com/org/foo/releases/v0.7.9/foo-arm-linux-musleabihf.tar.gz
48 // https://git.com/org/foo/releases/v0.7.9/foo-armv7-linux-musleabihf.tar.gz
49 // https://git.com/org/foo/releases/v0.7.9/foo-x86_64-linux-musl.tar.gz
50 //
51 var archMap = {
52   armv7l: /(\b|_)(armv?7l?)/i,
53   //amd64: /(amd.?64|x64|[_\-]64)/i,
54   amd64:
55     /(\b|_|amd|(dar)?win(dows)?|mac(os)?|linux|osx|x)64([_\-]?bit)?(\b|_)/i,
56   //x86: /(86)(\b|_)/i,
57   armv6l: /(\b|_)(aarch32|armv?6l?)(\b|_)/i,
58   arm64: /(\b|_)((aarch|arm)64|arm)/i,
59   x86: /(\b|_|amd|(dar)?win(dows)?|mac(os)?|linux|osx|x)(86|32)([_\-]?bit)(\b|_)/i,
60   ppc64le: /(\b|_)(ppc64le)/i,
61   ppc64: /(\b|_)(ppc64)(\b|_)/i,
62   s390x: /(\b|_)(s390x)/i
63 };
64 arches.forEach(function (name) {
65   maps.arches[name] = true;
66 });
67
68 function normalize(all) {
69   var supported = {
70     oses: {},
71     arches: {},
72     formats: {}
73   };
74
75   all.releases.forEach(function (rel) {
76     rel.version = rel.version.replace(/^v/i, '');
77     if (!rel.name) {
78       rel.name = rel.download.replace(/.*\//, '');
79     }
80     if (!rel.os) {
81       rel.os =
82         Object.keys(osMap).find(function (regKey) {
83           return osMap[regKey].test(rel.name || rel.download);
84         }) || 'unknown';
85     }
86     // Hacky-doo for musl
87     // TODO some sort of glibc vs musl tag?
88     if (!rel._musl) {
89       if (/(\b|\.|_|-)(musl)(\b|\.|_|-)/.test(rel.download)) {
90         rel._musl = true;
91       }
92     }
93     supported.oses[rel.os] = true;
94
95     if (!rel.arch) {
96       arches.some(function (regKey) {
97         var arch = (rel.name || rel.download).match(archMap[regKey]) && regKey;
98         if (arch) {
99           rel.arch = arch;
100           return true;
101         }
102       });
103     }
104     if (!rel.arch) {
105       if ('macos' === rel.os) {
106         rel.arch = 'amd64';
107       }
108     }
109     supported.arches[rel.arch] = true;
110
111     var tarExt;
112     if (!rel.ext) {
113       // pkg-v1.0.tar.gz => ['gz', 'tar', '0', 'pkg-v1']
114       // pkg-v1.0.tar => ['tar', '0' ,'pkg-v1']
115       // pkg-v1.0.zip => ['zip', '0', 'pkg-v1']
116       var exts = (rel.name || rel.download).split('.');
117       if (1 === exts.length) {
118         // for bare releases in the format of foo-linux-amd64
119         rel.ext = 'exe';
120       }
121       exts = exts.reverse().slice(0, 2);
122       if ('tar' === exts[1]) {
123         rel.ext = exts.reverse().join('.');
124         tarExt = 'tar';
125       } else if ('tgz' === exts[0]) {
126         rel.ext = 'tar.gz';
127         tarExt = 'tar';
128       } else {
129         rel.ext = exts[0];
130       }
131       if (/\-|linux|mac|os[_\-]?x|arm|amd|86|64|mip/i.test(rel.ext)) {
132         // for bare releases in the format of foo.linux-amd64
133         rel.ext = 'exe';
134       }
135     }
136     supported.formats[tarExt || rel.ext] = true;
137
138     if (all.download) {
139       rel.download = all.download.replace(/{{ download }}/, rel.download);
140     }
141   });
142
143   all.oses = Object.keys(supported.oses).filter(function (name) {
144     return maps.oses[name];
145   });
146   all.arches = Object.keys(supported.arches).filter(function (name) {
147     return maps.arches[name];
148   });
149   all.formats = Object.keys(supported.formats).filter(function (name) {
150     return maps.formats[name];
151   });
152
153   return all;
154 }
155
156 module.exports = normalize;
157 module.exports._debug = function (all) {
158   all = normalize(all);
159   all.releases = all.releases
160     .filter(function (r) {
161       return ['windows', 'macos', 'linux'].includes(r.os) && 'amd64' === r.arch;
162     })
163     .slice(0, 10);
164   return all;
165 };
166 // NOT in order of priority (which would be tar, xz, zip, ...)
167 module.exports.formats = formats;
168 module.exports.arches = arches;
169 module.exports.formatsMap = maps.formats;