5232c2775b006fc8053b09c12048316c5dc0bb06
[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   'amd64', // first and most likely match
32   'arm64',
33   'x86',
34   'ppc64le',
35   'ppc64',
36   'armv7l',
37   'armv6l',
38   's390x'
39 ];
40 var archMap = {
41   //amd64: /(amd.?64|x64|[_\-]64)/i,
42   amd64: /(\b|_|amd|(dar)?win(dows)?|mac(os)?|linux|osx|x)64([_\-]?bit)?(\b|_)/i,
43   //x86: /(86)(\b|_)/i,
44   x86: /(\b|_|amd|(dar)?win(dows)?|mac(os)?|linux|osx|x)(86|32)([_\-]?bit)(\b|_)/i,
45   ppc64le: /(\b|_)(ppc64le)/i,
46   ppc64: /(\b|_)(ppc64)(\b|_)/i,
47   arm64: /(\b|_)((aarch|arm)64|arm)/i,
48   armv7l: /(\b|_)(armv?7l)/i,
49   armv6l: /(\b|_)(aarch32|armv?6l)/i,
50   s390x: /(\b|_)(s390x)/i
51 };
52 arches.forEach(function (name) {
53   maps.arches[name] = true;
54 });
55
56 function normalize(all) {
57   var supported = {
58     oses: {},
59     arches: {},
60     formats: {}
61   };
62
63   all.releases.forEach(function (rel) {
64     rel.version = rel.version.replace(/^v/i, '');
65     if (!rel.name) {
66       rel.name = rel.download.replace(/.*\//, '');
67     }
68     if (!rel.os) {
69       rel.os =
70         Object.keys(osMap).find(function (regKey) {
71           return osMap[regKey].test(rel.name || rel.download);
72         }) || 'unknown';
73     }
74     supported.oses[rel.os] = true;
75
76     if (!rel.arch) {
77       arches.some(function (regKey) {
78         var arch = (rel.name || rel.download).match(archMap[regKey]) && regKey;
79         if (arch) {
80           rel.arch = arch;
81           return true;
82         }
83       });
84     }
85     if (!rel.arch) {
86       if ('macos' === rel.os) {
87         rel.arch = 'amd64';
88       }
89     }
90     supported.arches[rel.arch] = true;
91
92     var tarExt;
93     if (!rel.ext) {
94       // pkg-v1.0.tar.gz => ['gz', 'tar', '0', 'pkg-v1']
95       // pkg-v1.0.tar => ['tar', '0' ,'pkg-v1']
96       // pkg-v1.0.zip => ['zip', '0', 'pkg-v1']
97       var exts = (rel.name || rel.download).split('.');
98       if (1 === exts.length) {
99         // for bare releases in the format of foo-linux-amd64
100         rel.ext = 'exe';
101       }
102       exts = exts.reverse().slice(0, 2);
103       if ('tar' === exts[1]) {
104         rel.ext = exts.reverse().join('.');
105         tarExt = 'tar';
106       } else if ('tgz' == exts[0]) {
107         rel.ext = 'tar.gz';
108         tarExt = 'tar';
109       } else {
110         rel.ext = exts[0];
111       }
112       if (/\-|linux|mac|os[_\-]?x|arm|amd|86|64|mip/i.test(rel.ext)) {
113         // for bare releases in the format of foo.linux-amd64
114         rel.ext = 'exe';
115       }
116     }
117     supported.formats[tarExt || rel.ext] = true;
118
119     if (all.download) {
120       rel.download = all.download.replace(/{{ download }}/, rel.download);
121     }
122   });
123
124   all.oses = Object.keys(supported.oses).filter(function (name) {
125     return maps.oses[name];
126   });
127   all.arches = Object.keys(supported.arches).filter(function (name) {
128     return maps.arches[name];
129   });
130   all.formats = Object.keys(supported.formats).filter(function (name) {
131     return maps.formats[name];
132   });
133
134   return all;
135 }
136
137 module.exports = normalize;
138 module.exports._debug = function (all) {
139   all = normalize(all);
140   all.releases = all.releases
141     .filter(function (r) {
142       return ['windows', 'macos', 'linux'].includes(r.os) && 'amd64' === r.arch;
143     })
144     .slice(0, 10);
145   return all;
146 };
147 // NOT in order of priority (which would be tar, xz, zip, ...)
148 module.exports.formats = formats;
149 module.exports.arches = arches;
150 module.exports.formatsMap = maps.formats;