better error handling when vailable formats mismatch
[webi-installers/.git] / _webi / normalize.js
1 'use strict';
2
3 // this may need customizations between packages
4 var 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 var formats = ['zip', 'xz', 'tar', 'pkg', 'msi', 'git', 'exe', 'dmg'];
14 var formatsMap = {};
15 formats.forEach(function (ext) {
16   formatsMap[ext] = true;
17 });
18
19 // evaluation order matters
20 // (i.e. otherwise x86 and x64 can cross match)
21 var archArr = [
22   'amd64', // first and most likely match
23   'arm64',
24   'x86',
25   'ppc64le',
26   'ppc64',
27   'armv7l',
28   'armv6l',
29   's390x'
30 ];
31 var archMap = {
32   amd64: /(amd.?64|x64|[_\-]64)/i,
33   x86: /(86)(\b|_)/i,
34   ppc64le: /(\b|_)(ppc64le)/i,
35   ppc64: /(\b|_)(ppc64)(\b|_)/i,
36   arm64: /(\b|_)(arm64|arm)/i,
37   armv7l: /(\b|_)(armv?7l)/i,
38   armv6l: /(\b|_)(armv?6l)/i,
39   s390x: /(\b|_)(s390x)/i
40 };
41
42 function normalize(all) {
43   var supportedFormats = {};
44
45   all.releases.forEach(function (rel) {
46     supportedFormats[rel.ext] = true;
47     rel.version = rel.version.replace(/^v/i, '');
48     if (!rel.name) {
49       rel.name = rel.download.replace(/.*\//, '');
50     }
51     if (!rel.os) {
52       rel.os =
53         Object.keys(osMap).find(function (regKey) {
54           /* console.log(
55             'release os:',
56             regKey,
57             osMap[regKey],
58             osMap[regKey].test(rel.name || rel.download),
59             rel.name,
60             rel.download
61           );
62           // */
63           return osMap[regKey].test(rel.name || rel.download);
64         }) || 'unknown';
65     }
66
67     if (!rel.arch) {
68       archArr.some(function (regKey) {
69         //console.log('release arch:', rel.download, regKey, archMap[regKey]);
70         var arch = (rel.name || rel.download).match(archMap[regKey]) && regKey;
71         if (arch) {
72           rel.arch = arch;
73           return true;
74         }
75       })[0];
76     }
77
78     if (!rel.ext) {
79       // pkg-v1.0.tar.gz => ['gz', 'tar', '0', 'pkg-v1']
80       // pkg-v1.0.tar => ['tar', '0' ,'pkg-v1']
81       // pkg-v1.0.zip => ['zip', '0', 'pkg-v1']
82       var exts = (rel.name || rel.download).split('.').reverse().slice(0, 2);
83       var ext;
84       if ('tar' === exts[1]) {
85         rel.ext = exts.reverse().join('.');
86       } else if ('tgz' == exts[0]) {
87         rel.ext = 'tar.gz';
88       } else {
89         rel.ext = exts[0];
90       }
91     }
92
93     if (all.download) {
94       rel.download = all.download.replace(/{{ download }}/, rel.download);
95     }
96   });
97
98   all.formats = Object.keys(supportedFormats).filter(function (ext) {
99     return formatsMap[ext];
100   });
101
102   return all;
103 }
104
105 module.exports = normalize;
106 // NOT in order of priority (which would be tar, xz, zip, ...)
107 module.exports.formats = formats;
108 module.exports.arches = archArr;
109 module.exports.formatsMap = formatsMap;