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