-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathplatforms.js
123 lines (100 loc) · 3.91 KB
/
platforms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var _ = require('lodash');
var path = require('path');
var platforms = {
LINUX: 'linux',
LINUX_32: 'linux_32',
LINUX_64: 'linux_64',
OSX: 'osx',
OSX_32: 'osx_32',
OSX_64: 'osx_64',
WINDOWS: 'windows',
WINDOWS_32: 'windows_32',
WINDOWS_64: 'windows_64',
detect: detectPlatform
};
// Reduce a platfrom id to its type
function platformToType(platform) {
return _.first(platform.split('_'));
}
// Detect and normalize the platform name
function detectPlatform(platform) {
var name = platform.toLowerCase();
var prefix = "", suffix = "";
// Detect NuGet/Squirrel.Windows files
if (name == 'releases' || path.extname(name) == '.nupkg') return platforms.WINDOWS_32;
// Detect prefix: osx, widnows or linux
if (_.contains(name, 'win')
|| path.extname(name) == '.exe') prefix = platforms.WINDOWS;
if (_.contains(name, 'linux')
|| _.contains(name, 'ubuntu')
|| path.extname(name) == '.deb'
|| path.extname(name) == '.rpm') prefix = platforms.LINUX;
if (_.contains(name, 'mac')
|| _.contains(name, 'osx')
|| name.indexOf('darwin') >= 0
|| path.extname(name) == '.dmg') prefix = platforms.OSX;
// Detect suffix: 32 or 64
if (_.contains(name, '32')
|| _.contains(name, 'ia32')
|| _.contains(name, 'i386')) suffix = '32';
if (_.contains(name, '64') || _.contains(name, 'x64') || _.contains(name, 'amd64')) suffix = '64';
suffix = suffix || (prefix == platforms.OSX? '64' : '32');
return _.compact([prefix, suffix]).join('_');
}
function detectPlatformByUserAgent(useragent) {
if (useragent.isMac) return platforms.OSX;
else if (useragent.source.indexOf("WOW64") !== -1 || useragent.source.indexOf("Win64") !== -1) return platforms.WINDOWS_64;
else if (useragent.isWindows) return platforms.WINDOWS;
else if (useragent.isLinux) return platforms.LINUX;
else if (useragent.isLinux64) return platforms.LINUX_64;
else return null
}
// Satisfies a platform
function satisfiesPlatform(platform, list) {
if (_.contains(list, platform)) return true;
// By default, user 32bits version
if (_.contains(list+'_32', platform)) return true;
return false;
}
// Resolve a platform for a version
function resolveForVersion(version, platformID, opts) {
opts = _.defaults(opts || {}, {
// Order for filetype
filePreference: ['.exe', '.dmg', '.deb', '.rpm', '.zip', '.nupkg'],
wanted: null
});
// Prepare file prefs
if (opts.wanted) opts.filePreference = _.uniq([opts.wanted].concat(opts.filePreference));
// Normalize platform id
platformID = detectPlatform(platformID);
return _.chain(version.platforms)
.filter(function(pl) {
return pl.type.indexOf(platformID) === 0;
})
.sort(function(p1, p2) {
var result = 0;
// Compare by arhcitecture ("osx_64" > "osx")
if (p1.type.length > p2.type.length) result = -1;
else if (p2.type.length > p1.type.length) result = 1;
// Order by file type if samee architecture
if (result == 0) {
var ext1 = path.extname(p1.filename);
var ext2 = path.extname(p2.filename);
var pos1 = _.indexOf(opts.filePreference, ext1);
var pos2 = _.indexOf(opts.filePreference, ext2);
pos1 = pos1 == -1? opts.filePreference.length : pos1;
pos2 = pos2 == -1? opts.filePreference.length : pos2;
if (pos1 < pos2) result = -1;
else if (pos2 < pos1) result = 1;
}
return result;
})
.first()
.value()
}
module.exports = platforms;
module.exports.detect = detectPlatform;
module.exports.detectPlatformByUserAgent = detectPlatformByUserAgent;
module.exports.satisfies = satisfiesPlatform;
module.exports.toType = platformToType;
module.exports.resolve = resolveForVersion;