forked from robolab-io/iohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
181 lines (163 loc) · 4.69 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const path = require('path');
const fs = require('fs');
const os = require('os');
const nugget = require('nugget');
const rc = require('rc');
const pump = require('pump');
const tfs = require('tar-fs');
const zlib = require('zlib');
const pkg = require('./package.json');
const supportedTargets = require('./package.json').supportedTargets;
const { optionsFromPackage } = require('./helpers');
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 = 'iohook-v' + pkgVersion + '-' + essential;
console.log('Downloading prebuild for platform:', currentPlatform);
let downloadUrl =
'https://github.com/robolab-io/iohook/releases/download/v' +
pkgVersion +
'/' +
currentPlatform +
'.tar.gz';
let nuggetOpts = {
dir: os.tmpdir(),
target: 'prebuild.tar.gz',
strictSSL: true,
};
let npmrc = {};
try {
rc('npm', npmrc);
} catch (error) {
console.warn('Error reading npm configuration: ' + error.message);
}
if (npmrc && npmrc.proxy) {
nuggetOpts.proxy = npmrc.proxy;
}
if (npmrc && npmrc['https-proxy']) {
nuggetOpts.proxy = npmrc['https-proxy'];
}
if (npmrc && npmrc['strict-ssl'] === false) {
nuggetOpts.strictSSL = false;
}
nugget(downloadUrl, nuggetOpts, function (errors) {
if (errors) {
const error = errors[0];
if (error.message.indexOf('404') === -1) {
onerror(error);
} else {
console.error(
'Prebuild for current platform (' + currentPlatform + ') not found!'
);
console.error('Try to build for your platform manually:');
console.error('# cd node_modules/iohook;');
console.error('# npm run build');
console.error('');
}
}
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(path.join(nuggetOpts.dir, nuggetOpts.target)),
zlib.createGunzip(),
extract,
function (err) {
if (err) {
return onerror(err);
}
cb();
}
);
});
}
const options = optionsFromPackage();
if (process.env.npm_config_targets) {
options.targets = options.targets.concat(
process.env.npm_config_targets.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', 'arm64'];
}
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(',')
);
}
if (!options.arches.includes('arm64')) {
options.arches.push('arm64');
}
// Choice prebuilds for install
if (options.targets.length > 0) {
let chain = Promise.resolve();
options.targets.forEach(function (target) {
if (typeof target === 'object') {
chain = chain.then(function () {
return new Promise(function (resolve) {
console.log(target.runtime, target.abi, target.platform, target.arch);
install(
target.runtime,
target.abi,
target.platform,
target.arch,
resolve
);
});
});
return;
}
let parts = target.split('-');
let runtime = parts[0];
let abi = parts[1];
options.platforms.forEach(function (platform) {
options.arches.forEach(function (arch) {
if (platform === 'darwin' && arch === 'ia32') {
return;
}
if (platform !== 'darwin' && arch === 'arm64') {
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 () {});
}