make prettier
[webi-installers/.git] / _common / normalize.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 function normalize(all) {
36   all.releases.forEach(function (rel) {
37     if (!rel.os) {
38       rel.os =
39         Object.keys(osMap).find(function (regKey) {
40           //console.log('release os:', rel.download, regKey, osMap[regKey]);
41           return osMap[regKey].test(rel.download);
42         }) || 'unknown';
43     }
44
45     if (!rel.arch) {
46       archArr.some(function (regKey) {
47         //console.log('release arch:', rel.download, regKey, archMap[regKey]);
48         var arch = rel.download.match(archMap[regKey]) && regKey;
49         if (arch) {
50           rel.arch = arch;
51           return true;
52         }
53       })[0];
54     }
55
56     if (!rel.ext) {
57       // pkg-v1.0.tar.gz => ['gz', 'tar', '0', 'pkg-v1']
58       // pkg-v1.0.tar => ['tar', '0' ,'pkg-v1']
59       // pkg-v1.0.zip => ['zip', '0', 'pkg-v1']
60       var exts = rel.download.split('.').reverse().slice(0, 2);
61       var ext;
62       if ('tar' === exts[1]) {
63         rel.ext = exts.reverse().join('.');
64       } else if ('tgz' == exts[0]) {
65         rel.ext = 'tar.gz';
66       } else {
67         rel.ext = exts[0];
68       }
69     }
70
71     if (all.download) {
72       rel.download = all.download.replace(/{{ download }}/, rel.download);
73     }
74   });
75   return all;
76 }
77
78 module.exports = normalize;