forked from daquinoaldo/https-localhost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
76 lines (68 loc) · 2.25 KB
/
generate.js
File metadata and controls
76 lines (68 loc) · 2.25 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
74
75
76
#!/usr/bin/env node
const exec = require("child_process").exec
const fs = require("fs")
const https = require("https")
const MKCERT_VERSION = "v1.3.0"
// get the executable name
function getExe() {
/* istanbul ignore next: tested on all platform on travis */
switch (process.platform) {
case "darwin":
return "mkcert-" + MKCERT_VERSION + "-darwin-amd64"
case "linux":
return "mkcert-" + MKCERT_VERSION + "-linux-amd64"
case "win32":
return "mkcert-" + MKCERT_VERSION + "-windows-amd64.exe"
default:
console.warn("Cannot generate the localhost certificate on your " +
"platform. Please, consider contacting the developer if you can help.")
process.exit(0)
}
}
// download a binary file
function download(url, path) {
console.log("Downloading the mkcert executable...")
const file = fs.createWriteStream(path)
return new Promise(resolve => {
function get(url, file) {
https.get(url, (response) => {
if (response.statusCode === 302) get(response.headers.location, file)
else response.pipe(file).on("finish", resolve)
})
}
get(url, file)
})
}
// execute the binary executable to generate the certificates
function mkcert(path, exe) {
return new Promise((resolve, reject) => {
console.log("Running mkcert to generate certificates...")
exec(path + exe + " -install -cert-file " + path + "localhost.crt " +
"-key-file " + path + "localhost.key localhost", (err, stdout, stderr) => {
console.log(stdout)
console.error(stderr)
/* istanbul ignore if: cannot be tested */
if (err) reject(err)
resolve()
})
})
}
async function main() {
const url = "https://github.com/FiloSottile/mkcert/releases/download/" +
MKCERT_VERSION + "/"
const exe = getExe()
const path = "cert/"
// download the executable
await download(url + exe, path + exe)
// make binary executable
fs.chmodSync(path + exe, "0755")
// execute the binary
await mkcert(path, exe)
console.log("Certificates generated, installed and trusted. Ready to go!")
}
// run as script
/* istanbul ignore if: cannot be tested */
if (require.main === module)
try { main() } catch (err) { console.error("\nExec error: " + err) }
// export as module
module.exports = main