302952f8f28723a99473617cc57dda5b2565efa4
[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   freebsd: /(\b|_)(freebsd)/i,
8   windows: /(\b|_)(win|microsoft|msft)/i,
9   sunos: /(\b|_)(sun)/i,
10   aix: /(\b|_)(aix)/i
11 };
12
13 // evaluation order matters
14 // (i.e. otherwise x86 and x64 can cross match)
15 var archArr = [
16   'amd64', // first and most likely match
17   'arm64',
18   'x86',
19   'ppc64le',
20   'ppc64',
21   'armv7l',
22   'armv6l',
23   's390x'
24 ];
25 var archMap = {
26   amd64: /(amd.?64|x64|[_\-]64)/i,
27   x86: /(86)(\b|_)/i,
28   ppc64le: /(\b|_)(ppc64le)/i,
29   ppc64: /(\b|_)(ppc64)(\b|_)/i,
30   arm64: /(\b|_)(arm64|arm)/i,
31   armv7l: /(\b|_)(armv?7l)/i,
32   armv6l: /(\b|_)(armv?6l)/i,
33   s390x: /(\b|_)(s390x)/i
34 };
35
36 function normalize(all) {
37   all.releases.forEach(function (rel) {
38     rel.version = rel.version.replace(/^v/i, '');
39     if (!rel.name) {
40       rel.name = rel.download.replace(/.*\//, '');
41     }
42     if (!rel.os) {
43       console.log('name:', rel.name);
44       rel.os =
45         Object.keys(osMap).find(function (regKey) {
46           console.log('release os:', regKey, osMap[regKey], osMap[regKey].test(rel.name || rel.download), rel.name, rel.download);
47           return osMap[regKey].test(rel.name || rel.download);
48         }) || 'unknown';
49     }
50
51     if (!rel.arch) {
52       archArr.some(function (regKey) {
53         //console.log('release arch:', rel.download, regKey, archMap[regKey]);
54         var arch = (rel.name || rel.download).match(archMap[regKey]) && regKey;
55         if (arch) {
56           rel.arch = arch;
57           return true;
58         }
59       })[0];
60     }
61
62     if (!rel.ext) {
63       // pkg-v1.0.tar.gz => ['gz', 'tar', '0', 'pkg-v1']
64       // pkg-v1.0.tar => ['tar', '0' ,'pkg-v1']
65       // pkg-v1.0.zip => ['zip', '0', 'pkg-v1']
66       var exts = (rel.name || rel.download).split('.').reverse().slice(0, 2);
67       var ext;
68       if ('tar' === exts[1]) {
69         rel.ext = exts.reverse().join('.');
70       } else if ('tgz' == exts[0]) {
71         rel.ext = 'tar.gz';
72       } else {
73         rel.ext = exts[0];
74       }
75     }
76
77     if (all.download) {
78       rel.download = all.download.replace(/{{ download }}/, rel.download);
79     }
80   });
81   return all;
82 }
83
84 module.exports = normalize;