11#!/usr/bin/env node
2- import { existsSync } from "fs" ;
3- import { mkdir , writeFile } from " fs/promises" ;
4- import minimist from " minimist" ;
5- import pc from " picocolors" ;
6- import { DATA_DIR , resolvePath , readFile , pkgVersion } from " ./src/utils.js" ;
7- import { createCertificate } from " ./src/index.js" ;
2+ import { existsSync } from 'fs' ;
3+ import { mkdir , writeFile } from ' fs/promises' ;
4+ import minimist from ' minimist' ;
5+ import pc from ' picocolors' ;
6+ import { DATA_DIR , resolvePath , readFile , pkgVersion } from ' ./src/utils.js' ;
7+ import { createCertificate } from ' ./src/index.js' ;
88
99/**
1010 * Init, read variables and create folders
1111 */
1212const defaults = {
13- dir : resolvePath ( " certs" , DATA_DIR ) ,
13+ dir : resolvePath ( ' certs' , DATA_DIR ) ,
1414 key : 'dev.key' ,
15- cert : 'dev.cert'
16- }
15+ cert : 'dev.cert' ,
16+ } ;
1717
1818const argv = minimist ( process . argv . slice ( 2 ) ) ;
1919const cwd = process . cwd ( ) ;
@@ -26,7 +26,6 @@ if (argv.version) {
2626
2727// Display help and exit
2828if ( argv . h || argv . help ) {
29-
3029 console . log ( `
3130 ${ pc . bold ( 'Usage' ) }
3231 $ npx mkcert-cli <options>
@@ -54,31 +53,29 @@ const verbose = argv.v ?? false;
5453const host = argv . host ;
5554const force = ( argv . force || argv . f ) ?? false ;
5655const autoUpgrade = ( argv . upgrade || argv . u ) ?? false ;
57- const outDir = ( argv . outDir || argv . o ) ? resolvePath ( argv . outDir || argv . o , cwd ) : defaults . dir ;
58- const hosts = host
59- ? Array . isArray ( host )
60- ? host
61- : [ host ]
62- : [ ] ;
56+ const outDir = argv . outDir || argv . o ? resolvePath ( argv . outDir || argv . o , cwd ) : defaults . dir ;
57+ const hosts = host ? ( Array . isArray ( host ) ? host : [ host ] ) : [ ] ;
6358const certFile = ( argv . c || argv . cert ) ?? defaults . cert ;
6459const keyFile = ( argv . k || argv . key ) ?? defaults . key ;
6560const certFilePath = resolvePath ( certFile , outDir ) ;
6661const keyFilePath = resolvePath ( keyFile , outDir ) ;
6762const dryRun = ( argv . d || argv . dryRun ) ?? false ;
6863
69- console . log ( `\nRunning ${ pc . green ( `${ pc . bold ( " mkcert-cli" ) } ` ) } (${ pkgVersion } )\n` ) ;
64+ console . log ( `\nRunning ${ pc . green ( `${ pc . bold ( ' mkcert-cli' ) } ` ) } (${ pkgVersion } )\n` ) ;
7065
7166( dryRun || verbose ) &&
72- console . log ( `${ pc . bold ( " With options:" ) }
73- - ${ pc . blue ( " cwd" ) } : ${ pc . yellow ( cwd ) }
74- - ${ pc . blue ( " outDir" ) } : ${ pc . yellow ( outDir ) }
75- - ${ pc . blue ( " hosts" ) } : ${ JSON . stringify ( hosts ) }
76- - ${ pc . blue ( " cert" ) } : ${ certFile }
77- - ${ pc . blue ( " key" ) } : ${ keyFile }
78- - ${ pc . blue ( " force" ) } : ${ force }
79- - ${ pc . blue ( " autoUpgrade" ) } : ${ autoUpgrade }
67+ console . log ( `${ pc . bold ( ' With options:' ) }
68+ - ${ pc . blue ( ' cwd' ) } : ${ pc . yellow ( cwd ) }
69+ - ${ pc . blue ( ' outDir' ) } : ${ pc . yellow ( outDir ) }
70+ - ${ pc . blue ( ' hosts' ) } : ${ JSON . stringify ( hosts ) }
71+ - ${ pc . blue ( ' cert' ) } : ${ certFile }
72+ - ${ pc . blue ( ' key' ) } : ${ keyFile }
73+ - ${ pc . blue ( ' force' ) } : ${ force }
74+ - ${ pc . blue ( ' autoUpgrade' ) } : ${ autoUpgrade }
8075` ) ;
81- if ( dryRun ) { process . exit ( ) }
76+ if ( dryRun ) {
77+ process . exit ( ) ;
78+ }
8279
8380if ( ! existsSync ( outDir ) ) {
8481 await mkdir ( outDir , { recursive : true } ) ;
@@ -90,9 +87,7 @@ if (!existsSync(outDir)) {
9087const filesExist = existsSync ( certFilePath ) && existsSync ( keyFilePath ) ;
9188const writeFiles = force || ! filesExist ;
9289if ( ! writeFiles ) {
93- console . log ( `🎉 Files "${ pc . magenta ( certFile ) } " and "${ pc . magenta (
94- keyFile
95- ) } " already exist
90+ console . log ( `🎉 Files "${ pc . magenta ( certFile ) } " and "${ pc . magenta ( keyFile ) } " already exist
9691 in ${ pc . yellow ( outDir ) } ` ) ;
9792 process . exit ( 0 ) ;
9893}
10499 keyFilePath,
105100 certFilePath,
106101 } ) ;
107- await writeFile ( keyFilePath , key , { encoding : " utf-8" } ) ;
108- await writeFile ( certFilePath , cert , { encoding : " utf-8" } ) ;
102+ await writeFile ( keyFilePath , key , { encoding : ' utf-8' } ) ;
103+ await writeFile ( certFilePath , cert , { encoding : ' utf-8' } ) ;
109104} catch ( /** @type {any }*/ writeErr ) {
110105 console . error ( writeErr . toString ( ) ) ;
111106 process . exit ( 1 ) ;
0 commit comments