@@ -3,38 +3,82 @@ import { existsSync } from "fs";
33import { mkdir , writeFile } from "fs/promises" ;
44import minimist from "minimist" ;
55import pc from "picocolors" ;
6- import { DATA_DIR , resolvePath } from "./src/utils.js" ;
6+ import { DATA_DIR , resolvePath , readFile , pkgVersion } from "./src/utils.js" ;
77import { createCertificate } from "./src/index.js" ;
88
99/**
1010 * Init, read variables and create folders
1111 */
12- const defaultOutDir = resolvePath ( "certs" , DATA_DIR ) ;
12+ const defaults = {
13+ dir : resolvePath ( "certs" , DATA_DIR ) ,
14+ key : 'dev.key' ,
15+ cert : 'dev.cert'
16+ }
17+
1318const argv = minimist ( process . argv . slice ( 2 ) ) ;
1419const cwd = process . cwd ( ) ;
1520
16- const force = argv . f ?? false ;
21+ // Print version and exit
22+ if ( argv . version ) {
23+ console . log ( pkgVersion ) ;
24+ process . exit ( ) ;
25+ }
26+
27+ // Display help and exit
28+ if ( argv . h || argv . help ) {
29+
30+ console . log ( `
31+ ${ pc . bold ( 'Usage' ) }
32+ $ npx mkcert-cli <options>
33+
34+ ${ pc . bold ( 'Options' ) }
35+ ${ pc . blue ( '--outDir, -o' ) } Certificates output dir, defaults to ${ pc . bold ( defaults . dir ) }
36+ ${ pc . blue ( '--host' ) } Add custom host, defaults to ${ pc . bold ( '[localhost, ...IPv4]' ) }
37+ ${ pc . blue ( '--keyFile, -k' ) } Name of key file, defaults to "${ pc . bold ( defaults . key ) } "
38+ ${ pc . blue ( '--certFile, -c' ) } Name of cert file, defaults to "${ pc . bold ( defaults . cert ) } "
39+ ${ pc . blue ( '--force, -f' ) } Force recreate certificates
40+ ${ pc . blue ( '--upgrade, -u' ) } Upgrade mkcert binary if new version is available
41+ ${ pc . blue ( '--dryRun, -d' ) } Print all parameters without doing anything
42+
43+ ${ pc . bold ( 'Examples' ) }
44+ $ npx mkcert-cli --host my-domain.local --host my-other-domain.local --outDir .
45+ $ npx mkcert-cli -c myCert.pem -k myKey.key
46+ $ npx mkcert-cli -fu -o .
47+
48+ ` ) ;
49+ process . exit ( ) ;
50+ }
51+
1752const verbose = argv . v ?? false ;
18- const outDir = argv . outDir ? resolvePath ( argv . outDir , cwd ) : defaultOutDir ;
19- const hosts = argv . host ? ( Array . isArray ( argv . host ) ? argv . host : [ argv . host ] ) : [ ] ;
20- const certFile = argv . cert ?? "dev.cert" ;
21- const keyFile = argv . key ?? "dev.key" ;
53+
54+ const host = argv . host ;
55+ const force = ( argv . force || argv . f ) ?? false ;
56+ const 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+ : [ ] ;
63+ const certFile = ( argv . c || argv . cert ) ?? defaults . cert ;
64+ const keyFile = ( argv . k || argv . key ) ?? defaults . key ;
2265const certFilePath = resolvePath ( certFile , outDir ) ;
2366const keyFilePath = resolvePath ( keyFile , outDir ) ;
67+ const dryRun = ( argv . d || argv . dryRun ) ?? false ;
2468
25- console . log ( `
26- Running ${ pc . green ( `${ pc . bold ( "mkcert-cli" ) } ` ) } ...
27- ` ) ;
69+ console . log ( `\nRunning ${ pc . green ( `${ pc . bold ( "mkcert-cli" ) } ` ) } (${ pkgVersion } )\n` ) ;
2870
29- verbose &&
71+ ( dryRun || verbose ) &&
3072 console . log ( `${ pc . bold ( "With options:" ) }
3173 - ${ pc . blue ( "cwd" ) } : ${ pc . yellow ( cwd ) }
3274 - ${ pc . blue ( "outDir" ) } : ${ pc . yellow ( outDir ) }
33- - ${ pc . blue ( "host" ) } : ${ JSON . stringify ( hosts ) }
34- - ${ pc . blue ( "force" ) } : ${ force }
75+ - ${ pc . blue ( "hosts" ) } : ${ JSON . stringify ( hosts ) }
3576 - ${ pc . blue ( "cert" ) } : ${ certFile }
3677 - ${ pc . blue ( "key" ) } : ${ keyFile }
78+ - ${ pc . blue ( "force" ) } : ${ force }
79+ - ${ pc . blue ( "autoUpgrade" ) } : ${ autoUpgrade }
3780` ) ;
81+ if ( dryRun ) { process . exit ( ) }
3882
3983if ( ! existsSync ( outDir ) ) {
4084 await mkdir ( outDir , { recursive : true } ) ;
@@ -46,13 +90,20 @@ if (!existsSync(outDir)) {
4690const filesExist = existsSync ( certFilePath ) && existsSync ( keyFilePath ) ;
4791const writeFiles = force || ! filesExist ;
4892if ( ! writeFiles ) {
49- console . log ( `🎉 Files "${ pc . magenta ( certFile ) } " and "${ pc . magenta ( keyFile ) } " already exist
93+ console . log ( `🎉 Files "${ pc . magenta ( certFile ) } " and "${ pc . magenta (
94+ keyFile
95+ ) } " already exist
5096 in ${ pc . yellow ( outDir ) } ` ) ;
5197 process . exit ( 0 ) ;
5298}
5399
54100try {
55- const { cert, key } = await createCertificate ( { force, autoUpgrade : false , keyFilePath, certFilePath } ) ;
101+ const { cert, key } = await createCertificate ( {
102+ force,
103+ autoUpgrade,
104+ keyFilePath,
105+ certFilePath,
106+ } ) ;
56107 await writeFile ( keyFilePath , key , { encoding : "utf-8" } ) ;
57108 await writeFile ( certFilePath , cert , { encoding : "utf-8" } ) ;
58109} catch ( /** @type {any }*/ writeErr ) {
0 commit comments