forked from wilix-team/iohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
154 lines (145 loc) · 4.73 KB
/
install.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const path = require('path');
const fs = require('fs');
const os = require('os');
const get = require('simple-get');
const pump = require('pump');
const tfs = require('tar-fs');
const zlib = require('zlib');
const pkg = require('./package.json');
const supportedTargets = require('./package.json').supportedTargets;
function onerror(err) {
throw err;
}
/**
* Download and Install prebuild
* @param runtime
* @param abi
* @param platform
* @param arch
* @param cb Callback
*/
function install(runtime, abi, platform, arch, cb) {
const essential = runtime + '-v' + abi + '-' + platform + '-' + arch;
const pkgVersion = pkg.version;
const currentPlatform = pkg.name + '-v' + pkgVersion + '-' + essential;
console.log('Downloading prebuild for platform:', currentPlatform);
let downloadUrl = 'https://github.com/WilixLead/iohook/releases/download/v' + pkgVersion + '/' + currentPlatform + '.tar.gz';
let reqOpts = {url: downloadUrl};
let tempFile = path.join(os.tmpdir(), 'prebuild.tar.gz');
let req = get(reqOpts, function(err, res) {
if (err) {
return onerror(err);
}
if (res.statusCode !== 200) {
if (res.statusCode === 404) {
console.error('Prebuild for current platform (' + currentPlatform + ') not found!');
console.error('Try to compile for your platform:');
console.error('# cd node_modules/iohook;');
console.error('# npm run compile');
console.error('');
return onerror('Prebuild for current platform (' + currentPlatform + ') not found!');
}
return onerror('Bad response from prebuild server. Code: ' + res.statusCode);
}
pump(res, fs.createWriteStream(tempFile), function(err) {
if (err) {
throw err;
}
let options = {
readable: true,
writable: true,
hardlinkAsFilesFallback: true
};
let binaryName;
let updateName = function(entry) {
if (/\.node$/i.test(entry.name)) binaryName = entry.name
};
let targetFile = path.join(__dirname, 'builds', essential);
let extract = tfs.extract(targetFile, options)
.on('entry', updateName);
pump(fs.createReadStream(tempFile), zlib.createGunzip(), extract, function(err) {
if (err) {
return onerror(err);
}
cb()
})
})
});
req.setTimeout(30 * 1000, function() {
req.abort()
})
}
/**
* Return options for iohook from package.json
* @return {Object}
*/
function optionsFromPackage(attempts) {
attempts = attempts || 2;
if (attempts > 5) {
console.log('Can\'t resolve main package.json file');
return {
targets: [],
platforms: [process.platform],
arches: [process.arch]
}
}
let mainPath = Array(attempts).join("../");
try {
const content = fs.readFileSync(path.join(__dirname, mainPath, 'package.json'), 'utf-8');
const packageJson = JSON.parse(content);
const opts = packageJson.iohook || {};
if (!opts.targets) {
opts.targets = []
}
if (!opts.platforms) opts.platforms = [process.platform];
if (!opts.arches) opts.arches = [process.arch];
return opts
} catch (e) {
return optionsFromPackage(attempts + 1);
}
}
const options = optionsFromPackage();
if (process.env.npm_config_targets) {
options.targets = options.targets.concat(process.env.npm_config_targets.split(','));
}
options.targets = options.targets.map(targetStr => targetStr.split('-'));
if (process.env.npm_config_targets === 'all') {
options.targets = supportedTargets.map(arr => [arr[0], arr[2]]);
options.platforms = ['win32', 'darwin', 'linux'];
options.arches = ['x64', 'ia32']
}
if (process.env.npm_config_platforms) {
options.platforms = options.platforms.concat(process.env.npm_config_platforms.split(','));
}
if (process.env.npm_config_arches) {
options.arches = options.arches.concat(process.env.npm_config_arches.split(','));
}
// Choice prebuilds for install
if (options.targets.length > 0) {
let chain = Promise.resolve();
options.targets.forEach(function(parts) {
let runtime = parts[0];
let abi = parts[1];
options.platforms.forEach(function(platform) {
options.arches.forEach(function(arch) {
if ((platform === 'darwin' || platform === 'linux') && arch === 'ia32') {
return;
}
chain = chain.then(function() {
return new Promise(function(resolve) {
console.log(runtime, abi, platform, arch);
install(runtime, abi, platform, arch, resolve)
})
})
})
})
})
} else {
const runtime = process.versions['electron'] ? 'electron' : 'node';
const abi = process.versions.modules;
const platform = process.platform;
const arch = process.arch;
install(runtime, abi, platform, arch, function() {
})
}