1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 | const os = require('os');
|
6 | const nugget = require('nugget');
|
7 | const rc = require('rc');
|
8 | const pump = require('pump');
|
9 | const tfs = require('tar-fs');
|
10 | const zlib = require('zlib');
|
11 | const pkg = require('./package.json');
|
12 | const supportedTargets = require('./package.json').supportedTargets;
|
13 | const { optionsFromPackage } = require('./helpers');
|
14 |
|
15 | function onerror(err) {
|
16 | throw err;
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function install(runtime, abi, platform, arch, cb) {
|
28 | const essential = runtime + '-v' + abi + '-' + platform + '-' + arch;
|
29 | const pkgVersion = pkg.version;
|
30 | const currentPlatform = 'iohook-v' + pkgVersion + '-' + essential;
|
31 |
|
32 | console.log('Downloading prebuild for platform:', currentPlatform);
|
33 | let downloadUrl =
|
34 | 'https://github.com/wilix-team/iohook/releases/download/v' +
|
35 | pkgVersion +
|
36 | '/' +
|
37 | currentPlatform +
|
38 | '.tar.gz';
|
39 |
|
40 | let nuggetOpts = {
|
41 | dir: os.tmpdir(),
|
42 | target: 'prebuild.tar.gz',
|
43 | strictSSL: true,
|
44 | };
|
45 |
|
46 | let npmrc = {};
|
47 |
|
48 | try {
|
49 | rc('npm', npmrc);
|
50 | } catch (error) {
|
51 | console.warn('Error reading npm configuration: ' + error.message);
|
52 | }
|
53 |
|
54 | if (npmrc && npmrc.proxy) {
|
55 | nuggetOpts.proxy = npmrc.proxy;
|
56 | }
|
57 |
|
58 | if (npmrc && npmrc['https-proxy']) {
|
59 | nuggetOpts.proxy = npmrc['https-proxy'];
|
60 | }
|
61 |
|
62 | if (npmrc && npmrc['strict-ssl'] === false) {
|
63 | nuggetOpts.strictSSL = false;
|
64 | }
|
65 |
|
66 | nugget(downloadUrl, nuggetOpts, function (errors) {
|
67 | if (errors) {
|
68 | const error = errors[0];
|
69 |
|
70 | if (error.message.indexOf('404') === -1) {
|
71 | onerror(error);
|
72 | } else {
|
73 | console.error(
|
74 | 'Prebuild for current platform (' + currentPlatform + ') not found!'
|
75 | );
|
76 | console.error('Try to build for your platform manually:');
|
77 | console.error('# cd node_modules/iohook;');
|
78 | console.error('# npm run build');
|
79 | console.error('');
|
80 | }
|
81 | }
|
82 |
|
83 | let options = {
|
84 | readable: true,
|
85 | writable: true,
|
86 | hardlinkAsFilesFallback: true,
|
87 | };
|
88 |
|
89 | let binaryName;
|
90 | let updateName = function (entry) {
|
91 | if (/\.node$/i.test(entry.name)) binaryName = entry.name;
|
92 | };
|
93 | let targetFile = path.join(__dirname, 'builds', essential);
|
94 | let extract = tfs.extract(targetFile, options).on('entry', updateName);
|
95 | pump(
|
96 | fs.createReadStream(path.join(nuggetOpts.dir, nuggetOpts.target)),
|
97 | zlib.createGunzip(),
|
98 | extract,
|
99 | function (err) {
|
100 | if (err) {
|
101 | return onerror(err);
|
102 | }
|
103 | cb();
|
104 | }
|
105 | );
|
106 | });
|
107 | }
|
108 |
|
109 | const options = optionsFromPackage();
|
110 |
|
111 | if (process.env.npm_config_targets) {
|
112 | options.targets = options.targets.concat(
|
113 | process.env.npm_config_targets.split(',')
|
114 | );
|
115 | }
|
116 | if (process.env.npm_config_targets === 'all') {
|
117 | options.targets = supportedTargets.map((arr) => [arr[0], arr[2]]);
|
118 | options.platforms = ['win32', 'darwin', 'linux'];
|
119 | options.arches = ['x64', 'ia32'];
|
120 | }
|
121 | if (process.env.npm_config_platforms) {
|
122 | options.platforms = options.platforms.concat(
|
123 | process.env.npm_config_platforms.split(',')
|
124 | );
|
125 | }
|
126 | if (process.env.npm_config_arches) {
|
127 | options.arches = options.arches.concat(
|
128 | process.env.npm_config_arches.split(',')
|
129 | );
|
130 | }
|
131 |
|
132 |
|
133 | if (options.targets.length > 0) {
|
134 | let chain = Promise.resolve();
|
135 | options.targets.forEach(function (target) {
|
136 | if (typeof target === 'object') {
|
137 | chain = chain.then(function () {
|
138 | return new Promise(function (resolve) {
|
139 | console.log(target.runtime, target.abi, target.platform, target.arch);
|
140 | install(
|
141 | target.runtime,
|
142 | target.abi,
|
143 | target.platform,
|
144 | target.arch,
|
145 | resolve
|
146 | );
|
147 | });
|
148 | });
|
149 | return;
|
150 | }
|
151 | let parts = target.split('-');
|
152 | let runtime = parts[0];
|
153 | let abi = parts[1];
|
154 | options.platforms.forEach(function (platform) {
|
155 | options.arches.forEach(function (arch) {
|
156 | if (platform === 'darwin' && arch === 'ia32') {
|
157 | return;
|
158 | }
|
159 | chain = chain.then(function () {
|
160 | return new Promise(function (resolve) {
|
161 | console.log(runtime, abi, platform, arch);
|
162 | install(runtime, abi, platform, arch, resolve);
|
163 | });
|
164 | });
|
165 | });
|
166 | });
|
167 | });
|
168 | } else {
|
169 | const runtime = process.versions['electron'] ? 'electron' : 'node';
|
170 | const abi = process.versions.modules;
|
171 | const platform = process.platform;
|
172 | const arch = process.arch;
|
173 | install(runtime, abi, platform, arch, function () {});
|
174 | }
|