-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (56 loc) · 1.86 KB
/
index.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
'use strict';
var path = require('path');
var engineVersion = process.versions.v8.split('.');
while (engineVersion.length < 4) engineVersion.push('0');
var moduleRoot = path.dirname(module.parent.filename);
delete require.cache[__filename];
var multiform = {
/**
* Parses the calling module's `.multiformrc` and returns the dist directory
* of the first build config that is suitable for the current platform.
*/
select: function select(filename) {
var i, l, build;
var config = require(path.join(moduleRoot, 'multiform.json'));
for (i = 0, l = config.builds.length; i < l; i++) {
build = config.builds[i];
if (multiform.compatible(build.version)) {
var dist = path.resolve(moduleRoot, build.dir || 'dist-' + i);
if (filename) dist = path.join(dist, filename);
return dist;
}
}
throw new Error('Multiform: Could not find a build suitable for this platform.');
},
/**
* Selects, requires and returns the correct build for the current platform.
*/
load: function load(filename) {
return require(multiform.select(filename));
},
/**
* Checks if the current V8 is >= the given version.
*/
compatible: function passes(version) {
if (version) {
var difference;
var targetVersion = version.split('.');
while (engineVersion.length < 4) engineVersion.push('0');
for (var i = 0; i < 4; i++) {
difference = (+targetVersion[i]) - (+engineVersion[i]);
if (difference > 0) return false;
if (difference < 0) return true;
}
return true;
}
return true;
},
/**
* Changes the module root. For when you need to require multiform from some
* file that is not in the project root adjacent to the dist directories.
*/
setModuleRoot: function setModuleRoot(root) {
moduleRoot = root;
},
};
module.exports = multiform;