@@ -69,16 +69,16 @@ function download(url, path) {
6969}
7070
7171// execute the binary executable to generate the certificates
72- function mkcert ( appDataPath , exe ) {
72+ function mkcert ( appDataPath , exe , domain ) {
7373 const logPath = path . join ( appDataPath , "mkcert.log" )
7474 const errPath = path . join ( appDataPath , "mkcert.err" )
7575 // escape spaces in appDataPath (Mac OS)
7676 appDataPath = appDataPath . replace ( " " , "\\ " )
7777 const exePath = path . join ( appDataPath , exe )
78- const crtPath = path . join ( appDataPath , "localhost .crt")
79- const keyPath = path . join ( appDataPath , "localhost .key")
78+ const crtPath = path . join ( appDataPath , domain + " .crt")
79+ const keyPath = path . join ( appDataPath , domain + " .key")
8080 const cmd = exePath + " -install -cert-file " + crtPath +
81- " -key-file " + keyPath + " localhost"
81+ " -key-file " + keyPath + " " + domain
8282 return new Promise ( ( resolve , reject ) => {
8383 console . log ( "Running mkcert to generate certificates..." )
8484 exec ( cmd , ( err , stdout , stderr ) => {
@@ -96,7 +96,8 @@ function mkcert(appDataPath, exe) {
9696 } )
9797}
9898
99- async function generate ( appDataPath = CERT_PATH ) {
99+ async function generate ( appDataPath = CERT_PATH , customDomain = undefined ) {
100+ const domain = customDomain || "localhost"
100101 console . info ( "Generating certificates..." )
101102 console . log ( "Certificates path: " + appDataPath +
102103 ". Never modify nor share this files." )
@@ -114,23 +115,24 @@ async function generate(appDataPath = CERT_PATH) {
114115 // make binary executable
115116 fs . chmodSync ( exePath , "0755" )
116117 // execute the binary
117- await mkcert ( appDataPath , exe )
118+ await mkcert ( appDataPath , exe , domain )
118119 console . log ( "Certificates generated, installed and trusted. Ready to go!" )
119120}
120121
121- async function getCerts ( ) {
122+ async function getCerts ( customDomain = undefined ) {
123+ const domain = customDomain || "localhost"
122124 const certPath = process . env . CERT_PATH || CERT_PATH
123125 // check for updates if running as executable
124126 /* istanbul ignore if: cannot test pkg */
125127 if ( process . pkg ) checkUpdates ( )
126128 // check if a reinstall is forced or needed by a mkcert update
127129 if ( process . env . REINSTALL ||
128130 ! fs . existsSync ( path . join ( certPath , getExe ( ) ) ) )
129- await generate ( certPath )
131+ await generate ( certPath , domain )
130132 try {
131133 return {
132- key : fs . readFileSync ( path . join ( certPath , "localhost .key") ) ,
133- cert : fs . readFileSync ( path . join ( certPath , "localhost .crt") )
134+ key : fs . readFileSync ( path . join ( certPath , domain + " .key") ) ,
135+ cert : fs . readFileSync ( path . join ( certPath , domain + " .crt") )
134136 }
135137 } catch ( e ) {
136138 /* istanbul ignore else: should never occur */
@@ -141,9 +143,9 @@ async function getCerts() {
141143 } else {
142144 // Missing certificates (first run)
143145 // generate the certificate
144- await generate ( CERT_PATH )
146+ await generate ( CERT_PATH , domain )
145147 // recursive call
146- return getCerts ( )
148+ return getCerts ( domain )
147149 }
148150 }
149151}
0 commit comments