forked from shinnn/mkcert-prebuilt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
73 lines (60 loc) · 2.03 KB
/
install.js
File metadata and controls
73 lines (60 loc) · 2.03 KB
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
'use strict';
if (process.platform !== 'darwin' && process.platform !== 'linux' && process.platform !== 'win32') {
const platformName = require('platform-name');
console.error(`Prebuilt mkcert binaries are only provided for Linux, macOS and Windows, not for ${
platformName()
}.`);
process.exit(1);
}
const arch = require('arch');
if (arch() !== 'x64') {
console.error('mkcert doesn\'t support non 64 bit architectures.');
process.exit(1);
}
const {basename} = require('path');
const {pipeline} = require('stream');
const {promisify} = require('util');
const loadFromCwdOrNpm = require('load-from-cwd-or-npm');
const rejectUnsatisfiedNpmVersion = require('reject-unsatisfied-npm-version');
const {bin: {mkcert: binName}, name, version: pkgVersion} = require('./package.json');
const version = basename(binName, '.bin');
const url = `https://github.com/shinnn/${name}/releases/download/v${pkgVersion}/${
version
}-${process.platform}.tgz`;
const envKeys = Object.keys(process.env);
function caseInsensitivelyEqual(val) {
return key => key.toUpperCase() === val;
}
for (const [config, npmConfig] of new Map([
['HTTP_PROXY', 'NPM_CONFIG_HTTP_PROXY'],
['HTTPS_PROXY', 'NPM_CONFIG_HTTPS_PROXY'],
['NO_PROXY', 'NPM_CONFIG_NOPROXY']
])) {
const foundNpmConfigKey = envKeys.find(caseInsensitivelyEqual(npmConfig));
if (foundNpmConfigKey && !envKeys.some(caseInsensitivelyEqual(config))) {
process.env[config] = process.env[foundNpmConfigKey];
}
}
(async () => {
try {
const [request, {Unpack}] = await Promise.all([
loadFromCwdOrNpm('request'),
loadFromCwdOrNpm('tar'),
rejectUnsatisfiedNpmVersion('6.5.0')
]);
await promisify(pipeline)(request({
url,
headers: {
'user-agent': `${name}/${pkgVersion}`
}
}).on('response', function({statusCode, statusMessage}) {
if (statusCode === 200) {
return;
}
this.emit('error', new Error(`Failed to download an archive from ${url} (${statusCode} ${statusMessage})`));
}), new Unpack({strict: true}));
} catch ({stack}) {
console.error(stack);
process.exit(1);
}
})();