@@ -17,19 +17,24 @@ function checkUpdates() {
1717 host : "api.github.com" ,
1818 path : "/repos/daquinoaldo/https-localhost/releases/latest" ,
1919 method : "GET" ,
20- headers : { "User-Agent" : "node.js" }
20+ headers : { "User-Agent" : "node.js" } ,
2121 }
22- https . request ( options , res => {
23- let body = ""
24- res . on ( "data" , chunk => { body += chunk . toString ( "utf8" ) } )
25- res . on ( "end" , ( ) => {
26- const currentVersion = JSON . parse ( fs . readFileSync (
27- path . resolve ( __dirname , "package.json" ) ) ) . version
28- const latestVersion = JSON . parse ( body ) . tag_name . replace ( "v" , "" )
29- if ( currentVersion !== latestVersion )
30- console . warn ( "[https-localhost] New update available." )
22+ https
23+ . request ( options , res => {
24+ let body = ""
25+ res . on ( "data" , chunk => {
26+ body += chunk . toString ( "utf8" )
27+ } )
28+ res . on ( "end" , ( ) => {
29+ const currentVersion = JSON . parse (
30+ fs . readFileSync ( path . resolve ( __dirname , "package.json" ) ) ,
31+ ) . version
32+ const latestVersion = JSON . parse ( body ) . tag_name . replace ( "v" , "" )
33+ if ( currentVersion !== latestVersion )
34+ console . warn ( "[https-localhost] New update available." )
35+ } )
3136 } )
32- } ) . end ( )
37+ . end ( )
3338 } catch ( e ) {
3439 // Just catch everything, this is not a critic part and can fail.
3540 // It is important to not affect the script behavior.
@@ -50,8 +55,10 @@ function getExe() {
5055 case "win32" :
5156 return "mkcert-" + MKCERT_VERSION + "-windows-amd64.exe"
5257 default :
53- console . warn ( "Cannot generate the localhost certificate on your " +
54- "platform. Please, consider contacting the developer if you can help." )
58+ console . warn (
59+ "Cannot generate the localhost certificate on your " +
60+ "platform. Please, consider contacting the developer if you can help." ,
61+ )
5562 process . exit ( 0 )
5663 }
5764}
@@ -62,7 +69,7 @@ function download(url, path) {
6269 const file = fs . createWriteStream ( path )
6370 return new Promise ( resolve => {
6471 function get ( url , file ) {
65- https . get ( url , ( response ) => {
72+ https . get ( url , response => {
6673 if ( response . statusCode === 302 ) get ( response . headers . location , file )
6774 else response . pipe ( file ) . on ( "finish" , resolve )
6875 } )
@@ -75,26 +82,23 @@ function download(url, path) {
7582async function mkcert ( appDataPath , exe , domain ) {
7683 // fix problems with spaces
7784 /* istanbul ignore next: platform dependent */
78- const escapeSpaces = function ( path ) {
85+ const escapeSpaces = function ( path ) {
7986 // escape spaces (not already escaped)
8087 if ( process . platform === "darwin" || process . platform === "linux" )
8188 return path . replace ( / (?< ! \\ ) / g, "\\ " )
8289 // use apex on Windows
83- if ( process . platform === "win32" )
84- return "\"" + path + "\""
90+ if ( process . platform === "win32" ) return '"' + path + '"'
8591 return path
8692 }
8793
8894 const exePath = escapeSpaces ( path . join ( appDataPath , exe ) )
8995 const crtPath = escapeSpaces ( path . join ( appDataPath , domain + ".crt" ) )
9096 const keyPath = escapeSpaces ( path . join ( appDataPath , domain + ".key" ) )
91- const cmd = `${ exePath } -install -cert-file ${ crtPath } ` +
92- ` -key-file ${ keyPath } ${ domain } `
97+ const cmd = `${ exePath } -install -cert-file ${ crtPath } ` + ` -key-file ${ keyPath } ${ domain } `
9398
9499 // sleep on windows due to issue #28
95100 /* istanbul ignore if: cannot be tested */
96- if ( process . platform === "win32" )
97- await new Promise ( resolve => setTimeout ( resolve , 3000 ) )
101+ if ( process . platform === "win32" ) await new Promise ( resolve => setTimeout ( resolve , 3000 ) )
98102
99103 return new Promise ( ( resolve , reject ) => {
100104 console . log ( "Running mkcert to generate certificates..." )
@@ -117,15 +121,12 @@ async function mkcert(appDataPath, exe, domain) {
117121async function generate ( appDataPath = CERT_PATH , customDomain = undefined ) {
118122 const domain = customDomain || "localhost"
119123 console . info ( "Generating certificates..." )
120- console . log ( "Certificates path: " + appDataPath +
121- ". Never modify nor share this files." )
124+ console . log ( "Certificates path: " + appDataPath + ". Never modify nor share this files." )
122125 // mkdir if not exists
123126 /* istanbul ignore else: not relevant */
124- if ( ! fs . existsSync ( appDataPath ) )
125- fs . mkdirSync ( appDataPath , { recursive : true } )
127+ if ( ! fs . existsSync ( appDataPath ) ) fs . mkdirSync ( appDataPath , { recursive : true } )
126128 // build the executable url and path
127- const url = "https://github.com/FiloSottile/mkcert/releases/download/" +
128- MKCERT_VERSION + "/"
129+ const url = "https://github.com/FiloSottile/mkcert/releases/download/" + MKCERT_VERSION + "/"
129130 const exe = getExe ( )
130131 const exePath = path . join ( appDataPath , exe )
131132 // download the executable
@@ -144,19 +145,19 @@ async function getCerts(customDomain = undefined) {
144145 /* istanbul ignore if: cannot test pkg */
145146 if ( process . pkg ) checkUpdates ( )
146147 // check if a reinstall is forced or needed by a mkcert update
147- if ( process . env . REINSTALL ||
148- ! fs . existsSync ( path . join ( certPath , getExe ( ) ) ) )
148+ if ( process . env . REINSTALL || ! fs . existsSync ( path . join ( certPath , getExe ( ) ) ) )
149149 await generate ( certPath , domain )
150150 try {
151151 return {
152152 key : fs . readFileSync ( path . join ( certPath , domain + ".key" ) ) ,
153- cert : fs . readFileSync ( path . join ( certPath , domain + ".crt" ) )
153+ cert : fs . readFileSync ( path . join ( certPath , domain + ".crt" ) ) ,
154154 }
155155 } catch ( e ) {
156156 /* istanbul ignore else: should never occur */
157157 if ( certPath !== CERT_PATH ) {
158- console . error ( "Cannot find localhost.key and localhost.crt in the" +
159- " specified path: " + certPath )
158+ console . error (
159+ "Cannot find localhost.key and localhost.crt in the" + " specified path: " + certPath ,
160+ )
160161 process . exit ( 1 )
161162 } else {
162163 // Missing certificates (first run)
@@ -171,8 +172,7 @@ async function getCerts(customDomain = undefined) {
171172// delete a folder and the file inside it
172173function remove ( appDataPath = CERT_PATH ) {
173174 if ( fs . existsSync ( appDataPath ) ) {
174- fs . readdirSync ( appDataPath )
175- . forEach ( file => fs . unlinkSync ( path . join ( appDataPath , file ) ) )
175+ fs . readdirSync ( appDataPath ) . forEach ( file => fs . unlinkSync ( path . join ( appDataPath , file ) ) )
176176 fs . rmdirSync ( appDataPath )
177177 }
178178}
@@ -181,17 +181,23 @@ function remove(appDataPath = CERT_PATH) {
181181/* istanbul ignore if: cannot be tested */
182182if ( require . main === module )
183183 // if run with -u or --uninstall
184- if ( process . argv . length === 3 &&
185- ( process . argv [ 2 ] === "-u" || process . argv [ 2 ] === "--uninstall" ) ) {
184+ if (
185+ process . argv . length === 3 &&
186+ ( process . argv [ 2 ] === "-u" || process . argv [ 2 ] === "--uninstall" )
187+ ) {
186188 remove ( )
187189 console . info ( "Certificates removed." )
188- } else try { // install
189- generate ( )
190- } catch ( err ) { console . error ( "\nExec error: " + err ) }
190+ } else
191+ try {
192+ // install
193+ generate ( )
194+ } catch ( err ) {
195+ console . error ( "\nExec error: " + err )
196+ }
191197
192198// export as module
193199module . exports = {
194200 getCerts,
195201 generate,
196- remove
202+ remove,
197203}
0 commit comments