also match 'os-x', 'os_x', and 'os x' as 'macos'
[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   x86: /(86)(\b|_)/i,
43   ppc64le: /(\b|_)(ppc64le)/i,
44   ppc64: /(\b|_)(ppc64)(\b|_)/i,
45   arm64: /(\b|_)(arm64|arm)/i,
46   armv7l: /(\b|_)(armv?7l)/i,
47   armv6l: /(\b|_)(armv?6l)/i,
48   s390x: /(\b|_)(s390x)/i
49 };
50 arches.forEach(function (name) {
51   maps.arches[name] = true;
52 });
53
54 function normalize(all) {
55   var supported = {
56     oses: {},
57     arches: {},
58     formats: {}
59   };
60
61   all.releases.forEach(function (rel) {
62     rel.version = rel.version.replace(/^v/i, '');
63     if (!rel.name) {
64       rel.name = rel.download.replace(/.*\//, '');
65     }
66     if (!rel.os) {
67       rel.os =
68         Object.keys(osMap).find(function (regKey) {
69           return osMap[regKey].test(rel.name || rel.download);
70         }) || 'unknown';
71     }
72     supported.oses[rel.os] = true;
73
74     if (!rel.arch) {
75       arches.some(function (regKey) {
76         var arch = (rel.name || rel.download).match(archMap[regKey]) && regKey;
77         if (arch) {
78           rel.arch = arch;
79           return true;
80         }
81       })[0];
82     }
83     supported.arches[rel.arch] = true;
84
85     if (!rel.ext) {
86       // pkg-v1.0.tar.gz => ['gz', 'tar', '0', 'pkg-v1']
87       // pkg-v1.0.tar => ['tar', '0' ,'pkg-v1']
88       // pkg-v1.0.zip => ['zip', '0', 'pkg-v1']
89       var exts = (rel.name || rel.download).split('.').reverse().slice(0, 2);
90       var ext;
91       if ('tar' === exts[1]) {
92         rel.ext = exts.reverse().join('.');
93       } else if ('tgz' == exts[0]) {
94         rel.ext = 'tar.gz';
95       } else {
96         rel.ext = exts[0];
97       }
98     }
99     supported.formats[rel.ext] = true;
100
101     if (all.download) {
102       rel.download = all.download.replace(/{{ download }}/, rel.download);
103     }
104   });
105
106   all.oses = Object.keys(supported.oses).filter(function (name) {
107     return maps.oses[name];
108   });
109   all.arches = Object.keys(supported.arches).filter(function (name) {
110     return maps.arches[name];
111   });
112   all.formats = Object.keys(supported.formats).filter(function (name) {
113     return maps.formats[name];
114   });
115
116   return all;
117 }
118
119 module.exports = normalize;
120 // NOT in order of priority (which would be tar, xz, zip, ...)
121 module.exports.formats = formats;
122 module.exports.arches = arches;
123 module.exports.formatsMap = maps.formats;